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.6][T16-2] Perry Wang Zhiming #191

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
10 changes: 9 additions & 1 deletion src/seedu/addressbook/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class CommandResult {

/** The feedback message to be shown to the user. Contains a description of the execution result */
public final String feedbackToUser;
private final String feedbackToUser;

/** The list of persons that was produced by the command */
private final List<? extends ReadOnlyPerson> relevantPersons;
Expand All @@ -26,6 +26,14 @@ public CommandResult(String feedbackToUser, List<? extends ReadOnlyPerson> relev
this.relevantPersons = relevantPersons;
}

/**
* Returns a string of the feedback message to be shown to the user.
* Contains a description of the execution result.
*/
public String getFeedbackToUser() {
return feedbackToUser;
}

/**
* Returns a list of persons relevant to the command command result, if any.
*/
Expand Down
60 changes: 60 additions & 0 deletions src/seedu/addressbook/commands/FindByTagCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package seedu.addressbook.commands;

import java.util.ArrayList;
import java.util.List;

import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.tag.Tag;

/**
* Finds and lists all persons in address book who contains the specified tag.
* Keyword matching is not case sensitive.
*/
public class FindByTagCommand extends Command {

public static final String COMMAND_WORD = "findbytag";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons who contain the "
+ "specified tag (non-case-sensitive) and displays them as a list with index numbers.\n"
+ "Parameters: TAG\n"
+ "Example: " + COMMAND_WORD + " friends";

private final String tag;

public FindByTagCommand(String tag) {
this.tag = tag.trim();
}

/**
* Returns the tag to find in this command.
*/
public String getTag() {
return tag;
}

@Override
public CommandResult execute() {
final List<ReadOnlyPerson> personsFound = getPersonsWithNameContainingTag(tag);
return new CommandResult(getMessageForPersonListShownSummary(personsFound), personsFound);
}

/**
* Retrieves all persons in the address book who contain the specified tag.
*
* @param tag for searching
* @return list of persons found
*/
private List<ReadOnlyPerson> getPersonsWithNameContainingTag(String tag) {
final List<ReadOnlyPerson> matchedPersons = new ArrayList<>();
for (ReadOnlyPerson person : addressBook.getAllPersons()) {
for (Tag t : person.getTags()) {
if (t.tagName.toLowerCase().equals(tag.toLowerCase())) {
matchedPersons.add(person);
break;
}
}
}
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 @@ -20,6 +20,7 @@ public CommandResult execute() {
+ "\n" + DeleteCommand.MESSAGE_USAGE
+ "\n" + ClearCommand.MESSAGE_USAGE
+ "\n" + FindCommand.MESSAGE_USAGE
+ "\n" + FindByTagCommand.MESSAGE_USAGE
+ "\n" + ListCommand.MESSAGE_USAGE
+ "\n" + ViewCommand.MESSAGE_USAGE
+ "\n" + ViewAllCommand.MESSAGE_USAGE
Expand Down
21 changes: 2 additions & 19 deletions src/seedu/addressbook/data/person/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@
* Represents a Person's address in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidAddress(String)}
*/
public class Address {
public class Address extends Contact {

public static final String EXAMPLE = "123, some street";
public static final String MESSAGE_ADDRESS_CONSTRAINTS = "Person addresses can be in any format";
public static final String ADDRESS_VALIDATION_REGEX = ".+";

public final String value;
private boolean isPrivate;

/**
* Validates given address.
*
* @throws IllegalValueException if given address string is invalid.
*/
public Address(String address, boolean isPrivate) throws IllegalValueException {
String trimmedAddress = address.trim();
this.isPrivate = isPrivate;
this.setPrivate(isPrivate);
if (!isValidAddress(trimmedAddress)) {
throw new IllegalValueException(MESSAGE_ADDRESS_CONSTRAINTS);
}
Expand All @@ -36,24 +33,10 @@ public static boolean isValidAddress(String test) {
return test.matches(ADDRESS_VALIDATION_REGEX);
}

@Override
public String toString() {
return value;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Address // instanceof handles nulls
&& this.value.equals(((Address) other).value)); // state check
}

@Override
public int hashCode() {

Choose a reason for hiding this comment

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

if equals() is overriden, hashCode() also has to be overridden. you may find out why.

return value.hashCode();
}

public boolean isPrivate() {
return isPrivate;
}
}
29 changes: 29 additions & 0 deletions src/seedu/addressbook/data/person/Contact.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.addressbook.data.person;

/**
* Represents a Person's contact
*/
public class Contact {
public String value;
private boolean isPrivate;

protected Contact() {}

public boolean isPrivate() {
return isPrivate;
}

public void setPrivate(boolean isPrivate) {
this.isPrivate = isPrivate;
}

@Override
public String toString() {
return value;
}

@Override
public int hashCode() {
return value.hashCode();
}
}
22 changes: 2 additions & 20 deletions src/seedu/addressbook/data/person/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@
* Represents a Person's email in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidEmail(String)}
*/
public class Email {
public class Email extends Contact {

public static final String EXAMPLE = "[email protected]";
public static final String MESSAGE_EMAIL_CONSTRAINTS =
"Person emails should be 2 alphanumeric/period strings separated by '@'";
public static final String EMAIL_VALIDATION_REGEX = "[\\w\\.]+@[\\w\\.]+";

public final String value;
private boolean isPrivate;

/**
* Validates given email.
*
* @throws IllegalValueException if given email address string is invalid.
*/
public Email(String email, boolean isPrivate) throws IllegalValueException {
this.isPrivate = isPrivate;
this.setPrivate(isPrivate);
String trimmedEmail = email.trim();
if (!isValidEmail(trimmedEmail)) {
throw new IllegalValueException(MESSAGE_EMAIL_CONSTRAINTS);
Expand All @@ -37,25 +34,10 @@ public static boolean isValidEmail(String test) {
return test.matches(EMAIL_VALIDATION_REGEX);
}

@Override
public String toString() {
return value;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Email // instanceof handles nulls
&& this.value.equals(((Email) other).value)); // state check
}

@Override
public int hashCode() {
return value.hashCode();
}


public boolean isPrivate() {
return isPrivate;
}
}
21 changes: 2 additions & 19 deletions src/seedu/addressbook/data/person/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@
* Represents a Person's phone number in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidPhone(String)}
*/
public class Phone {
public class Phone extends Contact {

public static final String EXAMPLE = "123456789";
public static final String MESSAGE_PHONE_CONSTRAINTS = "Person phone numbers should only contain numbers";
public static final String PHONE_VALIDATION_REGEX = "\\d+";

public final String value;
private boolean isPrivate;

/**
* Validates given phone number.
*
* @throws IllegalValueException if given phone string is invalid.
*/
public Phone(String phone, boolean isPrivate) throws IllegalValueException {
this.isPrivate = isPrivate;
this.setPrivate(isPrivate);
String trimmedPhone = phone.trim();
if (!isValidPhone(trimmedPhone)) {
throw new IllegalValueException(MESSAGE_PHONE_CONSTRAINTS);
Expand All @@ -36,24 +33,10 @@ public static boolean isValidPhone(String test) {
return test.matches(PHONE_VALIDATION_REGEX);
}

@Override
public String toString() {
return value;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Phone // instanceof handles nulls
&& this.value.equals(((Phone) other).value)); // state check
}

@Override
public int hashCode() {
return value.hashCode();
}

public boolean isPrivate() {
return isPrivate;
}
}
21 changes: 21 additions & 0 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import seedu.addressbook.commands.Command;
import seedu.addressbook.commands.DeleteCommand;
import seedu.addressbook.commands.ExitCommand;
import seedu.addressbook.commands.FindByTagCommand;
import seedu.addressbook.commands.FindCommand;
import seedu.addressbook.commands.HelpCommand;
import seedu.addressbook.commands.IncorrectCommand;
Expand All @@ -41,6 +42,8 @@ public class Parser {
+ " (?<isAddressPrivate>p?)a/(?<address>[^/]+)"
+ "(?<tagArguments>(?: t/[^/]+)*)"); // variable number of tags

public static final Pattern TAG_ARGS_FORMAT =
Pattern.compile("(?<tag>^\\w+$)"); // only one word allowed

/**
* Signals that the user input could not be parsed.
Expand Down Expand Up @@ -99,6 +102,9 @@ public Command parseCommand(String userInput) {
case ExitCommand.COMMAND_WORD:
return new ExitCommand();

case FindByTagCommand.COMMAND_WORD:
return prepareFindByTag(arguments);

case HelpCommand.COMMAND_WORD: // Fallthrough
default:
return new HelpCommand();
Expand Down Expand Up @@ -250,5 +256,20 @@ private Command prepareFind(String args) {
return new FindCommand(keywordSet);
}

/**
* Parses arguments in the context of findByTag command
*
* @param args full command args string
* @return the prepared command
*/
private Command prepareFindByTag(String args) {
final Matcher matcher = TAG_ARGS_FORMAT.matcher(args.trim());
if (!matcher.matches()) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
FindByTagCommand.MESSAGE_USAGE));
}

return new FindByTagCommand(matcher.group("tag"));
}

}
2 changes: 1 addition & 1 deletion src/seedu/addressbook/ui/TextUi.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void showResultToUser(CommandResult result) {
if (resultPersons.isPresent()) {
showPersonListView(resultPersons.get());
}
showToUser(result.feedbackToUser, DIVIDER);
showToUser(result.getFeedbackToUser(), DIVIDER);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
|| find: Finds all persons whose names contain any of the specified keywords (case-sensitive) and displays them as a list with index numbers.
|| Parameters: KEYWORD [MORE_KEYWORDS]...
|| Example: find alice bob charlie
|| findbytag: Finds all persons who contain the specified tag (non-case-sensitive) and displays them as a list with index numbers.
|| Parameters: TAG
|| Example: findbytag friends
|| list: Displays all persons in the address book as a list with index numbers.
|| Example: list
|| view: Views the non-private details of the person identified by the index number in the last shown person listing.
Expand Down Expand Up @@ -233,6 +236,34 @@
||
|| 2 persons listed!
|| ===================================================
|| Enter command: || [Command entered: findbytag]
|| Invalid command format!
|| findbytag: Finds all persons who contain the specified tag (non-case-sensitive) and displays them as a list with index numbers.
|| Parameters: TAG
|| Example: findbytag friends
|| ===================================================
|| Enter command: || [Command entered: findbytag friends]
|| 1. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends]
|| 2. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends]
||
|| 2 persons listed!
|| ===================================================
|| Enter command: || [Command entered: findbytag FRIENDS]
|| 1. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends]
|| 2. Dickson Ee Phone: 444444 Address: 444, delta street Tags: [friends]
||
|| 2 persons listed!
|| ===================================================
|| Enter command: || [Command entered: findbytag nobodyhasthistag]
||
|| 0 persons listed!
|| ===================================================
|| Enter command: || [Command entered: find Charlie Betsy]
|| 1. Betsy Choo Tags: [secretive]
|| 2. Charlie Dickson Email: [email protected] Address: 333, gamma street Tags: [school][friends]
||
|| 2 persons listed!
|| ===================================================
|| Enter command: || [Command entered: delete]
|| Invalid command format!
|| delete: Deletes the person identified by the index number used in the last person listing.
Expand Down
Loading