forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add filter command #54
Merged
wolffe88
merged 2 commits into
AY2324S2-CS2103T-T14-1:master
from
wolffe88:filter-command
Mar 18, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/seedu/address/logic/commands/FilterCommand.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,58 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.PersonContainsKeywordsPredicate; | ||
|
||
/** | ||
* Finds and lists all persons in address book whose name 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 persons by the given departments or tags" | ||
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n" | ||
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n" | ||
+ "Example: " + COMMAND_WORD + " alice bob charlie"; | ||
|
||
private final PersonContainsKeywordsPredicate predicate; | ||
|
||
public FilterCommand(PersonContainsKeywordsPredicate predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
model.updateFilteredPersonList(predicate); | ||
return new CommandResult( | ||
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof FilterCommand)) { | ||
return false; | ||
} | ||
|
||
FilterCommand otherFilterCommand = (FilterCommand) other; | ||
return predicate.equals(otherFilterCommand.predicate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("predicate", predicate) | ||
.toString(); | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/main/java/seedu/address/logic/parser/FilterCommandParser.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,33 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.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.person.PersonContainsKeywordsPredicate; | ||
|
||
/** | ||
* Parses input arguments and creates a new FilterCommand object | ||
*/ | ||
public class FilterCommandParser implements Parser<FilterCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the FilterCommand | ||
* and returns a 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[] nameKeywords = trimmedArgs.split("\\s+"); | ||
|
||
return new FilterCommand(new PersonContainsKeywordsPredicate(Arrays.asList(nameKeywords))); | ||
} | ||
|
||
} |
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
45 changes: 45 additions & 0 deletions
45
src/main/java/seedu/address/model/person/PersonContainsKeywordsPredicate.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,45 @@ | ||
package seedu.address.model.person; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Javadoc typos |
||
*/ | ||
public class PersonContainsKeywordsPredicate implements Predicate<Person> { | ||
private final List<String> keywords; | ||
|
||
public PersonContainsKeywordsPredicate(List<String> keywords) { | ||
this.keywords = keywords; | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
return keywords.stream() | ||
.anyMatch(keyword -> person.getDepartment().toString().toLowerCase().replaceAll("\\s", "") | ||
.contains(keyword.toLowerCase()) | ||
|| person.getTags().toString().toLowerCase().contains(keyword.toLowerCase())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof PersonContainsKeywordsPredicate)) { | ||
return false; | ||
} | ||
|
||
PersonContainsKeywordsPredicate otherPersonContainsKeywordsPredicate = (PersonContainsKeywordsPredicate) other; | ||
return keywords.equals(otherPersonContainsKeywordsPredicate.keywords); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).add("keywords", keywords).toString(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/test/java/seedu/address/logic/commands/FilterCommandTest.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,51 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW; | ||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; | ||
import static seedu.address.testutil.TypicalPersons.CARL; | ||
import static seedu.address.testutil.TypicalPersons.DANIEL; | ||
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.UserPrefs; | ||
import seedu.address.model.person.PersonContainsKeywordsPredicate; | ||
|
||
class FilterCommandTest { | ||
|
||
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
|
||
@Test | ||
public void execute_zeroKeywords_noPersonFound() { | ||
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0); | ||
PersonContainsKeywordsPredicate predicate = preparePredicate(" "); | ||
FilterCommand command = new FilterCommand(predicate); | ||
expectedModel.updateFilteredPersonList(predicate); | ||
assertCommandSuccess(command, model, expectedMessage, expectedModel); | ||
assertEquals(Collections.emptyList(), model.getFilteredPersonList()); | ||
} | ||
|
||
@Test | ||
public void execute_multipleKeywords_multiplePersonsFound() { | ||
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 2); | ||
PersonContainsKeywordsPredicate predicate = preparePredicate("Marketing Production"); | ||
FilterCommand command = new FilterCommand(predicate); | ||
expectedModel.updateFilteredPersonList(predicate); | ||
assertCommandSuccess(command, model, expectedMessage, expectedModel); | ||
assertEquals(Arrays.asList(CARL, DANIEL), model.getFilteredPersonList()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great work on testing the scenario of filtering based on multiple keywords. |
||
} | ||
|
||
/** | ||
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}. | ||
*/ | ||
private PersonContainsKeywordsPredicate preparePredicate(String userInput) { | ||
return new PersonContainsKeywordsPredicate(Arrays.asList(userInput.split("\\s+"))); | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/test/java/seedu/address/logic/parser/FilterCommandParserTest.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,20 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.commands.FilterCommand; | ||
|
||
public class FilterCommandParserTest { | ||
|
||
private FilterCommandParser parser = new FilterCommandParser(); | ||
|
||
@Test | ||
public void parse_emptyArg_throwsParseException() { | ||
assertParseFailure(parser, " ", String.format(MESSAGE_INVALID_COMMAND_FORMAT, FilterCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
|
||
} |
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 |
---|---|---|
|
@@ -64,11 +64,6 @@ public void test_nameContainsKeywords_returnsTrue() { | |
// Keywords with substrings | ||
predicate = new NameContainsKeywordsPredicate(Collections.singletonList("ice")); | ||
assertTrue(predicate.test(new PersonBuilder().withName("Alice Bob").build())); | ||
|
||
// Keywords match tags | ||
predicate = new NameContainsKeywordsPredicate(Arrays.asList("12345", "[email protected]", "friend", "Street")); | ||
assertTrue(predicate.test(new PersonBuilder().withName("Alice").withPhone("12345") | ||
.withEmail("[email protected]").withAddress("Main Street").withTags("friend").build())); | ||
} | ||
|
||
@Test | ||
|
96 changes: 96 additions & 0 deletions
96
src/test/java/seedu/address/model/person/PersonContainsKeywordsPredicateTest.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,96 @@ | ||
package seedu.address.model.person; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.testutil.PersonBuilder; | ||
|
||
class PersonContainsKeywordsPredicateTest { | ||
|
||
@Test | ||
public void equals() { | ||
List<String> firstPredicateKeywordList = Collections.singletonList("first"); | ||
List<String> secondPredicateKeywordList = Arrays.asList("first", "second"); | ||
|
||
PersonContainsKeywordsPredicate firstPredicate = new PersonContainsKeywordsPredicate(firstPredicateKeywordList); | ||
PersonContainsKeywordsPredicate secondPredicate = | ||
new PersonContainsKeywordsPredicate(secondPredicateKeywordList); | ||
|
||
// same object -> returns true | ||
assertTrue(firstPredicate.equals(firstPredicate)); | ||
|
||
// same values -> returns true | ||
PersonContainsKeywordsPredicate firstPredicateCopy = | ||
new PersonContainsKeywordsPredicate(firstPredicateKeywordList); | ||
assertTrue(firstPredicate.equals(firstPredicateCopy)); | ||
|
||
// different types -> returns false | ||
assertFalse(firstPredicate.equals(1)); | ||
|
||
// null -> returns false | ||
assertFalse(firstPredicate.equals(null)); | ||
|
||
// different person -> returns false | ||
assertFalse(firstPredicate.equals(secondPredicate)); | ||
} | ||
|
||
@Test | ||
public void test_nameContainsKeywords_returnsTrue() { | ||
// One keyword | ||
PersonContainsKeywordsPredicate predicate = | ||
new PersonContainsKeywordsPredicate(Collections.singletonList("Finance")); | ||
assertTrue(predicate.test(new PersonBuilder().withDepartment("Finance").build())); | ||
|
||
// Multiple keywords | ||
predicate = new PersonContainsKeywordsPredicate(Arrays.asList("Quantitative", "Finance")); | ||
assertTrue(predicate.test(new PersonBuilder().withDepartment("Quantitative Finance").build())); | ||
|
||
// Only one matching keyword | ||
predicate = new PersonContainsKeywordsPredicate(Arrays.asList("Finance", "Marketing")); | ||
assertTrue(predicate.test(new PersonBuilder().withDepartment("Quantitative Finance").build())); | ||
|
||
// Mixed-case keywords | ||
predicate = new PersonContainsKeywordsPredicate(Arrays.asList("finAnce", "quaNtitative")); | ||
assertTrue(predicate.test(new PersonBuilder().withDepartment("Quantitative Finance").build())); | ||
|
||
// Keywords without spacing | ||
predicate = new PersonContainsKeywordsPredicate(Collections.singletonList("quantitativefinance")); | ||
assertTrue(predicate.test(new PersonBuilder().withDepartment("Quantitative Finance").build())); | ||
|
||
// Keywords with substrings | ||
predicate = new PersonContainsKeywordsPredicate(Collections.singletonList("nan")); | ||
assertTrue(predicate.test(new PersonBuilder().withDepartment("Quantitative Finance").build())); | ||
} | ||
|
||
@Test | ||
public void test_nameDoesNotContainKeywords_returnsFalse() { | ||
// Zero keywords | ||
NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Collections.emptyList()); | ||
assertFalse(predicate.test(new PersonBuilder().withDepartment("Finance").build())); | ||
|
||
// Non-matching keyword | ||
predicate = new NameContainsKeywordsPredicate(Arrays.asList("Marketing")); | ||
assertFalse(predicate.test(new PersonBuilder().withDepartment("Finance").build())); | ||
|
||
// Keywords match phone, email and address, but does not match department | ||
predicate = new NameContainsKeywordsPredicate(Arrays.asList("12345", "[email protected]", "Main", "Street")); | ||
assertFalse(predicate.test(new PersonBuilder().withName("Alice").withPhone("12345") | ||
.withEmail("[email protected]").withAddress("Main Street").withDepartment("Finance").build())); | ||
} | ||
|
||
@Test | ||
public void toStringMethod() { | ||
List<String> keywords = List.of("keyword1", "keyword2"); | ||
PersonContainsKeywordsPredicate predicate = new PersonContainsKeywordsPredicate(keywords); | ||
|
||
String expected = PersonContainsKeywordsPredicate.class.getCanonicalName() + "{keywords=" + keywords + "}"; | ||
assertEquals(expected, predicate.toString()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some typo in the java doc comments. Should be 'whose department or tags'