diff --git a/src/seedu/addressbook/data/person/Name.java b/src/seedu/addressbook/data/person/Name.java index 3cea2db6b..c91b946f4 100644 --- a/src/seedu/addressbook/data/person/Name.java +++ b/src/seedu/addressbook/data/person/Name.java @@ -13,7 +13,7 @@ public class Name { public static final String EXAMPLE = "John Doe"; public static final String MESSAGE_NAME_CONSTRAINTS = "Person names should be spaces or alphabetic characters"; - public static final String NAME_VALIDATION_REGEX = "[\\p{Alpha} ]+"; + public static final String NAME_VALIDATION_REGEX = "[\\p{Alpha}\\, ]+"; public final String fullName; @@ -60,5 +60,28 @@ public boolean equals(Object other) { public int hashCode() { return fullName.hashCode(); } + + public boolean isSimilar(Name other) { + if (other == null) { + return false; + } + String n1 = this.toString().toLowerCase(); + String n2 = other.toString().toLowerCase(); + + String[] arr1 = n1.split("[,\\s+]+"); + String[] arr2 = n2.split("[,\\s+]+"); + + for (String s1:arr1) { + for (String s2:arr2) { + if (s1.equals(s2)) { + return true; + } + } + } + + + + return false; + } } diff --git a/src/seedu/addressbook/data/person/NameTest.java b/src/seedu/addressbook/data/person/NameTest.java new file mode 100644 index 000000000..9eabda45a --- /dev/null +++ b/src/seedu/addressbook/data/person/NameTest.java @@ -0,0 +1,45 @@ +package seedu.addressbook.data.person; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import seedu.addressbook.data.exception.IllegalValueException; + +public class NameTest { + + private Name n1; + + @Before + public void setup() throws IllegalValueException { + n1 = new Name("John K Smith"); + } + + @Test + public void similarInput_returnsTrue() throws IllegalValueException { + final Name n2 = new Name("John"); + assertTrue(n1.isSimilar(n2)); + } + + @Test + public void nullInput_returnsFalse() { + final Name n2 = null; + assertFalse(n1.isSimilar(n2)); + } + + @Test + public void caseInsensitiveInput_returnsTrue() throws IllegalValueException { + final Name n2 = new Name("john"); + assertTrue(n1.isSimilar(n2)); + + } + + @Test + public void nameWithCommaInput_returnsTrue() throws IllegalValueException { + final Name n2 = new Name("Smith, John K"); + assertTrue(n1.isSimilar(n2)); + + } + +}