diff --git a/src/main/java/seedu/address/logic/Messages.java b/src/main/java/seedu/address/logic/Messages.java index 7f9e17148e7..b611bd354a2 100644 --- a/src/main/java/seedu/address/logic/Messages.java +++ b/src/main/java/seedu/address/logic/Messages.java @@ -18,6 +18,11 @@ public class Messages { public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!"; public static final String MESSAGE_DUPLICATE_FIELDS = "Multiple values specified for the following single-valued field(s): "; + public static final String MESSAGE_EMPTY_DEPARTMENT_FILTER = "department name cannot be empty!"; + + public static final String MESSAGE_LIST_SUCCESS = "Listed all employees (%1$d)"; + + public static final String MESSAGE_FILTER_SUCCESS = "Listed filtered employees (%1$d)"; /** * Returns an error message indicating the duplicate prefixes. diff --git a/src/main/java/seedu/address/logic/commands/ListCommand.java b/src/main/java/seedu/address/logic/commands/ListCommand.java index 83ed3cf2caf..0df738df41f 100644 --- a/src/main/java/seedu/address/logic/commands/ListCommand.java +++ b/src/main/java/seedu/address/logic/commands/ListCommand.java @@ -3,7 +3,11 @@ import static java.util.Objects.requireNonNull; import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; +import java.util.function.Predicate; + +import seedu.address.logic.Messages; import seedu.address.model.Model; +import seedu.address.model.person.Person; /** * Lists all persons in the address book to the user. @@ -12,13 +16,32 @@ public class ListCommand extends Command { public static final String COMMAND_WORD = "list"; - public static final String MESSAGE_SUCCESS = "Listed all employees"; + public final String message; + + public final Predicate predicate; + /** + * Initializes ListCommand non-filtered list + */ + public ListCommand() { + this.predicate = PREDICATE_SHOW_ALL_PERSONS; + this.message = Messages.MESSAGE_LIST_SUCCESS; + } + + /** + * Initializes ListCommand for filtered List + * + * @param predicate predicate for the filtering + */ + public ListCommand(Predicate predicate) { + this.predicate = predicate; + this.message = Messages.MESSAGE_FILTER_SUCCESS; + } @Override public CommandResult execute(Model model) { requireNonNull(model); - model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); - return new CommandResult(MESSAGE_SUCCESS); + model.updateFilteredPersonList(predicate); + return new CommandResult(String.format(message, model.getFilteredPersonList().size())); } } diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index 3149ee07e0b..88cbea43998 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -69,7 +69,7 @@ public Command parseCommand(String userInput) throws ParseException { return new FindCommandParser().parse(arguments); case ListCommand.COMMAND_WORD: - return new ListCommand(); + return new ListCommandParser().parse(arguments); case ExitCommand.COMMAND_WORD: return new ExitCommand(); diff --git a/src/main/java/seedu/address/logic/parser/ListCommandParser.java b/src/main/java/seedu/address/logic/parser/ListCommandParser.java new file mode 100644 index 00000000000..45968f02c7d --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/ListCommandParser.java @@ -0,0 +1,39 @@ +package seedu.address.logic.parser; + +import static java.util.Objects.requireNonNull; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DEPARTMENT; + +import seedu.address.logic.Messages; +import seedu.address.logic.commands.ListCommand; +import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.person.Department; +import seedu.address.model.person.MatchingDepartmentPredicate; + + +/** + * Parses input arguments and creates a new ListCommand object + */ +public class ListCommandParser implements Parser { + + /** + * Parses {@code userInput} into a command and returns it. + * + * @param args arguments + * @throws ParseException if {@code userInput} does not conform the expected format + */ + @Override + public ListCommand parse(String args) throws ParseException { + requireNonNull(args); + ArgumentMultimap argMultimap = + ArgumentTokenizer.tokenize(args, PREFIX_DEPARTMENT); + if (argMultimap.getValue(PREFIX_DEPARTMENT).isEmpty()) { + return new ListCommand(); + } + try { + Department filteringDepartment = new Department(argMultimap.getValue(PREFIX_DEPARTMENT).get()); + return new ListCommand(new MatchingDepartmentPredicate(filteringDepartment)); + } catch (IllegalArgumentException e) { + throw new ParseException(Messages.MESSAGE_EMPTY_DEPARTMENT_FILTER); + } + } +} diff --git a/src/main/java/seedu/address/model/person/MatchingDepartmentPredicate.java b/src/main/java/seedu/address/model/person/MatchingDepartmentPredicate.java new file mode 100644 index 00000000000..e35bc014d69 --- /dev/null +++ b/src/main/java/seedu/address/model/person/MatchingDepartmentPredicate.java @@ -0,0 +1,26 @@ +package seedu.address.model.person; + +import java.util.function.Predicate; + +/** + * Tests that a {@code Person}'s {@code Department} matches the department name given. + */ +public class MatchingDepartmentPredicate implements Predicate { + + private final Department department; + + public MatchingDepartmentPredicate(Department department) { + this.department = department; + } + + /** + * Evaluates this predicate on the given argument. + * + * @param person the input argument + * @return {@code true} if the input argument matches the predicate, otherwise {@code false} + */ + @Override + public boolean test(Person person) { + return this.department.equals(person.getDepartment()); + } +} diff --git a/src/test/java/seedu/address/logic/LogicManagerTest.java b/src/test/java/seedu/address/logic/LogicManagerTest.java index 8db5f4b5abd..f2342255081 100644 --- a/src/test/java/seedu/address/logic/LogicManagerTest.java +++ b/src/test/java/seedu/address/logic/LogicManagerTest.java @@ -71,7 +71,8 @@ public void execute_commandExecutionError_throwsCommandException() { @Test public void execute_validCommand_success() throws Exception { String listCommand = ListCommand.COMMAND_WORD; - assertCommandSuccess(listCommand, ListCommand.MESSAGE_SUCCESS, model); + assertCommandSuccess(listCommand, String.format( + Messages.MESSAGE_LIST_SUCCESS, model.getFilteredPersonList().size()), model); } @Test diff --git a/src/test/java/seedu/address/logic/commands/ListCommandTest.java b/src/test/java/seedu/address/logic/commands/ListCommandTest.java index 435ff1f7275..65e3ecd8d7b 100644 --- a/src/test/java/seedu/address/logic/commands/ListCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ListCommandTest.java @@ -1,39 +1,53 @@ package seedu.address.logic.commands; +import static seedu.address.logic.commands.CommandTestUtil.VALID_DEPARTMENT; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; -import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex; -import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import java.util.function.Predicate; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import seedu.address.logic.Messages; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; +import seedu.address.model.person.Department; +import seedu.address.model.person.MatchingDepartmentPredicate; +import seedu.address.model.person.Person; + + /** * Contains integration tests (interaction with the Model) and unit tests for ListCommand. */ public class ListCommandTest { + private static final Predicate FILTER_TEST_PREDICATE = + new MatchingDepartmentPredicate(new Department(VALID_DEPARTMENT)); private Model model; private Model expectedModel; + private Model expectedFilteredModel; @BeforeEach public void setUp() { model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + expectedFilteredModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + expectedFilteredModel.updateFilteredPersonList(FILTER_TEST_PREDICATE); } @Test public void execute_listIsNotFiltered_showsSameList() { - assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel); + assertCommandSuccess(new ListCommand(), model, String.format( + Messages.MESSAGE_LIST_SUCCESS, expectedModel.getFilteredPersonList().size()), expectedModel); } @Test public void execute_listIsFiltered_showsEverything() { - showPersonAtIndex(model, INDEX_FIRST_PERSON); - assertCommandSuccess(new ListCommand(), model, ListCommand.MESSAGE_SUCCESS, expectedModel); + assertCommandSuccess(new ListCommand(FILTER_TEST_PREDICATE), model, String.format( + Messages.MESSAGE_FILTER_SUCCESS, expectedFilteredModel.getFilteredPersonList().size()), + expectedFilteredModel); } }