forked from nus-cs2103-AY2425S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
385 additions
and
68 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/seedu/address/logic/commands/FilterCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.TagContainsKeywordsPredicate; | ||
|
||
/** | ||
* Finds and lists all persons in address book whose tag contains any of the argument keywords. | ||
* Keyword matching is case-insensitive. | ||
*/ | ||
public class FilterCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "filter"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose tags contain any of " | ||
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n" | ||
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n" | ||
+ "Example: " + COMMAND_WORD + " florist"; | ||
|
||
private final TagContainsKeywordsPredicate predicate; | ||
|
||
public FilterCommand(TagContainsKeywordsPredicate predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
model.updateFilteredPersonListByTag(predicate); | ||
return new CommandResult( | ||
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof FilterCommand otherFindCommand)) { | ||
return false; | ||
} | ||
|
||
return predicate.equals(otherFindCommand.predicate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("predicate", predicate) | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/seedu/address/logic/parser/FilterCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import java.util.Arrays; | ||
|
||
import seedu.address.logic.commands.FilterCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.TagContainsKeywordsPredicate; | ||
|
||
|
||
/** | ||
* Parses input arguments and creates a new FindCommand object | ||
*/ | ||
public class FilterCommandParser implements Parser<FilterCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the FilterCommand | ||
* and returns a FilterCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public FilterCommand parse(String args) throws ParseException { | ||
String trimmedArgs = args.trim(); | ||
if (trimmedArgs.isEmpty()) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
String[] nameKeywords = trimmedArgs.split("\\s+"); | ||
|
||
return new FilterCommand(new TagContainsKeywordsPredicate(Arrays.asList(nameKeywords))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/seedu/address/model/person/TagContainsKeywordsPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package seedu.address.model.person; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.StringUtil; | ||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Tag } matches any of the keywords given. | ||
*/ | ||
public class TagContainsKeywordsPredicate implements Predicate<Tag> { | ||
private final List<String> keywords; | ||
|
||
public TagContainsKeywordsPredicate(List<String> keywords) { | ||
this.keywords = keywords; | ||
} | ||
|
||
@Override | ||
public boolean test(Tag tag) { | ||
return keywords.stream() | ||
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(tag.tagName, keyword)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof TagContainsKeywordsPredicate otherNameContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
return keywords.equals(otherNameContainsKeywordsPredicate.keywords); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("keywords", keywords).toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,36 +29,54 @@ public class CommandTestUtil { | |
public static final String VALID_NAME_AMY = "Amy Bee"; | ||
public static final String VALID_NAME_BOB = "Bob Choo"; | ||
public static final String VALID_NAME_CLIVE = "Clive Fairfield"; | ||
public static final String VALID_NAME_DOMINIC = "Dominic Cheung"; | ||
public static final String VALID_NAME_ERIC = "Eric Caroll"; | ||
public static final String VALID_PHONE_AMY = "11111111"; | ||
public static final String VALID_PHONE_BOB = "22222222"; | ||
public static final String VALID_PHONE_CLIVE = "33333333"; | ||
public static final String VALID_PHONE_DOMINIC = ""; | ||
public static final String VALID_PHONE_ERIC = "65555656"; | ||
public static final String VALID_EMAIL_AMY = "[email protected]"; | ||
public static final String VALID_EMAIL_BOB = "[email protected]"; | ||
public static final String VALID_EMAIL_CLIVE = "[email protected]"; | ||
public static final String VALID_EMAIL_DOMINIC = "[email protected]"; | ||
public static final String VALID_EMAIL_ERIC = ""; | ||
public static final String VALID_ADDRESS_AMY = "Block 312, Amy Street 1"; | ||
public static final String VALID_ADDRESS_BOB = "Block 123, Bobby Street 3"; | ||
public static final String VALID_ADDRESS_BLANK = ""; | ||
public static final String VALID_ADDRESS_DOMINIC = "Block 9192, Dominican Street 4"; | ||
public static final String VALID_ADDRESS_ERIC = "Avenue 4, 201/1112"; | ||
public static final String VALID_TAG_HUSBAND = "husband"; | ||
public static final String VALID_TAG_FRIEND = "friend"; | ||
public static final String VALID_TAG_NEIGHBOR = "neighbor"; | ||
|
||
public static final String NAME_DESC_AMY = " " + PREFIX_NAME + VALID_NAME_AMY; | ||
public static final String NAME_DESC_BOB = " " + PREFIX_NAME + VALID_NAME_BOB; | ||
public static final String NAME_DESC_CLIVE = " " + PREFIX_NAME + VALID_NAME_CLIVE; | ||
public static final String NAME_DESC_DOMINIC = " " + PREFIX_NAME + VALID_NAME_DOMINIC; | ||
public static final String NAME_DESC_ERIC = " " + PREFIX_NAME + VALID_NAME_ERIC; | ||
public static final String PHONE_DESC_AMY = " " + PREFIX_PHONE + VALID_PHONE_AMY; | ||
public static final String PHONE_DESC_BOB = " " + PREFIX_PHONE + VALID_PHONE_BOB; | ||
public static final String PHONE_DESC_CLIVE = " " + PREFIX_PHONE + VALID_PHONE_CLIVE; | ||
public static final String PHONE_DESC_DOMINIC = " " + PREFIX_PHONE + VALID_PHONE_DOMINIC; | ||
public static final String PHONE_DESC_ERIC = " " + PREFIX_PHONE + VALID_PHONE_ERIC; | ||
public static final String EMAIL_DESC_AMY = " " + PREFIX_EMAIL + VALID_EMAIL_AMY; | ||
public static final String EMAIL_DESC_BOB = " " + PREFIX_EMAIL + VALID_EMAIL_BOB; | ||
public static final String EMAIL_DESC_CLIVE = " " + PREFIX_EMAIL + VALID_EMAIL_CLIVE; | ||
public static final String EMAIL_DESC_DOMINIC = " " + PREFIX_EMAIL + VALID_EMAIL_DOMINIC; | ||
public static final String EMAIL_DESC_ERIC = " " + PREFIX_EMAIL + VALID_EMAIL_ERIC; | ||
public static final String ADDRESS_DESC_AMY = " " + PREFIX_ADDRESS + VALID_ADDRESS_AMY; | ||
public static final String ADDRESS_DESC_BOB = " " + PREFIX_ADDRESS + VALID_ADDRESS_BOB; | ||
public static final String ADDRESS_DESC_DOMINIC = " " + PREFIX_ADDRESS + VALID_ADDRESS_DOMINIC; | ||
public static final String ADDRESS_DESC_ERIC = " " + PREFIX_ADDRESS + VALID_ADDRESS_ERIC; | ||
public static final String TAG_DESC_FRIEND = " " + PREFIX_TAG + VALID_TAG_FRIEND; | ||
public static final String TAG_DESC_HUSBAND = " " + PREFIX_TAG + VALID_TAG_HUSBAND; | ||
public static final String TAG_DESC_NEIGHBOR = " " + PREFIX_TAG + VALID_TAG_NEIGHBOR; | ||
|
||
public static final String INVALID_NAME_DESC = " " + PREFIX_NAME + "James&"; // '&' not allowed in names | ||
public static final String INVALID_PHONE_DESC = " " + PREFIX_PHONE + "911a"; // 'a' not allowed in phones | ||
// phone numbers must be at least 2 digits | ||
public static final String INVALID_PHONE_DESC_TOO_SHORT = " " + PREFIX_PHONE + "91"; | ||
public static final String INVALID_EMAIL_DESC = " " + PREFIX_EMAIL + "bob!yahoo"; // missing '@' symbol | ||
public static final String INVALID_TAG_DESC = " " + PREFIX_TAG + "hubby*"; // '*' not allowed in tags | ||
public static final String BLANK_ADDRESS_DESC = " " + PREFIX_ADDRESS + VALID_ADDRESS_BLANK; | ||
|
@@ -132,5 +150,4 @@ public static void showPersonAtIndex(Model model, Index targetIndex) { | |
|
||
assertEquals(1, model.getFilteredPersonList().size()); | ||
} | ||
|
||
} |
Oops, something went wrong.