Skip to content

Commit

Permalink
Add undo redo
Browse files Browse the repository at this point in the history
  • Loading branch information
nixonwidjaja committed Oct 20, 2023
1 parent 8c0305e commit a176b70
Show file tree
Hide file tree
Showing 21 changed files with 190 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public CommandResult execute(String commandText) throws CommandException, ParseE

CommandResult commandResult;
Command command = addressBookParser.parseCommand(commandText);
commandResult = command.execute(model);
commandResult = command.execute(model, commandText);

try {
storage.saveAddressBook(model.getAddressBook());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ public AddCommand(Person person) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, String commandText) throws CommandException {
requireNonNull(model);

if (model.hasPerson(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

model.addPerson(toAdd);
model.addCommandText(commandText);
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Predicate<Person> getPredicate() {
}

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model, String commandText) {
requireNonNull(model);
model.updateFilteredPersonList(predicate);
if (model.getFilteredPersonList().isEmpty()) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/ClaimCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ClaimCommand(Index index, Boolean isSubtract, long amount) {
* @throws CommandException Exception thrown if index input from HR is beyond the pre-existing max list index.
*/
@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, String commandText) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

Expand All @@ -65,6 +65,7 @@ public CommandResult execute(Model model) throws CommandException {
Person editedPerson = postClaimPerson(personToEdit, claimBudget);

model.setPerson(personToEdit, editedPerson);
model.addCommandText(commandText);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

return new CommandResult(String.format("%s Remaining claim %s has: %s",
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public class ClearCommand extends Command {


@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model, String commandText) {
requireNonNull(model);
model.setAddressBook(new AddressBook());
model.addCommandText(commandText);
return new CommandResult(MESSAGE_SUCCESS);
}
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public abstract class Command {
* @return feedback message of the operation result for display
* @throws CommandException If an error occurs during command execution.
*/
public abstract CommandResult execute(Model model) throws CommandException;
public abstract CommandResult execute(Model model, String commandText) throws CommandException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DeleteCommand(Index targetIndex) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, String commandText) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

Expand All @@ -42,6 +42,7 @@ public CommandResult execute(Model model) throws CommandException {

Person personToDelete = lastShownList.get(targetIndex.getZeroBased());
model.deletePerson(personToDelete);
model.addCommandText(commandText);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, personToDelete.getName()));
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, String commandText) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

Expand All @@ -89,6 +89,7 @@ public CommandResult execute(Model model) throws CommandException {
}

model.setPerson(personToEdit, editedPerson);
model.addCommandText(commandText);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ExitCommand extends Command {
public static final String MESSAGE_EXIT_ACKNOWLEDGEMENT = "Exiting Address Book as requested ...";

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model, String commandText) {
return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public FindCommand(NameContainsKeywordsPredicate predicate) {
}

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model, String commandText) {
requireNonNull(model);
model.updateFilteredPersonList(predicate);
return new CommandResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class HelpCommand extends Command {
public static final String SHOWING_HELP_MESSAGE = "Opened help window.";

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model, String commandText) {
return new CommandResult(SHOWING_HELP_MESSAGE, true, false);
}
}
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/LeaveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public LeaveCommand(Index index, String change) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, String commandText) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

Expand All @@ -82,6 +82,7 @@ public CommandResult execute(Model model) throws CommandException {
oldPerson.getDepartment(), oldPerson.getDob(), newLeave);

model.setPerson(oldPerson, newPerson);
model.addCommandText(commandText);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_LEAVE_SUCCESS, newPerson.getName(), newLeave.toString()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ListCommand(Predicate<Person> predicate) {
}

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model, String commandText) {
requireNonNull(model);
model.updateFilteredPersonList(predicate);
return new CommandResult(String.format(message, model.getFilteredPersonList().size()));
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/seedu/address/logic/commands/RedoCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

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

/**
* Redo the most recent command that was undone.
*/
public class RedoCommand extends Command {

public static final String COMMAND_WORD = "redo";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Redo the most recent command that was undone.";

private static final String MESSAGE_SUCCESS =
"The last command that modified the employee list has been redone!\n"
+ "Successfully redone the following command: %1$s";

@Override
public CommandResult execute(Model model, String cmd) throws CommandException {
requireNonNull(model);
try {
String commandText = model.redo();
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_SUCCESS, commandText));
} catch (IllegalArgumentException e) {
throw new CommandException(e.getMessage());
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/seedu/address/logic/commands/UndoCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

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

/**
* Undo the most recent command that modified the employee list.
*/
public class UndoCommand extends Command {

public static final String COMMAND_WORD = "undo";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Undo the most recent command that modified the employee list.";

private static final String MESSAGE_SUCCESS =
"The last command that modified the employee list has been undone!\n"
+ "Successfully undone the following command: %1$s";

@Override
public CommandResult execute(Model model, String cmd) throws CommandException {
requireNonNull(model);
try {
String commandText = model.undo();
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_SUCCESS, commandText));
} catch (IllegalArgumentException e) {
throw new CommandException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ViewCommand(HashMap<String, List<Index>> references) {
* @throws CommandException Exception thrown if index input from HR is beyond the pre-existing max list index.
*/
@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, String commandText) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ViewLeaveCommand(Predicate<Person> combinedPredicate) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
public CommandResult execute(Model model, String commandText) throws CommandException {
requireNonNull(model);
model.updateFilteredPersonList(combinedPredicate);
return new CommandResult(String.format(MESSAGE, model.getFilteredPersonList().size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.LeaveCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.RedoCommand;
import seedu.address.logic.commands.UndoCommand;
import seedu.address.logic.commands.ViewCommand;
import seedu.address.logic.commands.ViewLeaveCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -95,6 +97,13 @@ public Command parseCommand(String userInput) throws ParseException {

case BirthdayCommand.COMMAND_WORD:
return new BirthdayCommandParser().parse(arguments);

case UndoCommand.COMMAND_WORD:
return new UndoCommand();

case RedoCommand.COMMAND_WORD:
return new RedoCommand();

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/seedu/address/model/AddressBookList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package seedu.address.model;

import static seedu.address.commons.util.AppUtil.checkArgument;

import java.util.ArrayList;

public class AddressBookList extends ArrayList<AddressBook> {
private static final String REDO_ERROR_MESSAGE = "There is no command to redo! "
+ "The command to be redone need to previously modified the employee list.";
private static final String UNDO_ERROR_MESSAGE = "There is no command to undo! "
+ "The command to be undone need to previously modified the employee list.";

private ArrayList<String> pastCommands = new ArrayList<String>();
private int index;

public AddressBookList() {
super();
index = -1;
}

@Override
public boolean add(AddressBook addressBook) {
index++;
if (index < this.size()) {
this.removeRange(index, this.size());
while (this.pastCommands.size() >= index) {
this.pastCommands.remove(pastCommands.size() - 1);
}
}
return super.add(addressBook);
}

public AddressBook getAddressBook() {
return super.get(index);
}

public AddressBook undo() {
checkArgument(index > 0, UNDO_ERROR_MESSAGE);
index--;
return super.get(index);
}

public AddressBook redo() {
checkArgument(index < super.size() - 1, REDO_ERROR_MESSAGE);
index++;
return super.get(index);
}

public void addCommandText(String commandText) {
this.pastCommands.add(commandText);
}

public String undoPastCommand() {
return this.pastCommands.get(index);
}

public String redoPastCommand() {
return this.pastCommands.get(index - 1);
}
}
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,10 @@ public interface Model {
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredPersonList(Predicate<Person> predicate);

String undo();

String redo();

void addCommandText(String commandText);
}
Loading

0 comments on commit a176b70

Please sign in to comment.