Skip to content

Commit

Permalink
Update RemarkCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
tsulim committed Mar 1, 2024
1 parent 75b1a96 commit e058e5e
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/main/java/seedu/address/logic/commands/RemarkCommand.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package seedu.address.logic.commands;

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.List;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.person.Remark;

/**
Expand All @@ -24,6 +29,8 @@ public class RemarkCommand extends Command {
+ "r/ Likes to swim.";

public static final String MESSAGE_ARGUMENTS = "Index: %1$d, Remark: %2$s";
public static final String MESSAGE_ADD_REMARK_SUCCESS = "Added remark to Person: %1$s";
public static final String MESSAGE_DELETE_REMARK_SUCCESS = "Removed remark from Person: %1$s";

private final Index index;
private final Remark remark;
Expand All @@ -40,8 +47,29 @@ public RemarkCommand(Index index, Remark remark) {
}
@Override
public CommandResult execute(Model model) throws CommandException {
throw new CommandException(
String.format(MESSAGE_ARGUMENTS, index.getOneBased(), remark));
List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = new Person(personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(),
personToEdit.getAddress(), remark, personToEdit.getTags());

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

return new CommandResult(generateSuccessMessage(editedPerson));
}

/**
* Generates a command execution success message based on whether the remark is added to or removed from
* {@code personToEdit}.
*/
private String generateSuccessMessage(Person personToEdit) {
String message = !remark.value.isEmpty() ? MESSAGE_ADD_REMARK_SUCCESS : MESSAGE_DELETE_REMARK_SUCCESS;
return String.format(message, personToEdit);
}

@Override
Expand Down

0 comments on commit e058e5e

Please sign in to comment.