Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement duplicate email checking feature #53

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks for both the name and the email

}

/**
* Returns true if both persons have the same identity and data fields.
* This defines a stronger notion of equality between two persons.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class UniquePersonList implements Iterable<Person> {
*/
public boolean contains(Person toCheck) {
requireNonNull(toCheck);
return internalList.stream().anyMatch(toCheck::isSamePerson);
return internalList.stream().anyMatch(toCheck::hasDuplicateInfo);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should check if the list has any Person with the same email and name

}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/test/java/seedu/address/logic/commands/EditCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));

Expand Down
26 changes: 26 additions & 0 deletions src/test/java/seedu/address/model/person/PersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down