Skip to content

Commit

Permalink
Merge pull request #51 from marcellaantania/department-filter
Browse files Browse the repository at this point in the history
Department filter
  • Loading branch information
remuslum committed Oct 4, 2023
2 parents a7073e1 + 3ff9944 commit 1c95258
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 26 additions & 3 deletions src/main/java/seedu/address/logic/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Person> 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<Person> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/seedu/address/logic/parser/ListCommandParser.java
Original file line number Diff line number Diff line change
@@ -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<ListCommand> {

/**
* 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Person> {

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());
}
}
3 changes: 2 additions & 1 deletion src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 19 additions & 5 deletions src/test/java/seedu/address/logic/commands/ListCommandTest.java
Original file line number Diff line number Diff line change
@@ -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<Person> 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);
}
}

0 comments on commit 1c95258

Please sign in to comment.