Skip to content

Commit

Permalink
Merge pull request #69 from joellow88/add-undo-command
Browse files Browse the repository at this point in the history
Update undo functionality
  • Loading branch information
joellow88 authored Mar 17, 2023
2 parents ac8973e + 7b2c0c8 commit 2a0e628
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 44 deletions.
41 changes: 30 additions & 11 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,23 @@ Examples:
* `filter t/family t/friends t/classmates` returns `3 contacts listed!`
* ![filterByTags](images/filter/filterByTagsResult.png)


### Undo past commands

Undoes previous commands that modified ModCheck.
Undo will only undo commands that have successfully modified the data in ModCheck. For example, a successful `add`,
`edit`, or `delete` command can be undone by the undo command.
Any commands that does not modify the data in ModCheck will NOT be undone. This includes `view`, `find`, and other
similar commands. Any command that would have modified the data in ModCheck, but was unsuccessful in doing so (eg:
`add` duplicate person), will NOT be undone.

Chaining of a few undo commands is supported. Once the undo limit has been reached, the error message `No command to
undo!` will be shown.

Format: `undo`

Use `redo` to reapply the changes undone by undo.

--------------------------------------------------------------------------------------------------------------------

## FAQ
Expand All @@ -247,14 +264,16 @@ Examples:

## Command summary

Action | Format, Examples
--------|------------------
**Add** | `add n/NAME p/PHONE_NUMBER e/EMAIL a/ADDRESS [t/TAG]…​` <br> e.g., `add n/James Ho p/22224444 e/[email protected] a/123, Clementi Rd, 1234665 t/friend t/colleague`
**View** | `view INDEX`<br> e.g., `view 2`
**Clear** | `clear`
**Delete** | `delete INDEX`<br> e.g., `delete 3`
**Edit** | `edit INDEX [n/NAME] [p/PHONE_NUMBER] [e/EMAIL] [a/ADDRESS] [t/TAG]…​`<br> e.g.,`edit 2 n/James Lee e/[email protected]`
**Find** | `find KEYWORD [MORE_KEYWORDS]`<br> e.g., `find James Jake`
**List** | `list`
**Help** | `help`
**Filter** | `filter n/NAME` <br> `filter p/PHONE_NUMBER`<br> `filter e/EMAIL_ADDRESS` <br> `filter d/DESCRIPTION` <br> `filter t/TAG` <br> e.g. `filter n/Alex` <br> e.g. `filter p/91031282` <br> e.g. `filter e/[email protected]` <br> e.g. `filter d/helpful` <br> e.g. `filter t/family` <br>
| Action | Format, Examples |
|------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **Add** | `add n/NAME p/PHONE_NUMBER e/EMAIL a/ADDRESS [t/TAG]…​` <br> e.g., `add n/James Ho p/22224444 e/[email protected] a/123, Clementi Rd, 1234665 t/friend t/colleague` |
| **View** | `view INDEX`<br> e.g., `view 2` |
| **Clear** | `clear` |
| **Delete** | `delete INDEX`<br> e.g., `delete 3` |
| **Edit** | `edit INDEX [n/NAME] [p/PHONE_NUMBER] [e/EMAIL] [a/ADDRESS] [t/TAG]…​`<br> e.g.,`edit 2 n/James Lee e/[email protected]` |
| **Find** | `find KEYWORD [MORE_KEYWORDS]`<br> e.g., `find James Jake` |
| **List** | `list` |
| **Help** | `help` |
| **Filter** | `filter n/NAME` <br> `filter p/PHONE_NUMBER`<br> `filter e/EMAIL_ADDRESS` <br> `filter d/DESCRIPTION` <br> `filter t/TAG` <br> e.g. `filter n/Alex` <br> e.g. `filter p/91031282` <br> e.g. `filter e/[email protected]` <br> e.g. `filter d/helpful` <br> e.g. `filter t/family` <br> |
| **Undo** | `undo` |
| **Redo** | `redo` |
14 changes: 10 additions & 4 deletions src/main/java/seedu/address/logic/commands/RedoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.Undoable;


