From 180229795256c49badf02dddaafb8e2c3bef70c1 Mon Sep 17 00:00:00 2001 From: marcellaantania Date: Wed, 4 Oct 2023 18:59:01 +0800 Subject: [PATCH 1/3] Add list by department name functionalities --- .../java/seedu/address/logic/Messages.java | 1 + .../address/logic/commands/ListCommand.java | 22 ++++++++++-- .../logic/parser/AddressBookParser.java | 2 +- .../logic/parser/ListCommandParser.java | 36 +++++++++++++++++++ .../person/MatchingDepartmentPredicate.java | 27 ++++++++++++++ 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 src/main/java/seedu/address/logic/parser/ListCommandParser.java create mode 100644 src/main/java/seedu/address/model/person/MatchingDepartmentPredicate.java diff --git a/src/main/java/seedu/address/logic/Messages.java b/src/main/java/seedu/address/logic/Messages.java index 7f9e17148e7..a976954a518 100644 --- a/src/main/java/seedu/address/logic/Messages.java +++ b/src/main/java/seedu/address/logic/Messages.java @@ -18,6 +18,7 @@ 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!"; /** * 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..9e8bb67cb05 100644 --- a/src/main/java/seedu/address/logic/commands/ListCommand.java +++ b/src/main/java/seedu/address/logic/commands/ListCommand.java @@ -4,6 +4,9 @@ import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; import seedu.address.model.Model; +import seedu.address.model.person.Person; + +import java.util.function.Predicate; /** * Lists all persons in the address book to the user. @@ -14,11 +17,26 @@ public class ListCommand extends Command { public static final String MESSAGE_SUCCESS = "Listed all employees"; + public static final String MESSAGE_FILTER_SUCCESS = "Listed filtered employees"; + + public final String message; + + public final Predicate predicate; + + public ListCommand() { + this.predicate = PREDICATE_SHOW_ALL_PERSONS; + this.message = MESSAGE_SUCCESS; + } + + public ListCommand(Predicate predicate) { + this.predicate = predicate; + this.message = 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(message); } } 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..697fffd540c --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/ListCommandParser.java @@ -0,0 +1,36 @@ +package seedu.address.logic.parser; + +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; + +import static java.util.Objects.requireNonNull; +import static seedu.address.logic.parser.CliSyntax.*; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DOB; + +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..ae77edcbe41 --- /dev/null +++ b/src/main/java/seedu/address/model/person/MatchingDepartmentPredicate.java @@ -0,0 +1,27 @@ +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()); + } +} From 8f265200f053998f39c23def454771f9baca1bc7 Mon Sep 17 00:00:00 2001 From: marcellaantania Date: Wed, 4 Oct 2023 20:06:16 +0800 Subject: [PATCH 2/3] Moves relevant messages to Messages class, Adds number of persons listed after filter --- .../java/seedu/address/logic/Messages.java | 4 ++++ .../address/logic/commands/ListCommand.java | 23 +++++++++++-------- .../logic/parser/ListCommandParser.java | 9 +++++--- .../person/MatchingDepartmentPredicate.java | 3 +-- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/main/java/seedu/address/logic/Messages.java b/src/main/java/seedu/address/logic/Messages.java index a976954a518..b611bd354a2 100644 --- a/src/main/java/seedu/address/logic/Messages.java +++ b/src/main/java/seedu/address/logic/Messages.java @@ -20,6 +20,10 @@ public class Messages { "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 9e8bb67cb05..0df738df41f 100644 --- a/src/main/java/seedu/address/logic/commands/ListCommand.java +++ b/src/main/java/seedu/address/logic/commands/ListCommand.java @@ -3,11 +3,12 @@ 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; -import java.util.function.Predicate; - /** * Lists all persons in the address book to the user. */ @@ -15,28 +16,32 @@ public class ListCommand extends Command { public static final String COMMAND_WORD = "list"; - public static final String MESSAGE_SUCCESS = "Listed all employees"; - - public static final String MESSAGE_FILTER_SUCCESS = "Listed filtered employees"; - public final String message; public final Predicate predicate; + /** + * Initializes ListCommand non-filtered list + */ public ListCommand() { this.predicate = PREDICATE_SHOW_ALL_PERSONS; - this.message = MESSAGE_SUCCESS; + 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 = MESSAGE_FILTER_SUCCESS; + this.message = Messages.MESSAGE_FILTER_SUCCESS; } @Override public CommandResult execute(Model model) { requireNonNull(model); model.updateFilteredPersonList(predicate); - return new CommandResult(message); + return new CommandResult(String.format(message, model.getFilteredPersonList().size())); } } diff --git a/src/main/java/seedu/address/logic/parser/ListCommandParser.java b/src/main/java/seedu/address/logic/parser/ListCommandParser.java index 697fffd540c..45968f02c7d 100644 --- a/src/main/java/seedu/address/logic/parser/ListCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/ListCommandParser.java @@ -1,15 +1,18 @@ 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; -import static java.util.Objects.requireNonNull; -import static seedu.address.logic.parser.CliSyntax.*; -import static seedu.address.logic.parser.CliSyntax.PREFIX_DOB; +/** + * Parses input arguments and creates a new ListCommand object + */ public class ListCommandParser implements Parser { /** diff --git a/src/main/java/seedu/address/model/person/MatchingDepartmentPredicate.java b/src/main/java/seedu/address/model/person/MatchingDepartmentPredicate.java index ae77edcbe41..e35bc014d69 100644 --- a/src/main/java/seedu/address/model/person/MatchingDepartmentPredicate.java +++ b/src/main/java/seedu/address/model/person/MatchingDepartmentPredicate.java @@ -17,8 +17,7 @@ public MatchingDepartmentPredicate(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} + * @return {@code true} if the input argument matches the predicate, otherwise {@code false} */ @Override public boolean test(Person person) { From 3ff9944f2797e44732746df7853d676df7f5ab40 Mon Sep 17 00:00:00 2001 From: marcellaantania Date: Wed, 4 Oct 2023 20:06:39 +0800 Subject: [PATCH 3/3] Updates tests to test department filtering --- .../seedu/address/logic/LogicManagerTest.java | 3 ++- .../logic/commands/ListCommandTest.java | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) 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); } }