From d25cd29940d4d069252b37e87ea567c68abb68d2 Mon Sep 17 00:00:00 2001 From: "DESKTOP-B3317MO\\Tan Jiaqing" Date: Thu, 13 Sep 2018 13:48:58 +0800 Subject: [PATCH] Add case-insensitive feature to find function --- docs/UserGuide.adoc | 2 +- .../addressbook/commands/FindCommand.java | 21 ++++++++++++++++--- test/expected.txt | 4 ++-- test/input.txt | 2 +- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 4abb17e3e..c62533072 100644 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -79,7 +79,7 @@ Format: `find KEYWORD [MORE_KEYWORDS]` [NOTE] ==== -The search is case sensitive, the order of the keywords does not matter, only the name is searched, +The search is case insensitive, the order of the keywords does not matter, only the name is searched, and persons matching at least one keyword will be returned (i.e. `OR` search). ==== diff --git a/src/seedu/addressbook/commands/FindCommand.java b/src/seedu/addressbook/commands/FindCommand.java index 7fe2c52c3..adee14d2d 100644 --- a/src/seedu/addressbook/commands/FindCommand.java +++ b/src/seedu/addressbook/commands/FindCommand.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Set; @@ -10,14 +11,14 @@ /** * 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. */ public class FindCommand extends Command { public static final String COMMAND_WORD = "find"; public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names contain any of " - + "the specified keywords (case-sensitive) and displays them as a list with index numbers.\n" + + "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n" + "Parameters: KEYWORD [MORE_KEYWORDS]...\n" + "Example: " + COMMAND_WORD + " alice bob charlie"; @@ -49,7 +50,9 @@ public CommandResult execute() { private List getPersonsWithNameContainingAnyKeyword(Set keywords) { final List matchedPersons = new ArrayList<>(); for (ReadOnlyPerson person : addressBook.getAllPersons()) { - final Set wordsInName = new HashSet<>(person.getName().getWordsInName()); + Set wordsInName = new HashSet<>(person.getName().getWordsInName()); + keywords = convertLowercase(keywords); + wordsInName = convertLowercase(wordsInName); if (!Collections.disjoint(wordsInName, keywords)) { matchedPersons.add(person); } @@ -57,4 +60,16 @@ private List getPersonsWithNameContainingAnyKeyword(Set return matchedPersons; } + private Set convertLowercase(Set keywords) { + Set lowercaseKeywords = new HashSet<>(); + + Iterator iterator = keywords.iterator(); + + while(iterator.hasNext()){ + String word = iterator.next(); + lowercaseKeywords.add(word.toLowerCase()); + } + + return lowercaseKeywords; + } } diff --git a/test/expected.txt b/test/expected.txt index 56fe5fcac..1b3d01856 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -14,7 +14,7 @@ || Example: delete 1 || Clears address book permanently. || Example: clear -|| find: Finds all persons whose names contain any of the specified keywords (case-sensitive) and displays them as a list with index numbers. +|| find: Finds all persons whose names contain any of the specified keywords (case-insensitive) and displays them as a list with index numbers. || Parameters: KEYWORD [MORE_KEYWORDS]... || Example: find alice bob charlie || list: Displays all persons in the address book as a list with index numbers. @@ -200,7 +200,7 @@ || =================================================== || Enter command: || [Command entered: find] || Invalid command format! -|| find: Finds all persons whose names contain any of the specified keywords (case-sensitive) and displays them as a list with index numbers. +|| find: Finds all persons whose names contain any of the specified keywords (case-insensitive) and displays them as a list with index numbers. || Parameters: KEYWORD [MORE_KEYWORDS]... || Example: find alice bob charlie || =================================================== diff --git a/test/input.txt b/test/input.txt index eb8df81f8..f63c27b3f 100644 --- a/test/input.txt +++ b/test/input.txt @@ -96,7 +96,7 @@ 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