From 741466dbfbb07016631d8351eca04b2469737e1d Mon Sep 17 00:00:00 2001 From: Zhang Jiayu Date: Tue, 12 Feb 2019 19:02:28 +0800 Subject: [PATCH 1/2] Add FindPhoneNumberCommand --- .../META-INF/addressbook-level2.kotlin_module | Bin 0 -> 16 bytes .../commands/FindPhoneNumberCommand.java | 60 ++++++++++++++++++ .../addressbook/commands/HelpCommand.java | 1 + src/seedu/addressbook/data/person/Phone.java | 7 ++ src/seedu/addressbook/parser/Parser.java | 28 ++++---- 5 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 out/production/addressbook-level2/META-INF/addressbook-level2.kotlin_module create mode 100644 src/seedu/addressbook/commands/FindPhoneNumberCommand.java diff --git a/out/production/addressbook-level2/META-INF/addressbook-level2.kotlin_module b/out/production/addressbook-level2/META-INF/addressbook-level2.kotlin_module new file mode 100644 index 0000000000000000000000000000000000000000..8fb60192d378759239a3ecbf60eac8c8de446e9c GIT binary patch literal 16 RcmZQzU|?ooU|@t|UH|}6022TJ literal 0 HcmV?d00001 diff --git a/src/seedu/addressbook/commands/FindPhoneNumberCommand.java b/src/seedu/addressbook/commands/FindPhoneNumberCommand.java new file mode 100644 index 000000000..8ffa642d6 --- /dev/null +++ b/src/seedu/addressbook/commands/FindPhoneNumberCommand.java @@ -0,0 +1,60 @@ +package seedu.addressbook.commands; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import seedu.addressbook.data.person.ReadOnlyPerson; + +/** + * Finds and lists all persons in address book whose telephone number is the argument keywords. + * Keyword matching is case sensitive. + */ +public class FindPhoneNumberCommand extends Command { + + public static final String COMMAND_WORD = "findPhoneNumber"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds the person whose telephone number is " + + "the specified keywords (case-sensitive) and displays the person's information.\n" + + "Parameters: KEYWORD [MORE_KEYWORDS]...\n" + + "Example: " + COMMAND_WORD + " 12345678"; + + private final Set keywords; + + public FindPhoneNumberCommand(Set keywords) { + this.keywords = keywords; + } + + /** + * Returns a copy of keywords in this command. + */ + public Set getKeywords() { + return new HashSet<>(keywords); + } + + @Override + public CommandResult execute() { + final List numberFound = getPersonsWithNameContainingAnyKeyword(keywords); + return new CommandResult(getMessageForPersonListShownSummary(numberFound), numberFound); + } + + /** + * Retrieves all persons in the address book whose names contain some of the specified keywords. + * + * @param keywords for searching + * @return list of persons found + */ + private List getPersonsWithNameContainingAnyKeyword(Set keywords) { + final List matchedPersons = new ArrayList<>(); + for (ReadOnlyPerson person : addressBook.getAllPersons()) { + final Set wordsInName = new HashSet<>(person.getPhone().getWordsInPhone()); + if (!Collections.disjoint(wordsInName, keywords)) { + matchedPersons.add(person); + } + } + return matchedPersons; + } + +} diff --git a/src/seedu/addressbook/commands/HelpCommand.java b/src/seedu/addressbook/commands/HelpCommand.java index 9be217d89..0a2496d10 100644 --- a/src/seedu/addressbook/commands/HelpCommand.java +++ b/src/seedu/addressbook/commands/HelpCommand.java @@ -23,6 +23,7 @@ public CommandResult execute() { + "\n" + ViewAllCommand.MESSAGE_USAGE + "\n" + HelpCommand.MESSAGE_USAGE + "\n" + ExitCommand.MESSAGE_USAGE + + "\n" + FindPhoneNumberCommand.MESSAGE_USAGE ); } } diff --git a/src/seedu/addressbook/data/person/Phone.java b/src/seedu/addressbook/data/person/Phone.java index 431a7a882..ea7b364a6 100644 --- a/src/seedu/addressbook/data/person/Phone.java +++ b/src/seedu/addressbook/data/person/Phone.java @@ -2,6 +2,9 @@ import seedu.addressbook.data.exception.IllegalValueException; +import java.util.Arrays; +import java.util.List; + /** * Represents a Person's phone number in the address book. * Guarantees: immutable; is valid as declared in {@link #isValidPhone(String)} @@ -36,6 +39,10 @@ public static boolean isValidPhone(String test) { return test.matches(PHONE_VALIDATION_REGEX); } + public List getWordsInPhone() { + return Arrays.asList(value.split("\\s+")); + } + @Override public String toString() { return value; diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index abddb3f45..69de7f9a3 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -11,17 +11,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import seedu.addressbook.commands.AddCommand; -import seedu.addressbook.commands.ClearCommand; -import seedu.addressbook.commands.Command; -import seedu.addressbook.commands.DeleteCommand; -import seedu.addressbook.commands.ExitCommand; -import seedu.addressbook.commands.FindCommand; -import seedu.addressbook.commands.HelpCommand; -import seedu.addressbook.commands.IncorrectCommand; -import seedu.addressbook.commands.ListCommand; -import seedu.addressbook.commands.ViewAllCommand; -import seedu.addressbook.commands.ViewCommand; +import seedu.addressbook.commands.*; import seedu.addressbook.data.exception.IllegalValueException; /** @@ -88,6 +78,9 @@ public Command parseCommand(String userInput) { case ListCommand.COMMAND_WORD: return new ListCommand(); + case FindPhoneNumberCommand.COMMAND_WORD: + return prepareFindPhoneNumber(arguments); + case ViewCommand.COMMAND_WORD: return prepareView(arguments); @@ -248,5 +241,18 @@ private Command prepareFind(String args) { return new FindCommand(keywordSet); } + private Command prepareFindPhoneNumber(String args) { + final Matcher matcher = KEYWORDS_ARGS_FORMAT.matcher(args.trim()); + if (!matcher.matches()) { + return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, + FindCommand.MESSAGE_USAGE)); + } + + // keywords delimited by whitespace + final String[] keywords = matcher.group("keywords").split("\\s+"); + final Set keywordSet = new HashSet<>(Arrays.asList(keywords)); + return new FindPhoneNumberCommand(keywordSet); + } + } From c533dfe5b225aeb8cd3fda47e7e2bf49fe3de115 Mon Sep 17 00:00:00 2001 From: Zhang Jiayu Date: Tue, 12 Feb 2019 19:10:10 +0800 Subject: [PATCH 2/2] Update the User Guide. --- docs/UserGuide.adoc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 4abb17e3e..844674742 100644 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -183,3 +183,15 @@ The file name must end in `.txt` for it to be acceptable to the program. When running the program inside IntelliJ, you can set command line parameters before running the program. ==== + +== Finding a persons by their telephone number: `findPhoneNumber` + +Finds persons whose telephone number is the given keywords. + +Format: `findPhoneNumber KEYWORD` + + +Examples: + +* `find 12345678` + +Returns the person's full information. +