From 66a3a2bf6280954265feb146b90ff5aedb3891b5 Mon Sep 17 00:00:00 2001 From: anhhuynh Date: Wed, 7 Feb 2018 02:27:56 +0800 Subject: [PATCH 1/2] modify find function --- src/seedu/addressbook/AddressBook.java | 28 +++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 5a158b67..ccd14036 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -14,14 +14,7 @@ import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Optional; -import java.util.Scanner; -import java.util.Set; +import java.util.*; /* * NOTE : ============================================================= @@ -486,13 +479,30 @@ private static ArrayList getPersonsWithNameContainingAnyKeyword(Collec final ArrayList matchedPersons = new ArrayList<>(); for (String[] person : getAllPersonsInAddressBook()) { final Set wordsInName = new HashSet<>(splitByWhitespace(getNameFromPerson(person))); - if (!Collections.disjoint(wordsInName, keywords)) { + if (hasOverlap(wordsInName, keywords)) { matchedPersons.add(person); } } return matchedPersons; } + /** + * Check if 2 collections of string have overlap + * + * @param first for the first collection + * @param second for the second collection + * @return true if there is an overlap + */ + private static boolean hasOverlap(Collection first, Collection second) { + for (Iterator iteratorFirst = first.iterator(); iteratorFirst.hasNext();) + for (Iterator iteratorSecond = second.iterator(); iteratorSecond.hasNext();) + if (iteratorFirst.next().toLowerCase() + .equals(iteratorSecond.next().toLowerCase())) { + return true; + } + return false; + } + /** * Deletes person identified using last displayed index. * From 6260368a9d79b780532d104e26d769a3c70bcae6 Mon Sep 17 00:00:00 2001 From: anhhuynh Date: Wed, 7 Feb 2018 09:51:12 +0800 Subject: [PATCH 2/2] add test --- src/seedu/addressbook/AddressBook.java | 14 +++++++++----- test/expected.txt | 4 ---- test/input.txt | 4 +--- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index ccd14036..8b99689e 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -437,7 +437,7 @@ private static String getMessageForSuccessfulAddPerson(String[] addedPerson) { /** * Finds and lists all persons in address book whose name contains any of the argument keywords. - * Keyword matching is case sensitive. + * Keyword matching is case insensitive. * * @param commandArgs full command args string from the user * @return feedback display message for the operation result @@ -494,12 +494,16 @@ private static ArrayList getPersonsWithNameContainingAnyKeyword(Collec * @return true if there is an overlap */ private static boolean hasOverlap(Collection first, Collection second) { - for (Iterator iteratorFirst = first.iterator(); iteratorFirst.hasNext();) - for (Iterator iteratorSecond = second.iterator(); iteratorSecond.hasNext();) - if (iteratorFirst.next().toLowerCase() - .equals(iteratorSecond.next().toLowerCase())) { + for (Iterator iteratorFirst = first.iterator(); iteratorFirst.hasNext();) { + String valueFirst = iteratorFirst.next().toLowerCase(); + + for (Iterator iteratorSecond = second.iterator(); iteratorSecond.hasNext(); ) { + String valueSecond = iteratorSecond.next().toLowerCase(); + if (valueFirst.equals(valueSecond)) { return true; } + } + } return false; } diff --git a/test/expected.txt b/test/expected.txt index f18922ac..265d0de9 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -184,10 +184,6 @@ || 0 persons found! || =================================================== || Enter command: || [Command entered: find betsy] -|| -|| 0 persons found! -|| =================================================== -|| Enter command: || [Command entered: find Betsy] || 1. Betsy Choo Phone Number: 222222 Email: benchoo@nus.edu.sg || || 1 persons found! diff --git a/test/input.txt b/test/input.txt index 0b99df54..25e4a299 100644 --- a/test/input.txt +++ b/test/input.txt @@ -51,11 +51,9 @@ find bet # does not match if none have keyword find 23912039120 - # matching should be case-sensitive + # matching should be case-insensitive find betsy - # find unique keyword - find Betsy # find multiple with same keyword find Dickson # find multiple with some keywords