Skip to content

Commit

Permalink
Merge pull request #77 from CYX22222003/branch-update-edit
Browse files Browse the repository at this point in the history
Attempt to update new edit command
  • Loading branch information
blackpanther9229 authored Oct 17, 2024
2 parents f41204e + b68ae73 commit bfafa63
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,16 @@ public CommandResult execute(Model model) throws CommandException {
Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor);

if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
// if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
// throw new CommandException(MESSAGE_DUPLICATE_PERSON);
// }
model.deletePerson(personToEdit);
if (model.hasPerson(editedPerson)) {
model.insertPerson(personToEdit, index.getZeroBased());
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

model.setPerson(personToEdit, editedPerson);
model.insertPerson(editedPerson, index.getZeroBased());
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/seedu/address/model/CampusConnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ public void addPerson(Person p) {
persons.add(p);
}

/**
* Adds a person to the specific position of the CampusConnect.
*
*/
public void addPerson(Person p, int ind) {
persons.add(ind, p);
}

/**
* Replaces the given person {@code target} in the list with {@code editedPerson}.
* {@code target} must exist in the address book.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public interface Model {
*/
void addPerson(Person person);

/**
* Inserts person at the specific position.
*/
void insertPerson(Person p, int ind);

/**
* Replaces the given person {@code target} with {@code editedPerson}.
* {@code target} must exist in the address book.
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public void addPerson(Person person) {
updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
}

@Override
public void insertPerson(Person p , int ind) {
campusConnect.addPerson(p, ind);
updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
}

@Override
public void setPerson(Person target, Person editedPerson) {
requireAllNonNull(target, editedPerson);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/seedu/address/model/person/UniquePersonList.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public void add(Person toAdd) {
internalList.add(toAdd);
}

/**
* Add a person to the specific position of the list.
* The index must be valid
*/
public void add(int ind, Person toAdd) {
requireNonNull(toAdd);
if (contains(toAdd)) {
throw new DuplicatePersonException();
}
internalList.add(ind, toAdd);
}


/**
* Replaces the person {@code target} in the list with {@code editedPerson}.
* {@code target} must exist in the list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public ObservableList<Person> getFilteredPersonList() {
public void updateFilteredPersonList(Predicate<Person> predicate) {
throw new AssertionError("This method should not be called.");
}

@Override
public void insertPerson(Person p, int ind) {
throw new AssertionError("This method should not be called");
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/seedu/address/logic/commands/CommandTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ public static void assertCommandFailure(Command command, Model actualModel, Stri
assertEquals(expectedCampusConnect, actualModel.getCampusConnect());
assertEquals(expectedFilteredList, actualModel.getFilteredPersonList());
}

/**
* Executes the given code command without comparing data model
*
* @param command command object to be executed
* @param model model to be executed on
* @param expectedMsg expected message to be thrown
*/
public static void assertCommandFailureWithoutModel(Command command, Model model, String expectedMsg) {
assertThrows(CommandException.class, expectedMsg, () -> command.execute(model));
}

/**
* Updates {@code model}'s filtered list to show only the person at the given {@code targetIndex} in the
* {@code model}'s address book.
Expand Down
16 changes: 12 additions & 4 deletions src/test/java/seedu/address/logic/commands/EditCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailureWithoutModel;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
Expand Down Expand Up @@ -45,7 +46,10 @@ public void execute_allFieldsSpecifiedUnfilteredList_success() {
String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson));

Model expectedModel = new ModelManager(new CampusConnect(model.getCampusConnect()), new UserPrefs());
expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson);

Person firstPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
expectedModel.deletePerson(firstPerson);
expectedModel.insertPerson(editedPerson, INDEX_FIRST_PERSON.getZeroBased());

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

Model expectedModel = new ModelManager(new CampusConnect(model.getCampusConnect()), new UserPrefs());
Person firstPerson = expectedModel.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
expectedModel.deletePerson(firstPerson);
expectedModel.insertPerson(firstPerson, INDEX_FIRST_PERSON.getZeroBased());

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

Model expectedModel = new ModelManager(new CampusConnect(model.getCampusConnect()), new UserPrefs());
expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson);
expectedModel.deletePerson(personInFilteredList);
expectedModel.insertPerson(editedPerson, INDEX_FIRST_PERSON.getZeroBased());

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

assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON);
assertCommandFailureWithoutModel(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON);
}

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

assertCommandFailure(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON);
assertCommandFailureWithoutModel(editCommand, model, EditCommand.MESSAGE_DUPLICATE_PERSON);
}

@Test
Expand Down

0 comments on commit bfafa63

Please sign in to comment.