Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit ecdd485

Browse files
authored
[#621] UniquePersonList: Simplify logic of updatePerson(...) (#616)
This method retrieves the person in the list to be updated, and calls Person#resetData(ReadOnlyPerson) to update the values of that person. We do not need to retrieve the person in the list to be updated, to update the list accordingly. Let’s simplify the logic by setting the updated Person object in place of the person to be updated.
2 parents a60d16b + 895430f commit ecdd485

File tree

4 files changed

+6
-21
lines changed

4 files changed

+6
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public CommandResult executeUndoableCommand() throws CommandException {
8383
throw new AssertionError("The target person cannot be missing");
8484
}
8585
model.updateFilteredListToShowAll();
86-
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, personToEdit));
86+
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, editedPerson));
8787
}
8888

8989
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void updatePerson(ReadOnlyPerson target, ReadOnlyPerson editedReadOnlyPer
106106
// TODO: the tags master list will be updated even though the below line fails.
107107
// This can cause the tags master list to have additional tags that are not tagged to any person
108108
// in the person list.
109-
persons.updatePerson(target, editedPerson);
109+
persons.setPerson(target, editedPerson);
110110
}
111111

112112
/**

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,6 @@ public void setTags(Set<Tag> replacement) {
122122
tags.set(new UniqueTagList(replacement));
123123
}
124124

125-
/**
126-
* Updates this person with the details of {@code replacement}.
127-
*/
128-
public void resetData(ReadOnlyPerson replacement) {
129-
requireNonNull(replacement);
130-
131-
this.setName(replacement.getName());
132-
this.setPhone(replacement.getPhone());
133-
this.setEmail(replacement.getEmail());
134-
this.setAddress(replacement.getAddress());
135-
this.setTags(replacement.getTags());
136-
}
137-
138125
@Override
139126
public boolean equals(Object other) {
140127
return other == this // short circuit if same object

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ public void add(ReadOnlyPerson toAdd) throws DuplicatePersonException {
4848
/**
4949
* Replaces the person {@code target} in the list with {@code editedPerson}.
5050
*
51-
* @throws DuplicatePersonException if updating the person's details causes the person to be equivalent to
52-
* another existing person in the list.
51+
* @throws DuplicatePersonException if the replacement is equivalent to another existing person in the list.
5352
* @throws PersonNotFoundException if {@code target} could not be found in the list.
5453
*/
55-
public void updatePerson(ReadOnlyPerson target, ReadOnlyPerson editedPerson)
54+
public void setPerson(ReadOnlyPerson target, ReadOnlyPerson editedPerson)
5655
throws DuplicatePersonException, PersonNotFoundException {
5756
requireNonNull(editedPerson);
5857

@@ -61,12 +60,11 @@ public void updatePerson(ReadOnlyPerson target, ReadOnlyPerson editedPerson)
6160
throw new PersonNotFoundException();
6261
}
6362

64-
Person personToUpdate = internalList.get(index);
65-
if (!personToUpdate.equals(editedPerson) && internalList.contains(editedPerson)) {
63+
if (!target.equals(editedPerson) && internalList.contains(editedPerson)) {
6664
throw new DuplicatePersonException();
6765
}
6866

69-
personToUpdate.resetData(editedPerson);
67+
internalList.set(index, new Person(editedPerson));
7068
}
7169

7270
/**

0 commit comments

Comments
 (0)