From 3a1eea595b85253f7a5e2129f66ce8cfd0d90fa3 Mon Sep 17 00:00:00 2001 From: Jovyn Tan Date: Mon, 18 Oct 2021 12:06:00 +0800 Subject: [PATCH] Add reminder find command --- .../seedu/address/commons/core/Messages.java | 1 + .../address/logic/commands/ListCommand.java | 6 +-- .../reminder/ReminderFindCommand.java | 44 ++++++++++++++++++ .../logic/parser/.AddressBookParser.java.swp | Bin 12288 -> 0 bytes .../logic/parser/AddressBookParser.java | 6 ++- .../reminder/ReminderFindCommandParser.java | 33 +++++++++++++ src/main/java/seedu/address/model/Model.java | 9 +++- .../seedu/address/model/ModelManager.java | 6 +++ ...ReminderNameContainsKeywordsPredicate.java | 31 ++++++++++++ 9 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 src/main/java/seedu/address/logic/commands/reminder/ReminderFindCommand.java delete mode 100644 src/main/java/seedu/address/logic/parser/.AddressBookParser.java.swp create mode 100644 src/main/java/seedu/address/logic/parser/reminder/ReminderFindCommandParser.java create mode 100644 src/main/java/seedu/address/model/reminder/ReminderNameContainsKeywordsPredicate.java diff --git a/src/main/java/seedu/address/commons/core/Messages.java b/src/main/java/seedu/address/commons/core/Messages.java index 83427be5cef..68431b37271 100644 --- a/src/main/java/seedu/address/commons/core/Messages.java +++ b/src/main/java/seedu/address/commons/core/Messages.java @@ -12,5 +12,6 @@ public class Messages { public static final String MESSAGE_INVALID_REMINDER_DISPLAYED_INDEX = "The reminder index provided is invalid :("; public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!"; public static final String MESSAGE_CCAS_LISTED_OVERVIEW = "%1$d CCAs listed!"; + public static final String MESSAGE_REMINDERS_LISTED_OVERVIEW = "%1$d reminders listed!"; } diff --git a/src/main/java/seedu/address/logic/commands/ListCommand.java b/src/main/java/seedu/address/logic/commands/ListCommand.java index 818802b9f35..e76c3083a79 100644 --- a/src/main/java/seedu/address/logic/commands/ListCommand.java +++ b/src/main/java/seedu/address/logic/commands/ListCommand.java @@ -1,8 +1,7 @@ package seedu.address.logic.commands; import static java.util.Objects.requireNonNull; -import static seedu.address.model.Model.PREDICATE_SHOW_ALL_CCAS; -import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; +import static seedu.address.model.Model.*; import seedu.address.model.Model; @@ -13,7 +12,7 @@ public class ListCommand extends Command { public static final String COMMAND_WORD = "list"; - public static final String MESSAGE_SUCCESS = "Listed all persons and CCAs"; + public static final String MESSAGE_SUCCESS = "Listed all persons, CCAs and reminders"; @Override @@ -21,6 +20,7 @@ public CommandResult execute(Model model) { requireNonNull(model); model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); model.updateFilteredCcaList(PREDICATE_SHOW_ALL_CCAS); + model.updateFilteredReminderList(PREDICATE_SHOW_ALL_REMINDERS); return new CommandResult(MESSAGE_SUCCESS); } } diff --git a/src/main/java/seedu/address/logic/commands/reminder/ReminderFindCommand.java b/src/main/java/seedu/address/logic/commands/reminder/ReminderFindCommand.java new file mode 100644 index 00000000000..a57598338e9 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/reminder/ReminderFindCommand.java @@ -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 + } +} diff --git a/src/main/java/seedu/address/logic/parser/.AddressBookParser.java.swp b/src/main/java/seedu/address/logic/parser/.AddressBookParser.java.swp deleted file mode 100644 index 7de3748ea392c16f8e4a0e4e2a2ab43bb68465bf..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2O>f*p7{{koc`3Ajs00WJX&NrxZLv!$P}M+Tvzw+7-6YMDMnaXMi9Ne<1(la0XFdQ_{Kxisy_;_291&yXx6bbPd71yr zJhM?|`P7x^COvDOCb%BM&z;Nc1NZL1tv?C57OZXhlKHA>uPb@(x#q;2=><0PMn3vh z&!N@eVpRsAwH`#PR=sul`BP^s8CoK;tuAwY>l#~Qmf+lpE#^287sB#_j%!;Xiv*9X zTKeHc5UkE6pP0&GWMJK(70?PmfurOM)|!&y*|FpF*y9UY6|I0)Kr5gX&na2@HO}fd<;GU?}1q`1#Ul#XTdG-3HT5!0}I@Fh>#z^yWlK%0(^0p zkWax4@CJAW+(uA# zG&$ikxnc8Ax`A&rCtI!B)a25_%xg2Vi!)30*=Dmg)2NK+jvU@*u_w=^0hBX!MO?;_ zpLo2;y|5m1yUcftf}PC-r-e2%bG@`UJKq@XZF0kvqr4T`N_v}$I3MY**jUovC0C4e zS7cm~{OED~H!K7r6y2n{GO3`9tNq-IQamn|Y_JC2z zK$A{B6UwSnFew=XXaDxumA=KO5Mtex_6jxZbWtn@kyD{>l4RZBrS1C6qb(V^euw4* zO<$&61~VR+ohXPyV=QkPD@=j`<3fPl=7mP(AP?U!5h|gPg(1z)YzGn2%FD#`%$3@u z=|-$}p$+7VoJUu8*ycAh?t|2h<#kXloskbX81Y>x>+cb)ZBW2{0|)3GnAW0Iu%#O;Pb zBoP%Sh)tFr7RKRZoFGoCb!771ek#mF<9vS|-^)1DLYRe}qntruTP { + + /** + * 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))); + } + +} diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 96b96a712fc..c48120cb30b 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -16,6 +16,7 @@ public interface Model { /** {@code Predicate} that always evaluate to true */ Predicate PREDICATE_SHOW_ALL_PERSONS = unused -> true; Predicate PREDICATE_SHOW_ALL_CCAS = unused -> true; + Predicate PREDICATE_SHOW_ALL_REMINDERS = unused -> true; /** * Replaces user prefs data with the data in {@code userPrefs}. @@ -116,11 +117,17 @@ public interface Model { void updateFilteredPersonList(Predicate predicate); /** - * Updates the filter of the filtered person list to filter by the given {@code predicate}. + * Updates the filter of the filtered CCA list to filter by the given {@code predicate}. * @throws NullPointerException if {@code predicate} is null. */ void updateFilteredCcaList(Predicate predicate); + /** + * Updates the filter of the filtered reminder list to filter by the given {@code predicate}. + * @throws NullPointerException if {@code predicate} is null. + */ + void updateFilteredReminderList(Predicate predicate); + /** * Enrols a person into a CCA */ diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 0bd17fe4fda..b218da7b413 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -262,4 +262,10 @@ public boolean equals(Object obj) { public ObservableList getFilteredReminderList() { return filteredReminders; } + + @Override + public void updateFilteredReminderList(Predicate predicate) { + requireNonNull(predicate); + filteredReminders.setPredicate(predicate); + } } diff --git a/src/main/java/seedu/address/model/reminder/ReminderNameContainsKeywordsPredicate.java b/src/main/java/seedu/address/model/reminder/ReminderNameContainsKeywordsPredicate.java new file mode 100644 index 00000000000..c018b0b10a2 --- /dev/null +++ b/src/main/java/seedu/address/model/reminder/ReminderNameContainsKeywordsPredicate.java @@ -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 { + private final List keywords; + + public ReminderNameContainsKeywordsPredicate(List 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 + } + +}