Skip to content

Commit bfafa63

Browse files
Merge pull request #77 from CYX22222003/branch-update-edit
Attempt to update new edit command
2 parents f41204e + b68ae73 commit bfafa63

File tree

8 files changed

+68
-6
lines changed

8 files changed

+68
-6
lines changed

src/main/java/seedu/address/logic/commands/EditCommand.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,16 @@ public CommandResult execute(Model model) throws CommandException {
7676
Person personToEdit = lastShownList.get(index.getZeroBased());
7777
Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor);
7878

79-
if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
79+
// if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
80+
// throw new CommandException(MESSAGE_DUPLICATE_PERSON);
81+
// }
82+
model.deletePerson(personToEdit);
83+
if (model.hasPerson(editedPerson)) {
84+
model.insertPerson(personToEdit, index.getZeroBased());
8085
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
8186
}
8287

83-
model.setPerson(personToEdit, editedPerson);
88+
model.insertPerson(editedPerson, index.getZeroBased());
8489
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
8590
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
8691
}

src/main/java/seedu/address/model/CampusConnect.java

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ public void addPerson(Person p) {
7575
persons.add(p);
7676
}
7777

78+
/**
79+
* Adds a person to the specific position of the CampusConnect.
80+
*
81+
*/
82+
public void addPerson(Person p, int ind) {
83+
persons.add(ind, p);
84+
}
85+
7886
/**
7987
* Replaces the given person {@code target} in the list with {@code editedPerson}.
8088
* {@code target} must exist in the address book.

src/main/java/seedu/address/model/Model.java

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public interface Model {
6969
*/
7070
void addPerson(Person person);
7171

72+
/**
73+
* Inserts person at the specific position.
74+
*/
75+
void insertPerson(Person p, int ind);
76+
7277
/**
7378
* Replaces the given person {@code target} with {@code editedPerson}.
7479
* {@code target} must exist in the address book.

src/main/java/seedu/address/model/ModelManager.java

+6
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public void addPerson(Person person) {
104104
updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
105105
}
106106

107+
@Override
108+
public void insertPerson(Person p , int ind) {
109+
campusConnect.addPerson(p, ind);
110+
updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
111+
}
112+
107113
@Override
108114
public void setPerson(Person target, Person editedPerson) {
109115
requireAllNonNull(target, editedPerson);

src/main/java/seedu/address/model/person/UniquePersonList.java

+13
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ public void add(Person toAdd) {
4848
internalList.add(toAdd);
4949
}
5050

51+
/**
52+
* Add a person to the specific position of the list.
53+
* The index must be valid
54+
*/
55+
public void add(int ind, Person toAdd) {
56+
requireNonNull(toAdd);
57+
if (contains(toAdd)) {
58+
throw new DuplicatePersonException();
59+
}
60+
internalList.add(ind, toAdd);
61+
}
62+
63+
5164
/**
5265
* Replaces the person {@code target} in the list with {@code editedPerson}.
5366
* {@code target} must exist in the list.

src/test/java/seedu/address/logic/commands/AddCommandTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ public ObservableList<Person> getFilteredPersonList() {
157157
public void updateFilteredPersonList(Predicate<Person> predicate) {
158158
throw new AssertionError("This method should not be called.");
159159
}
160+
161+
@Override
162+
public void insertPerson(Person p, int ind) {
163+
throw new AssertionError("This method should not be called");
164+
}
160165
}
161166

162167
/**

src/test/java/seedu/address/logic/commands/CommandTestUtil.java

+12
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ public static void assertCommandFailure(Command command, Model actualModel, Stri
105105
assertEquals(expectedCampusConnect, actualModel.getCampusConnect());
106106
assertEquals(expectedFilteredList, actualModel.getFilteredPersonList());
107107
}
108+
109+
/**
110+
* Executes the given code command without comparing data model
111+
*
112+
* @param command command object to be executed
113+
* @param model model to be executed on
114+
* @param expectedMsg expected message to be thrown
115+
*/
116+
public static void assertCommandFailureWithoutModel(Command command, Model model, String expectedMsg) {
117+
assertThrows(CommandException.class, expectedMsg, () -> command.execute(model));
118+
}
119+
108120
/**
109121
* Updates {@code model}'s filtered list to show only the person at the given {@code targetIndex} in the
110122
* {@code model}'s address book.

src/test/java/seedu/address/logic/commands/EditCommandTest.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB;
1111
import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND;
1212
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
13+
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailureWithoutModel;
1314
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
1415
import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex;
1516
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
@@ -45,7 +46,10 @@ public void execute_allFieldsSpecifiedUnfilteredList_success() {
4546
String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson));
4647

4748
Model expectedModel = new ModelManager(new CampusConnect(model.getCampusConnect()), new UserPrefs());
48-
expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson);
49+
50+
Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
51+
expectedModel.deletePerson(firstPerson);
52+
expectedModel.insertPerson(editedPerson, INDEX_FIRST_PERSON.getZeroBased());
4953

5054
assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
5155
}
@@ -79,6 +83,9 @@ public void execute_noFieldSpecifiedUnfilteredList_success() {
7983
String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson));
8084

8185
Model expectedModel = new ModelManager(new CampusConnect(model.getCampusConnect()), new UserPrefs());
86+
Person firstPerson = expectedModel.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
87+
expectedModel.deletePerson(firstPerson);
88+
expectedModel.insertPerson(firstPerson, INDEX_FIRST_PERSON.getZeroBased());
8289

8390
assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
8491
}
@@ -104,7 +111,8 @@ public void execute_filteredList_success() {
104111
String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson));
105112

106113
Model expectedModel = new ModelManager(new CampusConnect(model.getCampusConnect()), new UserPrefs());
107-
expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson);
114+
expectedModel.deletePerson(personInFilteredList);
115+
expectedModel.insertPerson(editedPerson, INDEX_FIRST_PERSON.getZeroBased());
108116

109117
assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
110118
}
@@ -115,7 +123,7 @@ public void execute_duplicatePersonUnfilteredList_failure() {
115123
EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(firstPerson).build();
116124
EditCommand editCommand = new EditCommand(INDEX_SECOND_PERSON, descriptor);
117125

118-
assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON);
126+
assertCommandFailureWithoutModel(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON);
119127
}
120128

121129
@Test
@@ -127,7 +135,7 @@ public void execute_duplicatePersonFilteredList_failure() {
127135
EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON,
128136
new EditPersonDescriptorBuilder(personInList).build());
129137

130-
assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON);
138+
assertCommandFailureWithoutModel(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON);
131139
}
132140

133141
@Test

0 commit comments

Comments
 (0)