forked from nus-cs2103-AY1920S1/addressbook-level3
/
UndoCommand.java
41 lines (39 loc) Β· 1.54 KB
/
UndoCommand.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
package seedu.address.logic.commands;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.history.HistoryManager;
/**
* Undo the most recent undoable Command.
*/
public class UndoCommand extends Command {
public static final String COMMAND_WORD = "undo";
public static final String MESSAGE_SUCCESS = "Undo Command Success";
public static final String MESSAGE_FAILURE = "Undo Command Failure:"
+ " No available commands to be undone. "
+ " Commands that can be undone is as follows: add, delete, edit, clear and training.";
@Override
public CommandResult execute(Model model) throws CommandException {
HistoryManager history = new HistoryManager();
if (history.isUndoneEmpty()) {
return new CommandResult(MESSAGE_FAILURE);
}
while (!history.getLatestCommand().isUndoable()) {
System.out.println(history.getLatestCommand());
if (history.isUndoneEmpty()) {
return new CommandResult(MESSAGE_FAILURE);
} else {
System.out.println(HistoryManager.getCommands().pop());
System.out.println(HistoryManager.getAddressBooks().pop());
if (history.isUndoneEmpty()) {
return new CommandResult(MESSAGE_FAILURE);
}
}
}
model.undo();
return new CommandResult(MESSAGE_SUCCESS);
}
@Override
public boolean isUndoable() {
return false;
}
}