forked from AY1920S1-CS2103T-T11-3/main
/
DeleteEventCommand.java
60 lines (46 loc) · 2.17 KB
/
DeleteEventCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package seedu.address.logic.commands.event;
import static java.util.Objects.requireNonNull;
import java.util.List;
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
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.event.Event;
import seedu.address.ui.MainWindow;
/**
* Deletes a event identified using its displayed index from the EventBook.
*/
public class DeleteEventCommand extends Command {
public static final String COMMAND_WORD = "delete_ev";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the event identified by the index number used in the displayed event list.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";
public static final String MESSAGE_DELETE_EVENT_SUCCESS = "Deleted Event: %1$s";
private final Index targetIndex;
public DeleteEventCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
if (MainWindow.isFinanceTab() || MainWindow.isStatsTab()) {
throw new CommandException(Messages.MESSAGE_WRONG_TAB_MISSING_EVENT_LIST);
}
List<Event> lastShownList = MainWindow.getCurrentEventList(model);
if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_EVENT_DISPLAYED_INDEX);
}
Event eventToDelete = lastShownList.get(targetIndex.getZeroBased());
model.deleteEvent(eventToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_EVENT_SUCCESS, eventToDelete));
}
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof DeleteEventCommand // instanceof handles nulls
&& targetIndex.equals(((DeleteEventCommand) other).targetIndex)); // state check
}
}