Skip to content

Commit

Permalink
Merge pull request #351 from Shruthiiii03/branch-Update-Person-Equality
Browse files Browse the repository at this point in the history
Update equality check for person
  • Loading branch information
gohqingkhang authored Nov 11, 2024
2 parents ad3c2da + aee7683 commit 227e7f7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
14 changes: 9 additions & 5 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,20 @@ public HashMap<AbsentDate, AbsentReason> getAttendances() {
}

/**
* Returns true if both persons have the same name.
* Returns true if both persons have the same register number and class, or same phone numbers, or same emails.
* This defines a weaker notion of equality between two persons.
*/
public boolean isSamePerson(Person otherPerson) {
if (otherPerson == this) {
return true;
if (otherPerson == null) {
return false;
}

return otherPerson != null
&& otherPerson.getName().equals(getName());
boolean isSameClass = (otherPerson.getStudentClass().equals(this.getStudentClass()));
boolean isSameRegNo = (otherPerson.getRegisterNumber().equals(this.getRegisterNumber()));
boolean isSamePhone = (otherPerson.getPhone().equals(this.getPhone()));
boolean isSameEmail = (otherPerson.getEmail().equals(this.getEmail()));

return (isSameClass && isSameRegNo) || isSamePhone || isSameEmail;
}

/**
Expand Down
37 changes: 27 additions & 10 deletions src/test/java/seedu/address/model/person/PersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
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_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_REGISTER_NUMBER_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_REGISTER_NUMBER_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_SEX_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_STUDENT_CLASS_BOB;
import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalPersons.ALICE;
import static seedu.address.testutil.TypicalPersons.AMY;
import static seedu.address.testutil.TypicalPersons.BOB;

import org.junit.jupiter.api.Test;
Expand All @@ -35,24 +39,37 @@ public void isSamePerson() {
// null -> returns false
assertFalse(ALICE.isSamePerson(null));

// same name, all other attributes different -> returns true
// same name, all other attributes different -> returns false
Person editedAlice = new PersonBuilder(ALICE).withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_BOB)
.withAddress(VALID_ADDRESS_BOB).withRegisterNumber(VALID_REGISTER_NUMBER_BOB).withSex(VALID_SEX_BOB)
.withStudentClass(VALID_STUDENT_CLASS_BOB).withTags(VALID_TAG_HUSBAND).build();
assertTrue(ALICE.isSamePerson(editedAlice));
assertFalse(ALICE.isSamePerson(editedAlice));

// different name, all other attributes same -> returns false
// same class and register number -> returns true
editedAlice = new PersonBuilder(ALICE).withName(VALID_NAME_BOB).build();
assertFalse(ALICE.isSamePerson(editedAlice));
assertTrue(ALICE.isSamePerson(editedAlice));

// name differs in case, all other attributes same -> returns false
// name differs in case, same class and register number -> returns true
Person editedBob = new PersonBuilder(BOB).withName(VALID_NAME_BOB.toLowerCase()).build();
assertFalse(BOB.isSamePerson(editedBob));
assertTrue(BOB.isSamePerson(editedBob));

// name has trailing spaces, all other attributes same -> returns false
String nameWithTrailingSpaces = VALID_NAME_BOB + " ";
editedBob = new PersonBuilder(BOB).withName(nameWithTrailingSpaces).build();
assertFalse(BOB.isSamePerson(editedBob));
// same phone numbers, other attributes different -> return true
Person diffPhoneBob = new PersonBuilder(BOB).withPhone(VALID_PHONE_AMY).withEmail(VALID_EMAIL_BOB)
.withAddress(VALID_ADDRESS_BOB).withRegisterNumber(VALID_REGISTER_NUMBER_BOB).withSex(VALID_SEX_BOB)
.withStudentClass(VALID_STUDENT_CLASS_BOB).withTags(VALID_TAG_HUSBAND).build();
assertTrue(AMY.isSamePerson(diffPhoneBob));

// same emails -> return true
Person diffEmailBob = new PersonBuilder(BOB).withPhone(VALID_PHONE_BOB).withEmail(VALID_EMAIL_AMY)
.withAddress(VALID_ADDRESS_BOB).withRegisterNumber(VALID_REGISTER_NUMBER_BOB).withSex(VALID_SEX_BOB)
.withStudentClass(VALID_STUDENT_CLASS_BOB).withTags(VALID_TAG_HUSBAND).build();
assertTrue(AMY.isSamePerson(diffEmailBob));

// diff register number and phone number and email -> return false
Person diffPerson = new PersonBuilder(BOB).withPhone(VALID_PHONE_AMY).withEmail(VALID_EMAIL_AMY)
.withAddress(VALID_ADDRESS_BOB).withRegisterNumber(VALID_REGISTER_NUMBER_AMY).withSex(VALID_SEX_BOB)
.withStudentClass(VALID_STUDENT_CLASS_BOB).withTags(VALID_TAG_HUSBAND).build();
assertFalse(BOB.isSamePerson(diffPerson));
}

@Test
Expand Down

0 comments on commit 227e7f7

Please sign in to comment.