|
| 1 | +package seedu.address.logic.commands.notes; |
| 2 | + |
| 3 | +import static java.util.Objects.requireNonNull; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import seedu.address.commons.core.Messages; |
| 8 | +import seedu.address.commons.core.index.Index; |
| 9 | +import seedu.address.logic.commands.Command; |
| 10 | +import seedu.address.logic.commands.CommandResult; |
| 11 | +import seedu.address.logic.commands.exceptions.CommandException; |
| 12 | +import seedu.address.model.Model; |
| 13 | +import seedu.address.model.note.Note; |
| 14 | + |
| 15 | +/** |
| 16 | + * Deletes a note identified using it's displayed index from the note book. |
| 17 | + */ |
| 18 | +public class DeleteNoteCommand extends Command { |
| 19 | + |
| 20 | + public static final String COMMAND_WORD = "deleten"; |
| 21 | + |
| 22 | + public static final String MESSAGE_USAGE = COMMAND_WORD |
| 23 | + + ": Deletes the note identified by the index number used in the displayed note list.\n" |
| 24 | + + "Parameters: INDEX (must be a positive integer)\n" |
| 25 | + + "Example: " + COMMAND_WORD + " 1"; |
| 26 | + |
| 27 | + public static final String MESSAGE_DELETE_NOTE_SUCCESS = "Deleted Note: %1$s"; |
| 28 | + |
| 29 | + private final Index targetIndex; |
| 30 | + |
| 31 | + public DeleteNoteCommand(Index targetIndex) { |
| 32 | + this.targetIndex = targetIndex; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public CommandResult execute(Model model) throws CommandException { |
| 37 | + requireNonNull(model); |
| 38 | + List<Note> lastShownList = model.getFilteredNoteList(); |
| 39 | + |
| 40 | + if (targetIndex.getZeroBased() >= lastShownList.size()) { |
| 41 | + throw new CommandException(Messages.MESSAGE_INVALID_NOTE_DISPLAYED_INDEX); |
| 42 | + } |
| 43 | + |
| 44 | + Note noteToDelete = lastShownList.get(targetIndex.getZeroBased()); |
| 45 | + model.deleteNote(noteToDelete); |
| 46 | + return new CommandResult(String.format(MESSAGE_DELETE_NOTE_SUCCESS, noteToDelete)); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean equals(Object other) { |
| 51 | + return other == this // short circuit if same object |
| 52 | + || (other instanceof seedu.address.logic.commands.notes.DeleteNoteCommand // instanceof handles nulls |
| 53 | + && targetIndex.equals(((seedu.address.logic.commands.notes.DeleteNoteCommand) other).targetIndex)); // state check |
| 54 | + } |
| 55 | +} |
| 56 | + |
0 commit comments