Skip to content

Commit

Permalink
Merge pull request #47 from jiewei98/undo-command
Browse files Browse the repository at this point in the history
Add a check if previous command was undoable
  • Loading branch information
MontyPython28 authored Mar 17, 2022
2 parents 333bdfb + c933aa4 commit 2b16f1b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -26,6 +27,7 @@ public class ModelManager implements Model {
private final FilteredList<Person> filteredPersons;

private AddressBook backup;
private boolean undoable = false;

/**
* Initializes a ModelManager with the given addressBook and userPrefs.
Expand Down Expand Up @@ -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);
}

}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/seedu/address/logic/commands/UndoCommandTest.java
Original file line number Diff line number Diff line change
@@ -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");
}

}

0 comments on commit 2b16f1b

Please sign in to comment.