From ba0905dc7da01d2c9dd6f03c50b937f6c791f9c0 Mon Sep 17 00:00:00 2001 From: harithhh Date: Tue, 5 Nov 2024 15:35:36 +0800 Subject: [PATCH 1/2] Implement edit for reminder --- .../commands/reminder/AddReminderCommand.java | 15 +- .../reminder/EditReminderCommand.java | 171 +++++++++++++++--- .../reminder/EditReminderCommandParser.java | 46 ++++- 3 files changed, 201 insertions(+), 31 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/reminder/AddReminderCommand.java b/src/main/java/seedu/address/logic/commands/reminder/AddReminderCommand.java index 29c81e640cf..2603e746053 100644 --- a/src/main/java/seedu/address/logic/commands/reminder/AddReminderCommand.java +++ b/src/main/java/seedu/address/logic/commands/reminder/AddReminderCommand.java @@ -1,5 +1,8 @@ package seedu.address.logic.commands.reminder; import static java.util.Objects.requireNonNull; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE_TIME; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION; +import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; import java.util.List; @@ -22,13 +25,13 @@ public class AddReminderCommand extends Command { public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a reminder to the address book. " + "Parameters: " - + "Person " - + "DATE and TIME " - + "Description\n" + + PREFIX_NAME + "NAME " + + PREFIX_DATE_TIME + "DATE and TIME " + + PREFIX_DESCRIPTION + "DESCRIPTION\n" + "Example: " + COMMAND_WORD + " " - + "John " - + "2021-12-31 23:59" - + "New Year's Eve"; + + PREFIX_NAME + "John " + + PREFIX_DATE_TIME + "2021-12-31 23:59" + + PREFIX_DESCRIPTION + "New Year's Eve"; public static final String MESSAGE_SUCCESS = "New reminder added: %1$s"; public static final String MESSAGE_NONEXISTENT_PERSON = "This person doesn't exist in the address book."; diff --git a/src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java b/src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java index 3ddf58d724f..06f072e07f4 100644 --- a/src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java +++ b/src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java @@ -1,27 +1,114 @@ package seedu.address.logic.commands.reminder; +import javafx.collections.ObservableList; +import seedu.address.commons.core.index.Index; +import seedu.address.commons.util.CollectionUtil; +import seedu.address.logic.Messages; import seedu.address.logic.commands.Command; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; +import seedu.address.model.person.NameContainsKeywordsDeletePredicate; +import seedu.address.model.person.Person; +import seedu.address.model.reminder.Reminder; +import seedu.address.model.reminder.ReminderDescription; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +import static java.util.Objects.requireNonNull; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE_TIME; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION; /** * Edits a reminder in the address book. */ public class EditReminderCommand extends Command { - public static final String COMMAND_WORD = "editReminder"; - - public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits a reminder in the address book. " - + "Parameters: " - + "INDEX " - + "DATE " - + "TIME " - + "MESSAGE\n" - + "Example: " + COMMAND_WORD + " " - + "1 " - + "2021-12-31 " - + "23:59 " - + "New Year's Eve"; + public static final String COMMAND_WORD = "re"; // reminder edit + + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits a reminder in the address book identified " + + "by the index number displayed in the reminder list. " + + "Existing values will be overwritten by the input values.\n" + + "Parameters: INDEX (must be positive integer)" + + PREFIX_DATE_TIME + "DATE and TIME " + + PREFIX_DESCRIPTION + "DESCRIPTION\n" + + "Example: " + + COMMAND_WORD + " 1 " + + PREFIX_DATE_TIME + " 2022-01-01 00:00 " + + PREFIX_DESCRIPTION + " New Year's"; + + public static final String MESSAGE_EDIT_REMINDER_SUCCESS = "Edited reminder %1$s"; + public static final String MESSAGE_REMINDER_NOT_EDITED = "At least one field must be edited"; + + private final Index index; + private final EditReminderFields editReminderFields; + + /** + * + * @param index of the reminder in the reminder list to edit + * @param editReminderFields details of the edited reminder + */ + public EditReminderCommand(Index index, EditReminderFields editReminderFields) { + requireNonNull(index); + requireNonNull(editReminderFields); + + this.index = index; + this.editReminderFields = editReminderFields; + } + + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + List lastShownList = model.getDisplayReminders(); + + if (index.getZeroBased() >= lastShownList.size()) { + throw new CommandException(Messages.MESSAGE_INVALID_REMINDER_DISPLAYED_INDEX); + } + + Reminder reminderToEdit = lastShownList.get(index.getZeroBased()); + Reminder editedReminder = createEditedReminder(reminderToEdit, editReminderFields); + + if (!reminderToEdit.isSameReminder(editedReminder) && model.hasReminder(editedReminder)) { + throw new CommandException(MESSAGE_REMINDER_NOT_EDITED); + } + + // Parse input using the NameContainsKeywordsPredicate + String fullName = reminderToEdit.getPersonName() + "/"; + String[] nameKeywords = fullName.split("\\s+"); + NameContainsKeywordsDeletePredicate predicate = new NameContainsKeywordsDeletePredicate( + List.of(nameKeywords)); + ObservableList persons = model.getClientHub().getPersonList(); + List matchingPersons = persons.filtered(predicate); + + // Check if there is exactly one match + if (matchingPersons.size() == 1) { + Person person = matchingPersons.get(0); + person.deleteReminder(reminderToEdit); + person.addReminder(editedReminder); + model.setReminder(reminderToEdit, editedReminder); + model.updateFilteredReminderList(Model.PREDICATE_SHOW_ALL_REMINDERS); + return new CommandResult(String.format(MESSAGE_EDIT_REMINDER_SUCCESS, Messages.format(reminderToEdit))); + } else { + throw new CommandException("More than one person with the specified name found. Please be more specific."); + } + } + + /** + * Creates and returns an edited a {@code Reminder} with the details of {@code reminderToEdit} + * edited with the details of {@code editReminderFields}. + */ + public static Reminder createEditedReminder(Reminder reminderToEdit, EditReminderFields editReminderFields) { + assert reminderToEdit != null; + + // EditReminder does not allow editing of personName + String personName = reminderToEdit.getPersonName(); + LocalDateTime UpdatedDateTime = editReminderFields.getDateTime().orElse(reminderToEdit.getDateTime()); + ReminderDescription UpdatedDescription = editReminderFields.getDescription().orElse(reminderToEdit.getDescription()); + + return new Reminder(personName, UpdatedDateTime, UpdatedDescription); + } @Override public boolean equals(Object other) { @@ -34,15 +121,53 @@ public String toString() { return "EditReminderCommand"; } - /** - * Executes the command to edit a reminder to the model. - * - * @param model The {@code Model} which the command should operate on. - * @return A {@code CommandResult} indicating the outcome of the command execution. - * @throws CommandException If there is an issue during command execution. - */ - @Override - public CommandResult execute(Model model) throws CommandException { - return null; + public static class EditReminderFields { + private LocalDateTime dateTime; + private ReminderDescription description; + + public EditReminderFields() {} + + public EditReminderFields(EditReminderFields toCopy) { + setDateTime(toCopy.dateTime); + setDescription(toCopy.description); + } + + /** + * @return true if any field is edited + */ + public boolean isAnyFieldEdited() { + return CollectionUtil.isAnyNonNull(dateTime, description); + } + + public void setDateTime(LocalDateTime dateTime) { + this.dateTime = dateTime; + } + + public Optional getDateTime() { + return Optional.ofNullable(dateTime); + } + public void setDescription(ReminderDescription description) { + this.description = description; + } + + public Optional getDescription() { + return Optional.ofNullable(description); + } + + @Override + public boolean equals(Object other) { + if (other == this || other instanceof EditReminderFields) { + return true; + } + + EditReminderFields otherEditReminderFields = (EditReminderFields) other; + return Objects.equals(dateTime, otherEditReminderFields.dateTime) + && Objects.equals(description, otherEditReminderFields.description); + } + + @Override + public String toString() { + return "EditReminderFields"; + } } } diff --git a/src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java b/src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java index 8b8de411b5e..ca8efcb7284 100644 --- a/src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java @@ -1,7 +1,20 @@ package seedu.address.logic.parser.reminder; +import seedu.address.commons.core.index.Index; +import seedu.address.logic.commands.EditCommand; import seedu.address.logic.commands.reminder.EditReminderCommand; +import seedu.address.logic.commands.reminder.EditReminderCommand.EditReminderFields; +import seedu.address.logic.parser.ArgumentMultimap; +import seedu.address.logic.parser.ArgumentTokenizer; import seedu.address.logic.parser.Parser; +import seedu.address.logic.parser.ParserUtil; +import seedu.address.logic.parser.exceptions.ParseException; + +import static java.util.Objects.requireNonNull; +import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE_TIME; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION; +import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; /** * Parses user input to create a new {@code EditReminderCommand}. @@ -11,7 +24,36 @@ public EditReminderCommandParser() { } @Override - public EditReminderCommand parse(String userInput) { - return null; + public EditReminderCommand parse(String args) throws ParseException { + requireNonNull(args); + ArgumentMultimap argMultimap + = ArgumentTokenizer.tokenize(args, PREFIX_DATE_TIME, PREFIX_DESCRIPTION); + + Index index; + + try { + index = ParserUtil.parseIndex(argMultimap.getPreamble()); + } catch (ParseException pe) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, + EditReminderCommand.MESSAGE_USAGE), pe); + } + + argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_DATE_TIME, PREFIX_DESCRIPTION); + + EditReminderFields editReminderFields = new EditReminderFields(); + + if (argMultimap.getValue(PREFIX_DATE_TIME).isPresent()) { + editReminderFields.setDateTime(ParserUtil.parseDateTime(argMultimap.getValue(PREFIX_DATE_TIME).get())); + } + if (argMultimap.getValue(PREFIX_DESCRIPTION).isPresent()) { + editReminderFields.setDescription(ParserUtil.parseReminderDescription( + argMultimap.getValue(PREFIX_DESCRIPTION).get())); + } + + if (!editReminderFields.isAnyFieldEdited()) { + throw new ParseException(EditCommand.MESSAGE_NOT_EDITED); + } + + return new EditReminderCommand(index, editReminderFields); } } From 1d048c0ff4a62e06995b748d00f544cb0d078492 Mon Sep 17 00:00:00 2001 From: harithhh Date: Tue, 5 Nov 2024 15:52:08 +0800 Subject: [PATCH 2/2] Add missing Javadoc comments and fix checkstyle --- .../commands/reminder/AddReminderCommand.java | 2 - .../reminder/EditReminderCommand.java | 45 ++++++++++++------- .../reminder/EditReminderCommandParser.java | 14 +++--- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/reminder/AddReminderCommand.java b/src/main/java/seedu/address/logic/commands/reminder/AddReminderCommand.java index 2603e746053..5ad31327289 100644 --- a/src/main/java/seedu/address/logic/commands/reminder/AddReminderCommand.java +++ b/src/main/java/seedu/address/logic/commands/reminder/AddReminderCommand.java @@ -15,8 +15,6 @@ import seedu.address.model.person.Person; import seedu.address.model.reminder.Reminder; - - /** * Adds a reminder to the address book. */ diff --git a/src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java b/src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java index 06f072e07f4..8853f0316a9 100644 --- a/src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java +++ b/src/main/java/seedu/address/logic/commands/reminder/EditReminderCommand.java @@ -1,4 +1,12 @@ package seedu.address.logic.commands.reminder; +import static java.util.Objects.requireNonNull; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE_TIME; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Objects; +import java.util.Optional; import javafx.collections.ObservableList; import seedu.address.commons.core.index.Index; @@ -13,23 +21,14 @@ import seedu.address.model.reminder.Reminder; import seedu.address.model.reminder.ReminderDescription; -import java.time.LocalDateTime; -import java.util.List; -import java.util.Objects; -import java.util.Optional; - -import static java.util.Objects.requireNonNull; -import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE_TIME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION; - /** * Edits a reminder in the address book. */ public class EditReminderCommand extends Command { public static final String COMMAND_WORD = "re"; // reminder edit - public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits a reminder in the address book identified " + - "by the index number displayed in the reminder list. " + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits a reminder in the address book identified " + + "by the index number displayed in the reminder list. " + "Existing values will be overwritten by the input values.\n" + "Parameters: INDEX (must be positive integer)" + PREFIX_DATE_TIME + "DATE and TIME " @@ -46,7 +45,6 @@ public class EditReminderCommand extends Command { private final EditReminderFields editReminderFields; /** - * * @param index of the reminder in the reminder list to edit * @param editReminderFields details of the edited reminder */ @@ -55,7 +53,7 @@ public EditReminderCommand(Index index, EditReminderFields editReminderFields) { requireNonNull(editReminderFields); this.index = index; - this.editReminderFields = editReminderFields; + this.editReminderFields = new EditReminderFields(editReminderFields); } @Override @@ -104,10 +102,11 @@ public static Reminder createEditedReminder(Reminder reminderToEdit, EditReminde // EditReminder does not allow editing of personName String personName = reminderToEdit.getPersonName(); - LocalDateTime UpdatedDateTime = editReminderFields.getDateTime().orElse(reminderToEdit.getDateTime()); - ReminderDescription UpdatedDescription = editReminderFields.getDescription().orElse(reminderToEdit.getDescription()); + LocalDateTime updatedDateTime = editReminderFields.getDateTime().orElse(reminderToEdit.getDateTime()); + ReminderDescription updatedDescription = editReminderFields.getDescription() + .orElse(reminderToEdit.getDescription()); - return new Reminder(personName, UpdatedDateTime, UpdatedDescription); + return new Reminder(personName, updatedDateTime, updatedDescription); } @Override @@ -121,12 +120,26 @@ public String toString() { return "EditReminderCommand"; } + /** + * Stores the details to edit the reminder with. Each non-empty field value will replace the + * corresponding field value of the reminder. + */ public static class EditReminderFields { private LocalDateTime dateTime; private ReminderDescription description; + /** + * Default constructor for {@code EditReminderFields}. + * Initializes an instance with no specified fields. + */ public EditReminderFields() {} + /** + * Copy constructor for {@code EditReminderFields}. + * Creates a new instance by copying the fields from the specified {@code EditReminderFields} instance. + * + * @param toCopy The {@code EditReminderFields} instance to copy from. Must not be {@code null}. + */ public EditReminderFields(EditReminderFields toCopy) { setDateTime(toCopy.dateTime); setDescription(toCopy.description); diff --git a/src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java b/src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java index ca8efcb7284..2c7bf3093d4 100644 --- a/src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/reminder/EditReminderCommandParser.java @@ -1,4 +1,8 @@ package seedu.address.logic.parser.reminder; +import static java.util.Objects.requireNonNull; +import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE_TIME; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION; import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.EditCommand; @@ -10,12 +14,6 @@ import seedu.address.logic.parser.ParserUtil; import seedu.address.logic.parser.exceptions.ParseException; -import static java.util.Objects.requireNonNull; -import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; -import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE_TIME; -import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION; -import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; - /** * Parses user input to create a new {@code EditReminderCommand}. */ @@ -26,8 +24,8 @@ public EditReminderCommandParser() { @Override public EditReminderCommand parse(String args) throws ParseException { requireNonNull(args); - ArgumentMultimap argMultimap - = ArgumentTokenizer.tokenize(args, PREFIX_DATE_TIME, PREFIX_DESCRIPTION); + ArgumentMultimap argMultimap = + ArgumentTokenizer.tokenize(args, PREFIX_DATE_TIME, PREFIX_DESCRIPTION); Index index;