Skip to content

Commit

Permalink
Implement features to insert persons
Browse files Browse the repository at this point in the history
  • Loading branch information
CYX22222003 committed Oct 16, 2024
1 parent dc54137 commit b68ae73
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public CommandResult execute(Model model) throws CommandException {
// }
model.deletePerson(personToEdit);
if (model.hasPerson(editedPerson)) {
model.addPerson(personToEdit);
model.insertPerson(personToEdit, index.getZeroBased());
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

model.addPerson(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();

Check warning on line 58 in src/main/java/seedu/address/model/person/UniquePersonList.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L58 was not covered by tests
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void execute_allFieldsSpecifiedUnfilteredList_success() {

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

assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
}
Expand Down Expand Up @@ -85,7 +85,7 @@ public void execute_noFieldSpecifiedUnfilteredList_success() {
Model expectedModel = new ModelManager(new CampusConnect(model.getCampusConnect()), new UserPrefs());
Person firstPerson = expectedModel.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
expectedModel.deletePerson(firstPerson);
expectedModel.addPerson(firstPerson);
expectedModel.insertPerson(firstPerson, INDEX_FIRST_PERSON.getZeroBased());

assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
}
Expand All @@ -112,7 +112,7 @@ public void execute_filteredList_success() {

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

assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
}
Expand Down

0 comments on commit b68ae73

Please sign in to comment.