/**
Expand All @@ -18,11 +19,16 @@ public class RedoCommand extends Command {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
if (!model.hasRedoableCommand()) {
throw new CommandException(MESSAGE_NO_REDOABLE_COMMAND);
if (model instanceof Undoable) {
Undoable undoableModel = (Undoable) model;
if (!undoableModel.hasRedoableCommand()) {
throw new CommandException(MESSAGE_NO_REDOABLE_COMMAND);
}
String returnMessage = undoableModel.executeRedo();
return new CommandResult(String.format(MESSAGE_SUCCESS, returnMessage));
} else {
throw new IllegalArgumentException("Model passed does not support undo!");
}
String returnMessage = model.executeRedo();
return new CommandResult(String.format(MESSAGE_SUCCESS, returnMessage));
}
}

14 changes: 10 additions & 4 deletions src/main/java/seedu/address/logic/commands/UndoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.Undoable;


/**
Expand All @@ -22,11 +23,16 @@ public class UndoCommand extends Command {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
if (!model.hasUndoableCommand()) {
throw new CommandException(MESSAGE_NO_UNDOABLE_COMMAND);
if (model instanceof Undoable) {
Undoable undoableModel = (Undoable) model;
if (!undoableModel.hasUndoableCommand()) {
throw new CommandException(MESSAGE_NO_UNDOABLE_COMMAND);
}
String returnMessage = undoableModel.executeUndo();
return new CommandResult(String.format(MESSAGE_SUCCESS, returnMessage));
} else {
throw new IllegalArgumentException("Model passed does not support undo!");
}
String returnMessage = model.executeUndo();
return new CommandResult(String.format(MESSAGE_SUCCESS, returnMessage));
}
}

22 changes: 0 additions & 22 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,5 @@ public interface Model {
*/
void updateFilteredPersonList(Predicate<Person> predicate);

/**
* Returns if there is an undoable command in model.
* @return True if there is an undoable command in model, false otherwise.
*/
boolean hasUndoableCommand();

/**
* Undoes the changes made by the last modification command used
* @return The string representation of the last modification command used
*/
String executeUndo();
/**
* Returns if there is a redoable command in model.
* @return True if there is a redoable command in model, false otherwise.
*/
boolean hasRedoableCommand();

/**
* Redoes the changes unmade by the last undo command
* @return The string representation of the command redone
*/
String executeRedo();

}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* Represents the in-memory model of the address book data.
*/
public class ModelManager implements Model {
public class ModelManager implements Model, Undoable {
private static final Logger logger = LogsCenter.getLogger(ModelManager.class);
private final AddressBook addressBook;
private final UserPrefs userPrefs;
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/seedu/address/model/Undoable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.address.model;

/**
* The interface for supporting undo and redo functionality.
*/
public interface Undoable {
/**
* Returns if there is an undoable command in model.
* @return True if there is an undoable command in model, false otherwise.
*/
boolean hasUndoableCommand();

/**
* Undoes the changes made by the last modification command used
* @return The string representation of the last modification command used
*/
String executeUndo();
/**
* Returns if there is a redoable command in model.
* @return True if there is a redoable command in model, false otherwise.
*/
boolean hasRedoableCommand();

/**
* Redoes the changes unmade by the last undo command
* @return The string representation of the command redone
*/
String executeRedo();
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public String toString() {
.append(getPhone())
.append("; Email: ")
.append(getEmail())
.append("; Address: ")
.append("; Description: ")
.append(getAddress());

Set<Tag> tags = getTags();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.Undoable;
import seedu.address.model.person.Person;
import seedu.address.testutil.PersonBuilder;

Expand Down Expand Up @@ -77,7 +78,7 @@ public void equals() {
/**
* A default model stub that have all of the methods failing.
*/
private class ModelStub implements Model {
private class ModelStub implements Model, Undoable {
@Override
public void setUserPrefs(ReadOnlyUserPrefs userPrefs) {
throw new AssertionError("This method should not be called.");
Expand Down

0 comments on commit 2a0e628

Please sign in to comment.