forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from bryanyee33/86-undo-command
Add Undo command
- Loading branch information
Showing
15 changed files
with
234 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/seedu/address/logic/commands/UndoCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
|
||
/** | ||
* Undoes the latest modifying command | ||
*/ | ||
public class UndoCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "undo"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Undo the latest modifying command.\n" | ||
+ "Example: " + COMMAND_WORD; | ||
|
||
public static final String MESSAGE_SUCCESS = "Undid the latest modifying command."; | ||
public static final String MESSAGE_UNDO_EXCEPTION = "No modifying command to reverse."; | ||
|
||
@Override | ||
public String execute(Model model) throws CommandException { | ||
if (!model.canUndo()) { | ||
throw new CommandException(MESSAGE_UNDO_EXCEPTION); | ||
} | ||
|
||
model.undo(); | ||
return MESSAGE_SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/seedu/address/model/exceptions/AddressBookUndoException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package seedu.address.model.exceptions; | ||
|
||
/** | ||
* Signals that there are no more states to undo to. | ||
*/ | ||
public class AddressBookUndoException extends RuntimeException { | ||
public AddressBookUndoException() { | ||
super("There are no previous AddressBook states to return to."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/test/java/seedu/address/logic/commands/UndoCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; | ||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; | ||
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; | ||
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.model.AddressBook; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.UserPrefs; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.testutil.PersonBuilder; | ||
|
||
public class UndoCommandTest { | ||
|
||
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
|
||
@Test | ||
public void execute_multipleInvalidUndo_throwsCommandException() { | ||
UndoCommand undoCommand = new UndoCommand(); | ||
|
||
String expectedMessage = UndoCommand.MESSAGE_UNDO_EXCEPTION; | ||
|
||
assertCommandFailure(undoCommand, model, expectedMessage); | ||
assertCommandFailure(undoCommand, model, expectedMessage); | ||
assertCommandFailure(undoCommand, model, expectedMessage); | ||
} | ||
|
||
@Test | ||
public void execute_oneUndo_success() { | ||
UndoCommand undoCommand = new UndoCommand(); | ||
|
||
String expectedMessage = UndoCommand.MESSAGE_SUCCESS; | ||
|
||
Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); | ||
model.deletePerson(personToDelete); | ||
ModelManager expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
|
||
assertCommandSuccess(undoCommand, model, expectedMessage, expectedModel); | ||
} | ||
|
||
@Test | ||
public void execute_multipleUndo_success() { | ||
UndoCommand undoCommand = new UndoCommand(); | ||
|
||
String expectedMessage = UndoCommand.MESSAGE_SUCCESS; | ||
ModelManager expectedModelBase = new ModelManager(getTypicalAddressBook(), new UserPrefs()); | ||
|
||
// delete | ||
Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); | ||
ModelManager expectedModelDelete = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); | ||
expectedModelDelete.deletePerson(personToDelete); | ||
model.deletePerson(personToDelete); | ||
|
||
// edit | ||
Person editedPerson = new PersonBuilder().build(); | ||
ModelManager expectedModelEdit = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); | ||
expectedModelEdit.setPerson(model.getFilteredPersonList().get(0), editedPerson); | ||
editedPerson = new PersonBuilder().build(); | ||
model.setPerson(model.getFilteredPersonList().get(0), editedPerson); | ||
|
||
// clear | ||
model.setAddressBook(new AddressBook()); | ||
|
||
// undo clear | ||
assertCommandSuccess(undoCommand, model, expectedMessage, expectedModelEdit); | ||
// undo edit | ||
assertCommandSuccess(undoCommand, model, expectedMessage, expectedModelDelete); | ||
// undo delete | ||
assertCommandSuccess(undoCommand, model, expectedMessage, expectedModelBase); | ||
// no more to undo | ||
assertCommandFailure(undoCommand, model, UndoCommand.MESSAGE_UNDO_EXCEPTION); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.