diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 7e4858342..c1ce249c0 100644 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -73,6 +73,24 @@ Returns `John Doe` but not `john`. * `find Betsy Tim John` + Returns Any person having names `Betsy`, `Tim`, or `John`. +== Finding all persons with specified phone number: `findp` + +Find persons whose phone number matches the given phone number. + +Format: `findp PHONE_NUMBER` + +[NOTE] +==== +You can only type in one phone number each time. +==== + +Examples: + +* `findp 98765432` + +Returns `John Doe` + +* `findp 11111111` + +Returns `Britton` + == Deleting a person : `delete` Deletes the specified person from the address book. Irreversible. + diff --git a/src/seedu/addressbook/commands/FindpCommand.java b/src/seedu/addressbook/commands/FindpCommand.java new file mode 100644 index 000000000..c08899601 --- /dev/null +++ b/src/seedu/addressbook/commands/FindpCommand.java @@ -0,0 +1,35 @@ +package seedu.addressbook.commands; + +import seedu.addressbook.data.person.ReadOnlyPerson; +import java.lang.String; +import java.util.*; + +public class FindpCommand extends Command{ + public static final String COMMAND_WORD = "findp"; + public static final String MESSAGE_USAGE = COMMAND_WORD + ":\n" + "Finds all persons whose phone number matches" + + "the specified phone number and displays them as a list with index numbers.\n\t" + + "Parameters: PHONE NUMBER\n\t" + + "Example: " + COMMAND_WORD + " 12345678"; + + private final String phone_number; + + public FindpCommand(String phone_number) { + this.phone_number = phone_number; + } + @Override + public CommandResult execute() { + final List personsFound = getPersonsWithPhoneNumber(phone_number); + return new CommandResult(getMessageForPersonListShownSummary(personsFound), personsFound); + } + private List getPersonsWithPhoneNumber(String phone_number) { + final List matchedPersons = new ArrayList<>(); + for (ReadOnlyPerson person : addressBook.getAllPersons()) { + final String phone_number_of_person = person.getPhone().value; + if (phone_number_of_person.equals(phone_number)) { + matchedPersons.add(person); + } + } + return matchedPersons; + } + +} \ No newline at end of file diff --git a/src/seedu/addressbook/commands/HelpCommand.java b/src/seedu/addressbook/commands/HelpCommand.java index ef2ed7d0e..49d9bdf1d 100644 --- a/src/seedu/addressbook/commands/HelpCommand.java +++ b/src/seedu/addressbook/commands/HelpCommand.java @@ -15,6 +15,7 @@ public class HelpCommand extends Command { + "\n" + DeleteCommand.MESSAGE_USAGE + "\n" + ClearCommand.MESSAGE_USAGE + "\n" + FindCommand.MESSAGE_USAGE + + "\n" + FindpCommand.MESSAGE_USAGE + "\n" + ListCommand.MESSAGE_USAGE + "\n" + ViewCommand.MESSAGE_USAGE + "\n" + ViewAllCommand.MESSAGE_USAGE diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index 58f4f7e6c..9164c8077 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -69,6 +69,9 @@ public Command parseCommand(String userInput) { case FindCommand.COMMAND_WORD: return prepareFind(arguments); + case FindpCommand.COMMAND_WORD: + return prepareFindp(arguments); + case ListCommand.COMMAND_WORD: return new ListCommand(); @@ -226,5 +229,14 @@ private Command prepareFind(String args) { return new FindCommand(keywordSet); } + private Command prepareFindp(String args) { + final Matcher matcher = KEYWORDS_ARGS_FORMAT.matcher(args.trim()); + if (!matcher.matches()) { + return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, + FindpCommand.MESSAGE_USAGE)); + } + String phone_number = args.trim(); + return new FindpCommand(phone_number); + } } \ No newline at end of file