forked from nus-cs2103-AY2425S1/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 find tag feature to FindCommand #59
Merged
Dominic-Khoo
merged 1 commit into
AY2425S1-CS2103T-W11-1a:master
from
Dominic-Khoo:branch-find-tag
Oct 22, 2024
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -2,27 +2,29 @@ | |
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.NameContainsKeywordsPredicate; | ||
import seedu.address.model.person.Person; | ||
|
||
/** | ||
* Finds and lists all persons in address book whose name contains any of the argument keywords. | ||
* Finds and lists all persons in address book whose name or tags contain any of the argument keywords. | ||
* Keyword matching is case insensitive. | ||
*/ | ||
public class FindCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "find"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names contain any of " | ||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names or tags contain any of " | ||
+ "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"; | ||
+ "Example: " + COMMAND_WORD + " alice bob charlie (for names) OR find t/diabetic t/G6PD-Def"; | ||
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. there might be a better way to display this usage message but small issue |
||
|
||
private final NameContainsKeywordsPredicate predicate; | ||
private final Predicate<Person> predicate; | ||
|
||
public FindCommand(NameContainsKeywordsPredicate predicate) { | ||
public FindCommand(Predicate<Person> predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
|
@@ -40,7 +42,6 @@ public boolean equals(Object other) { | |
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof FindCommand)) { | ||
return false; | ||
} | ||
|
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
29 changes: 29 additions & 0 deletions
29
src/main/java/seedu/address/model/TagContainsKeywordsPredicate.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,29 @@ | ||
package seedu.address.model.person; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Tests that a {@code Person}'s {@code Tag} matches any of the keywords given. | ||
*/ | ||
public class TagContainsKeywordsPredicate implements Predicate<Person> { | ||
private final List<String> keywords; | ||
|
||
public TagContainsKeywordsPredicate(List<String> keywords) { | ||
this.keywords = keywords; | ||
} | ||
|
||
@Override | ||
public boolean test(Person person) { | ||
return keywords.stream() | ||
.anyMatch(keyword -> person.getTags().stream() | ||
.anyMatch(tag -> tag.tagName.equalsIgnoreCase(keyword))); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof TagContainsKeywordsPredicate // instanceof handles nulls | ||
&& keywords.equals(((TagContainsKeywordsPredicate) other).keywords)); // state check | ||
} | ||
} |
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 |
---|---|---|
|
@@ -39,15 +39,15 @@ | |
"nric": "S1234567E", | ||
"address" : "michegan ave", | ||
"remark" : "", | ||
"tags" : [ ] | ||
"tags" : [ "Diabetic", "G6PD" ] | ||
}, { | ||
"name" : "Fiona Kunz", | ||
"phone" : "9482427", | ||
"email" : "[email protected]", | ||
"nric": "S1234567F", | ||
"address" : "little tokyo", | ||
"remark" : "", | ||
"tags" : [ ] | ||
"tags" : [ "Diabetic", "G6PD" ] | ||
}, { | ||
"name" : "George Best", | ||
"phone" : "9482442", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,7 @@ public class TypicalPersons { | |
.withNric("S1234567E") | ||
.withEmail("[email protected]") | ||
.withAddress("michegan ave") | ||
.withTags("Diabetic", "G6PD") | ||
.withAppointment("10-11-2024 05:31") | ||
.build(); | ||
public static final Person FIONA = new PersonBuilder() | ||
|
@@ -76,6 +77,7 @@ public class TypicalPersons { | |
.withNric("S1234567F") | ||
.withEmail("[email protected]") | ||
.withAddress("little tokyo") | ||
.withTags("Diabetic", "G6PD") | ||
.withAppointment("08-11-2024 13:22") | ||
.build(); | ||
public static final Person GEORGE = new PersonBuilder() | ||
|
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.
side note I think predicates can have their own directory for organisation
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.
noted, will do in next iteration