forked from nus-cs2103-AY2122S1/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 #83 from jovyntls/reminders-find-command
Add Find command for Reminders
- Loading branch information
Showing
9 changed files
with
131 additions
and
5 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
44 changes: 44 additions & 0 deletions
44
src/main/java/seedu/address/logic/commands/reminder/ReminderFindCommand.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,44 @@ | ||
package seedu.address.logic.commands.reminder; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.reminder.ReminderNameContainsKeywordsPredicate; | ||
|
||
/** | ||
* Finds and lists all persons in address book whose name contains any of the argument keywords. | ||
* Keyword matching is case insensitive. | ||
*/ | ||
public class ReminderFindCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "findr"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all reminders whose titles 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 + " meeting"; | ||
|
||
private final ReminderNameContainsKeywordsPredicate predicate; | ||
|
||
public ReminderFindCommand(ReminderNameContainsKeywordsPredicate predicate) { | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
model.updateFilteredReminderList(predicate); | ||
return new CommandResult( | ||
String.format(Messages.MESSAGE_REMINDERS_LISTED_OVERVIEW, model.getFilteredReminderList().size())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof ReminderFindCommand // instanceof handles nulls | ||
&& predicate.equals(((ReminderFindCommand) other).predicate)); // state check | ||
} | ||
} |
Binary file not shown.
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/reminder/ReminderFindCommandParser.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.reminder; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import java.util.Arrays; | ||
|
||
import seedu.address.logic.commands.reminder.ReminderFindCommand; | ||
import seedu.address.logic.parser.Parser; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.reminder.ReminderNameContainsKeywordsPredicate; | ||
|
||
/** | ||
* Parses input arguments and creates a new ReminderFindCommand object | ||
*/ | ||
public class ReminderFindCommandParser implements Parser<ReminderFindCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the ReminderFindCommand | ||
* and returns a ReminderFindCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public ReminderFindCommand parse(String args) throws ParseException { | ||
String trimmedArgs = args.trim(); | ||
if (trimmedArgs.isEmpty()) { | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ReminderFindCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
String[] nameKeywords = trimmedArgs.split("\\s+"); | ||
|
||
return new ReminderFindCommand(new ReminderNameContainsKeywordsPredicate(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
31 changes: 31 additions & 0 deletions
31
src/main/java/seedu/address/model/reminder/ReminderNameContainsKeywordsPredicate.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,31 @@ | ||
package seedu.address.model.reminder; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.util.StringUtil; | ||
|
||
/** | ||
* Tests that a {@code Reminder}'s {@code CcaName} matches any of the keywords given. | ||
*/ | ||
public class ReminderNameContainsKeywordsPredicate implements Predicate<Reminder> { | ||
private final List<String> keywords; | ||
|
||
public ReminderNameContainsKeywordsPredicate(List<String> keywords) { | ||
this.keywords = keywords; | ||
} | ||
|
||
@Override | ||
public boolean test(Reminder reminder) { | ||
return keywords.stream() | ||
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(reminder.getName().fullName, keyword)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof ReminderNameContainsKeywordsPredicate // instanceof handles nulls | ||
&& keywords.equals(((ReminderNameContainsKeywordsPredicate) other).keywords)); // state check | ||
} | ||
|
||
} |