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

[T4A7][F11-A2]Deng Yue #1075

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
25 changes: 25 additions & 0 deletions src/seedu/addressbook/data/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@ public static boolean isValidName(String test) {
public List<String> getWordsInName() {
return Arrays.asList(fullName.split("\\s+"));
}

/**
* Returns true of the other name is very similar to this name.
* Two names are considered similar if ...
*/
public boolean isSimilar(Name other) {
if(this.fullName.equals(other.fullName)) {
return true;
} else if (this.fullName.equalsIgnoreCase(other.fullName.toLowerCase())) {
return true;
} else if (containsSubstring(this.fullName, other.fullName)) {
return true;
}
return false;
}

public boolean containsSubstring(String s1, String s2) {
String s1LowerCase = s1.toLowerCase();
String s2LowerCase = s2.toLowerCase();
String[] otherArray = s2LowerCase.split(" ");
for(String s : otherArray) {
if(s1LowerCase.contains(s)) return true;
}
return false;
}

@Override
public String toString() {
Expand Down
44 changes: 44 additions & 0 deletions test/java/seedu/addressbook/data/person/NameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package seedu.addressbook.data.person;

import static org.junit.Assert.*;

import org.junit.Test;

import seedu.addressbook.data.exception.IllegalValueException;

public class NameTest {

@Test
public void isSimilar() {
try {
Name name = new Name("John K Smith");

//same string
assertTrue(name.isSimilar(new Name("John K Smith")));

//same letters with different cases;
assertTrue(name.isSimilar(new Name("JOHN K SMITH")));
assertTrue(name.isSimilar(new Name("JOHN K smith")));
assertTrue(name.isSimilar(new Name("JohN K Smith")));

//same first name or middle name or last name
assertTrue(name.isSimilar(new Name("John")));
assertTrue(name.isSimilar(new Name("K")));
assertTrue(name.isSimilar(new Name("Smith")));
assertTrue(name.isSimilar(new Name("John K")));

//same letters in first name or middle name with different cases
assertTrue(name.isSimilar(new Name("JOHN")));
assertTrue(name.isSimilar(new Name("k")));
assertTrue(name.isSimilar(new Name("SmITh")));
assertTrue(name.isSimilar(new Name("JoHn K")));

//not similar
assertFalse(name.isSimilar(new Name("johnn kk smithh")));
assertFalse(name.isSimilar(new Name("johnksmith")));
} catch (IllegalValueException e) {
e.printStackTrace();
}
}

}