diff --git a/src/main/java/seedu/address/model/person/Person.java b/src/main/java/seedu/address/model/person/Person.java index abe8c46b535..1c3514c9b0b 100644 --- a/src/main/java/seedu/address/model/person/Person.java +++ b/src/main/java/seedu/address/model/person/Person.java @@ -74,6 +74,22 @@ public boolean isSamePerson(Person otherPerson) { && otherPerson.getName().equals(getName()); } + /** + * Returns true if both person have the same email address. + * This is to check for duplicates in emails in the contact list. + */ + public boolean hasSameEmail(Person otherPerson) { + return otherPerson != null && otherPerson.getEmail().equals(this.getEmail()); + } + + /** + * Returns true if two contacts are considered as duplicates. + * This is to avoid adding duplicate contact in the contact list. + */ + public boolean hasDuplicateInfo(Person otherPerson) { + return this.isSamePerson(otherPerson) || this.hasSameEmail(otherPerson); + } + /** * Returns true if both persons have the same identity and data fields. * This defines a stronger notion of equality between two persons. diff --git a/src/main/java/seedu/address/model/person/UniquePersonList.java b/src/main/java/seedu/address/model/person/UniquePersonList.java index cc0a68d79f9..7b654442d32 100644 --- a/src/main/java/seedu/address/model/person/UniquePersonList.java +++ b/src/main/java/seedu/address/model/person/UniquePersonList.java @@ -33,7 +33,7 @@ public class UniquePersonList implements Iterable { */ public boolean contains(Person toCheck) { requireNonNull(toCheck); - return internalList.stream().anyMatch(toCheck::isSamePerson); + return internalList.stream().anyMatch(toCheck::hasDuplicateInfo); } /** diff --git a/src/test/java/seedu/address/logic/commands/EditCommandTest.java b/src/test/java/seedu/address/logic/commands/EditCommandTest.java index 469dd97daa7..5cb357e9b39 100644 --- a/src/test/java/seedu/address/logic/commands/EditCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/EditCommandTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.DESC_AMY; import static seedu.address.logic.commands.CommandTestUtil.DESC_BOB; +import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND; @@ -55,11 +56,11 @@ public void execute_someFieldsSpecifiedUnfilteredList_success() { Person lastPerson = model.getFilteredPersonList().get(indexLastPerson.getZeroBased()); PersonBuilder personInList = new PersonBuilder(lastPerson); - Person editedPerson = personInList.withName(VALID_NAME_BOB).withPhone(VALID_PHONE_BOB) - .withTags(VALID_TAG_HUSBAND).build(); + Person editedPerson = personInList.withName(VALID_NAME_BOB).withEmail(VALID_EMAIL_BOB) + .withPhone(VALID_PHONE_BOB).withTags(VALID_TAG_HUSBAND).build(); EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB) - .withPhone(VALID_PHONE_BOB).withTags(VALID_TAG_HUSBAND).build(); + .withPhone(VALID_PHONE_BOB).withTags(VALID_TAG_HUSBAND).withEmail(VALID_EMAIL_BOB).build(); EditCommand editCommand = new EditCommand(indexLastPerson, descriptor); String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); @@ -87,9 +88,11 @@ public void execute_filteredList_success() { showPersonAtIndex(model, INDEX_FIRST_PERSON); Person personInFilteredList = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); - Person editedPerson = new PersonBuilder(personInFilteredList).withName(VALID_NAME_BOB).build(); + + Person editedPerson = new PersonBuilder(personInFilteredList) + .withName(VALID_NAME_BOB).withEmail(VALID_EMAIL_BOB).build(); EditCommand editCommand = new EditCommand(INDEX_FIRST_PERSON, - new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).build()); + new EditPersonDescriptorBuilder().withName(VALID_NAME_BOB).withEmail(VALID_EMAIL_BOB).build()); String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)); diff --git a/src/test/java/seedu/address/model/person/PersonTest.java b/src/test/java/seedu/address/model/person/PersonTest.java index 31a10d156c9..65719b8d66b 100644 --- a/src/test/java/seedu/address/model/person/PersonTest.java +++ b/src/test/java/seedu/address/model/person/PersonTest.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.VALID_ADDRESS_BOB; +import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_AMY; import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; @@ -51,6 +52,31 @@ public void isSamePerson() { assertFalse(BOB.isSamePerson(editedBob)); } + @Test + public void hasSameEmail() { + assertTrue(ALICE.hasSameEmail(ALICE)); + + Person editedAlice = new PersonBuilder(ALICE).withEmail(VALID_EMAIL_BOB).build(); + assertFalse(ALICE.hasSameEmail(editedAlice)); + + Person editedBob = new PersonBuilder(BOB).withEmail(VALID_EMAIL_AMY).build(); + Person aliceWithNewEmail = new PersonBuilder(ALICE).withEmail(VALID_EMAIL_AMY).build(); + assertTrue(aliceWithNewEmail.hasSameEmail(editedBob)); + + assertFalse(ALICE.hasSameEmail(null)); + } + + @Test + public void hasDuplicateInfo() { + assertTrue(ALICE.hasDuplicateInfo(ALICE)); + assertFalse(ALICE.hasDuplicateInfo(null)); + + Person editedBob = new PersonBuilder(BOB).withEmail(VALID_EMAIL_AMY).build(); + Person aliceWithNewEmail = new PersonBuilder(ALICE).withEmail(VALID_EMAIL_AMY).build(); + assertTrue(editedBob.hasDuplicateInfo(aliceWithNewEmail)); + assertFalse(editedBob.hasDuplicateInfo(ALICE)); + } + @Test public void equals() { // same values -> returns true