Skip to content

Commit

Permalink
Merge 33fbf5d into 4398deb
Browse files Browse the repository at this point in the history
  • Loading branch information
bendymochi committed Mar 11, 2019
2 parents 4398deb + 33fbf5d commit bcd64b5
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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_DISPLAYED_OVERVIEW = "Equipments showed on map!";

public static final String MESSAGE_EQUIPMENT_LISTED_OVERVIEW = "%1$d equipment(s) listed!";
public static final String MESSAGE_EQUIPMENT_DISPLAYED_OVERVIEW = "Equipment(s) showed on map!";
}
44 changes: 44 additions & 0 deletions src/main/java/seedu/address/logic/commands/FilterCommand.java
Original file line number Diff line number Diff line change
@@ -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));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/seedu/address/logic/parser/FilterCommandParser.java
Original file line number Diff line number Diff line change
@@ -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<FilterCommand> {

/**
* 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)));
}

}
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/model/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ public int hashCode() {
public String toString() {
return '[' + tagName + ']';
}
/**
* Getter for tag name.
*/
public String getTagName() {
return tagName;
}

}
Original file line number Diff line number Diff line change
@@ -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<Equipment> {
private final List<String> keywords;

public TagsContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
}


@Override
public boolean test(Equipment equipment) {
Set<Tag> 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/systemtests/FindCommandSystemTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)}.<br>
Expand All @@ -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);
Expand Down

0 comments on commit bcd64b5

Please sign in to comment.