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

modify find function #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 24 additions & 10 deletions src/seedu/addressbook/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 : =============================================================
Expand Down Expand Up @@ -444,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
Expand Down Expand Up @@ -486,13 +479,34 @@ private static ArrayList<String[]> getPersonsWithNameContainingAnyKeyword(Collec
final ArrayList<String[]> matchedPersons = new ArrayList<>();
for (String[] person : getAllPersonsInAddressBook()) {
final Set<String> 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<String> first, Collection<String> second) {
for (Iterator<String> iteratorFirst = first.iterator(); iteratorFirst.hasNext();) {
String valueFirst = iteratorFirst.next().toLowerCase();

for (Iterator<String> iteratorSecond = second.iterator(); iteratorSecond.hasNext(); ) {
String valueSecond = iteratorSecond.next().toLowerCase();
if (valueFirst.equals(valueSecond)) {
return true;
}
}
}
return false;
}

/**
* Deletes person identified using last displayed index.
*
Expand Down
4 changes: 0 additions & 4 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]
||
|| 1 persons found!
Expand Down
4 changes: 1 addition & 3 deletions test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down