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

[W5.7][F11-2]Tan Jiaqing #442

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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).
====

Expand Down
21 changes: 18 additions & 3 deletions src/seedu/addressbook/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import seedu.addressbook.data.person.ReadOnlyPerson;

/**
* 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";

Expand Down Expand Up @@ -49,12 +50,26 @@ public CommandResult execute() {
private List<ReadOnlyPerson> getPersonsWithNameContainingAnyKeyword(Set<String> keywords) {
final List<ReadOnlyPerson> matchedPersons = new ArrayList<>();
for (ReadOnlyPerson person : addressBook.getAllPersons()) {
final Set<String> wordsInName = new HashSet<>(person.getName().getWordsInName());
Set<String> wordsInName = new HashSet<>(person.getName().getWordsInName());
keywords = convertLowercase(keywords);
wordsInName = convertLowercase(wordsInName);
if (!Collections.disjoint(wordsInName, keywords)) {
matchedPersons.add(person);
}
}
return matchedPersons;
}

private Set<String> convertLowercase(Set<String> keywords) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to follow naming convention, i.e. convertToLowercase

Set<String> lowercaseKeywords = new HashSet<>();

Iterator<String> iterator = keywords.iterator();

while(iterator.hasNext()){
String word = iterator.next();
lowercaseKeywords.add(word.toLowerCase());
}

return lowercaseKeywords;
}
}
4 changes: 2 additions & 2 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
|| ===================================================
Expand Down
2 changes: 1 addition & 1 deletion test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down