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

[W7][T09-2]Jin Shibo #36

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
18 changes: 18 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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. +
Expand Down
35 changes: 35 additions & 0 deletions src/seedu/addressbook/commands/FindpCommand.java
Original file line number Diff line number Diff line change
@@ -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<ReadOnlyPerson> personsFound = getPersonsWithPhoneNumber(phone_number);
return new CommandResult(getMessageForPersonListShownSummary(personsFound), personsFound);
}
private List<ReadOnlyPerson> getPersonsWithPhoneNumber(String phone_number) {
final List<ReadOnlyPerson> 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;
}

}
1 change: 1 addition & 0 deletions src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}

}