diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 4a0d2263f1d..f336c47d915 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -11,6 +11,7 @@ import javafx.collections.transformation.FilteredList; import seedu.address.commons.core.GuiSettings; import seedu.address.commons.core.LogsCenter; +import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.person.Person; /** @@ -26,6 +27,7 @@ public class ModelManager implements Model { private final FilteredList filteredPersons; private AddressBook backup; + private boolean undoable = false; /** * Initializes a ModelManager with the given addressBook and userPrefs. @@ -100,27 +102,39 @@ public boolean hasPerson(Person person) { @Override public void deletePerson(Person target) { + undoable = true; backup = copyAddressBook(); addressBook.removePerson(target); + undoable = false; } @Override public void addPerson(Person person) { + undoable = true; backup = copyAddressBook(); addressBook.addPerson(person); updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); + undoable = false; } @Override public void setPerson(Person target, Person editedPerson) { + undoable = true; backup = copyAddressBook(); requireAllNonNull(target, editedPerson); addressBook.setPerson(target, editedPerson); + undoable = false; } @Override - public void undoCommand() { - setAddressBook(backup); + public void undoCommand() throws CommandException { + if (undoable) { + setAddressBook(backup); + backup = copyAddressBook(); + } else { + throw new CommandException(UNABLE_TO_UNDO); + } + } /** diff --git a/src/test/java/seedu/address/logic/commands/UndoCommandTest.java b/src/test/java/seedu/address/logic/commands/UndoCommandTest.java new file mode 100644 index 00000000000..7ad9ce72b4b --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/UndoCommandTest.java @@ -0,0 +1,27 @@ +package seedu.address.logic.commands; + +import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; +import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; + +import org.junit.jupiter.api.Test; + +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; + +/** + * Contains integration tests (interaction with the Model) and unit tests for + * {@code UndoCommand}. + */ +public class UndoCommandTest { + + private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + + @Test + public void execute_undoCommand_throwsCommandException() { + UndoCommand undoCommand = new UndoCommand(); + + assertCommandFailure(undoCommand, model, "Command cannot be undone"); + } + +}