forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from marcellaantania/department-filter
Department filter
- Loading branch information
Showing
7 changed files
with
118 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/seedu/address/logic/parser/ListCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/seedu/address/model/person/MatchingDepartmentPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 19 additions & 5 deletions
24
src/test/java/seedu/address/logic/commands/ListCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |