From 85dda6f792238d4ae038d9c0edd36ac4024bae87 Mon Sep 17 00:00:00 2001 From: bendymochi Date: Mon, 11 Mar 2019 16:46:26 +0800 Subject: [PATCH 1/2] Refactor Persons to Equipment in Message --- src/main/java/seedu/address/commons/core/Messages.java | 2 +- src/main/java/seedu/address/logic/commands/FindCommand.java | 2 +- .../java/seedu/address/logic/commands/FindCommandTest.java | 6 +++--- src/test/java/systemtests/FindCommandSystemTest.java | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/seedu/address/commons/core/Messages.java b/src/main/java/seedu/address/commons/core/Messages.java index a4641cea900c..2e7a8641f2cd 100644 --- a/src/main/java/seedu/address/commons/core/Messages.java +++ b/src/main/java/seedu/address/commons/core/Messages.java @@ -8,6 +8,6 @@ public class Messages { public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command"; public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s"; public static final String MESSAGE_INVALID_EQUIPMENT_DISPLAYED_INDEX = "The equipment index provided is invalid"; - public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d equipments listed!"; + public static final String MESSAGE_EQUIPMENT_LISTED_OVERVIEW = "%1$d equipments listed!"; } diff --git a/src/main/java/seedu/address/logic/commands/FindCommand.java b/src/main/java/seedu/address/logic/commands/FindCommand.java index 99a25793a101..c83dcc4665db 100644 --- a/src/main/java/seedu/address/logic/commands/FindCommand.java +++ b/src/main/java/seedu/address/logic/commands/FindCommand.java @@ -31,7 +31,7 @@ public CommandResult execute(Model model, CommandHistory history) { requireNonNull(model); model.updateFilteredPersonList(predicate); return new CommandResult( - String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); + String.format(Messages.MESSAGE_EQUIPMENT_LISTED_OVERVIEW, model.getFilteredPersonList().size())); } @Override diff --git a/src/test/java/seedu/address/logic/commands/FindCommandTest.java b/src/test/java/seedu/address/logic/commands/FindCommandTest.java index c279a149f4a0..ee40daf6de00 100644 --- a/src/test/java/seedu/address/logic/commands/FindCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/FindCommandTest.java @@ -3,7 +3,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static seedu.address.commons.core.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW; +import static seedu.address.commons.core.Messages.MESSAGE_EQUIPMENT_LISTED_OVERVIEW; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; import static seedu.address.testutil.TypicalEquipments.CARL; import static seedu.address.testutil.TypicalEquipments.ELLE; @@ -58,7 +58,7 @@ public void equals() { @Test public void execute_zeroKeywords_noPersonFound() { - String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0); + String expectedMessage = String.format(MESSAGE_EQUIPMENT_LISTED_OVERVIEW, 0); NameContainsKeywordsPredicate predicate = preparePredicate(" "); FindCommand command = new FindCommand(predicate); expectedModel.updateFilteredPersonList(predicate); @@ -68,7 +68,7 @@ public void execute_zeroKeywords_noPersonFound() { @Test public void execute_multipleKeywords_multiplePersonsFound() { - String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3); + String expectedMessage = String.format(MESSAGE_EQUIPMENT_LISTED_OVERVIEW, 3); NameContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz"); FindCommand command = new FindCommand(predicate); expectedModel.updateFilteredPersonList(predicate); diff --git a/src/test/java/systemtests/FindCommandSystemTest.java b/src/test/java/systemtests/FindCommandSystemTest.java index 0809e104728d..4b31b51d274b 100644 --- a/src/test/java/systemtests/FindCommandSystemTest.java +++ b/src/test/java/systemtests/FindCommandSystemTest.java @@ -1,7 +1,7 @@ package systemtests; import static org.junit.Assert.assertFalse; -import static seedu.address.commons.core.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW; +import static seedu.address.commons.core.Messages.MESSAGE_EQUIPMENT_LISTED_OVERVIEW; import static seedu.address.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND; import static seedu.address.testutil.TypicalEquipments.BENSON; import static seedu.address.testutil.TypicalEquipments.CARL; @@ -158,7 +158,7 @@ public void find() { /** * Executes {@code command} and verifies that the command box displays an empty string, the result display - * box displays {@code Messages#MESSAGE_PERSONS_LISTED_OVERVIEW} with the number of people in the filtered list, + * box displays {@code Messages#MESSAGE_EQUIPMENT_LISTED_OVERVIEW} with the number of people in the filtered list, * and the model related components equal to {@code expectedModel}. * These verifications are done by * {@code EquipmentManagerSystemTest#assertApplicationDisplaysExpected(String, String, Model)}.
@@ -168,7 +168,7 @@ public void find() { */ private void assertCommandSuccess(String command, Model expectedModel) { String expectedResultMessage = String.format( - MESSAGE_PERSONS_LISTED_OVERVIEW, expectedModel.getFilteredPersonList().size()); + MESSAGE_EQUIPMENT_LISTED_OVERVIEW, expectedModel.getFilteredPersonList().size()); executeCommand(command); assertApplicationDisplaysExpected("", expectedResultMessage, expectedModel); From 824b0bd2f7fdd2e0e09360f96decea3dddf1a361 Mon Sep 17 00:00:00 2001 From: bendymochi Date: Mon, 11 Mar 2019 21:28:18 +0800 Subject: [PATCH 2/2] Add FilterCommand --- .../address/logic/commands/FilterCommand.java | 44 +++++++++++++++++++ .../logic/parser/FilterCommandParser.java | 33 ++++++++++++++ .../java/seedu/address/model/tag/Tag.java | 6 +++ .../tag/TagsContainsKeywordsPredicate.java | 39 ++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 src/main/java/seedu/address/logic/commands/FilterCommand.java create mode 100644 src/main/java/seedu/address/logic/parser/FilterCommandParser.java create mode 100644 src/main/java/seedu/address/model/tag/TagsContainsKeywordsPredicate.java diff --git a/src/main/java/seedu/address/logic/commands/FilterCommand.java b/src/main/java/seedu/address/logic/commands/FilterCommand.java new file mode 100644 index 000000000000..025d2d3d3374 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/FilterCommand.java @@ -0,0 +1,44 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; + +import seedu.address.commons.core.Messages; +import seedu.address.logic.CommandHistory; +import seedu.address.model.Model; +import seedu.address.model.tag.TagsContainsKeywordsPredicate; + +/** + * Filters and lists all equipments in Equipment Manager whose tags 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 + ": Filters all equipments whose tags contain any of " + + "the specified keywords (case-insensitive) and displays them as a list of index numbers.\n" + + "Parameters: KEYWORD [MORE KEYWORDS]...\n" + + "Example: " + COMMAND_WORD + " west urgent"; + + private final TagsContainsKeywordsPredicate predicate; + + public FilterCommand(TagsContainsKeywordsPredicate predicate) { + this.predicate = predicate; + } + + @Override + public CommandResult execute(Model model, CommandHistory history) { + requireNonNull(model); + model.updateFilteredPersonList(predicate); + return new CommandResult( + String.format(Messages.MESSAGE_EQUIPMENT_LISTED_OVERVIEW, + model.getFilteredPersonList().size())); + } + + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof FilterCommand // instance of handles nulls + && predicate.equals(((FilterCommand) other).predicate)); + + } +} diff --git a/src/main/java/seedu/address/logic/parser/FilterCommandParser.java b/src/main/java/seedu/address/logic/parser/FilterCommandParser.java new file mode 100644 index 000000000000..26f4a3004790 --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/FilterCommandParser.java @@ -0,0 +1,33 @@ +package seedu.address.logic.parser; + +import static seedu.address.commons.core.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.tag.TagsContainsKeywordsPredicate; + +/** + * Parses input arguments and creates a new FindCommand object + */ +public class FilterCommandParser implements Parser { + + /** + * Parses the given {@code String} of arguments in the context of the FilterCommand + * and returns an 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[] tagKeywords = trimmedArgs.split("\\s+"); + + return new FilterCommand(new TagsContainsKeywordsPredicate(Arrays.asList(tagKeywords))); + } + +} diff --git a/src/main/java/seedu/address/model/tag/Tag.java b/src/main/java/seedu/address/model/tag/Tag.java index b0ea7e7dad7f..af639ae4bb8c 100644 --- a/src/main/java/seedu/address/model/tag/Tag.java +++ b/src/main/java/seedu/address/model/tag/Tag.java @@ -50,5 +50,11 @@ public int hashCode() { public String toString() { return '[' + tagName + ']'; } + /** + * Getter for tag name. + */ + public String getTagName() { + return tagName; + } } diff --git a/src/main/java/seedu/address/model/tag/TagsContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/tag/TagsContainsKeywordsPredicate.java new file mode 100644 index 000000000000..688a3603c0d2 --- /dev/null +++ b/src/main/java/seedu/address/model/tag/TagsContainsKeywordsPredicate.java @@ -0,0 +1,39 @@ +package seedu.address.model.tag; + +import java.util.List; +import java.util.Set; +import java.util.function.Predicate; + +import seedu.address.commons.util.StringUtil; +import seedu.address.model.equipment.Equipment; + +/** + * Tests that a {@code Tag}'s {@code Name} matches any of the keywords given. + */ + +public class TagsContainsKeywordsPredicate implements Predicate { + private final List keywords; + + public TagsContainsKeywordsPredicate(List keywords) { + this.keywords = keywords; + } + + + @Override + public boolean test(Equipment equipment) { + Set tags = equipment.getTags(); + if (keywords.isEmpty()) { + return false; + } + return keywords.stream() + .anyMatch(keyword -> tags.stream().anyMatch(tag -> + StringUtil.containsWordIgnoreCase(tag.getTagName(), keyword))); + } + + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof TagsContainsKeywordsPredicate // instanceof handles nulls + && keywords.equals(((TagsContainsKeywordsPredicate) other).keywords)); // state check + } +}