diff --git a/src/main/java/seedu/address/logic/Messages.java b/src/main/java/seedu/address/logic/Messages.java index 0b83dff1128..6bf12737c91 100644 --- a/src/main/java/seedu/address/logic/Messages.java +++ b/src/main/java/seedu/address/logic/Messages.java @@ -19,8 +19,8 @@ public class Messages { public static final String MESSAGE_INVALID_DEVELOPER_DISPLAYED_INDEX = "The developer index provided is invalid!"; public static final String MESSAGE_INVALID_CLIENT_DISPLAYED_INDEX = "The client index provided is invalid!"; public static final String MESSAGE_INVALID_PROJECT_DISPLAYED_INDEX = "The project index provided is invalid!"; - public static final String MESSAGE_INAPPLICABLE_PREFIX_USED = "You tried to edit an inapplicable field! Please check " + - "the prefixes used and try again. \n%1$s"; + public static final String MESSAGE_INAPPLICABLE_PREFIX_USED = "You tried to edit an inapplicable field! " + + "Please check the prefixes used and try again. \n%1$s"; public static final String MESSAGE_NONEXISTENT_PROJECT = "There is no existing Project with the name: %1$s!"; public static String getMessageDevelopersListedOverview(int count) { return count == 1 diff --git a/src/main/java/seedu/address/logic/commands/ClearCommand.java b/src/main/java/seedu/address/logic/commands/ClearCommand.java index c22182beb73..64eb626165d 100644 --- a/src/main/java/seedu/address/logic/commands/ClearCommand.java +++ b/src/main/java/seedu/address/logic/commands/ClearCommand.java @@ -18,6 +18,6 @@ public class ClearCommand extends Command { public CommandResult execute(Model model) { requireNonNull(model); model.setAddressBook(new AddressBook()); - return new CommandResult(MESSAGE_SUCCESS,TabIndex.Developer); + return new CommandResult(MESSAGE_SUCCESS, TabIndex.Developer); } } diff --git a/src/main/java/seedu/address/logic/commands/DeleteDeveloperCommand.java b/src/main/java/seedu/address/logic/commands/DeleteDeveloperCommand.java index ae1e9d08564..ae24d0c8dd4 100644 --- a/src/main/java/seedu/address/logic/commands/DeleteDeveloperCommand.java +++ b/src/main/java/seedu/address/logic/commands/DeleteDeveloperCommand.java @@ -42,6 +42,7 @@ public CommandResult execute(Model model) throws CommandException { Developer developerToDelete = lastShownList.get(targetIndex.getZeroBased()); model.deleteDeveloper(developerToDelete); + model.commitAddressBook(model); return new CommandResult(String.format(MESSAGE_DELETE_DEVELOPER_SUCCESS, Messages.format(developerToDelete)),TabIndex.Developer); } diff --git a/src/main/java/seedu/address/logic/commands/ExitCommand.java b/src/main/java/seedu/address/logic/commands/ExitCommand.java index 31e53fc06bf..dfe2b088274 100644 --- a/src/main/java/seedu/address/logic/commands/ExitCommand.java +++ b/src/main/java/seedu/address/logic/commands/ExitCommand.java @@ -13,7 +13,7 @@ public class ExitCommand extends Command { @Override public CommandResult execute(Model model) { - return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true,TabIndex.Developer); + return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true, TabIndex.Developer); } } diff --git a/src/main/java/seedu/address/logic/commands/RedoCommand.java b/src/main/java/seedu/address/logic/commands/RedoCommand.java new file mode 100644 index 00000000000..6c5a9eb4b19 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/RedoCommand.java @@ -0,0 +1,18 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; + +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; + +public class RedoCommand extends Command { + public static final String COMMAND_WORD = "redo"; + public static final String MESSAGE_SUCCESS = "Redo successful!"; + + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + model.redoAddressBook(model); + return new CommandResult(MESSAGE_SUCCESS, TabIndex.Developer); + } +} diff --git a/src/main/java/seedu/address/logic/commands/TabIndex.java b/src/main/java/seedu/address/logic/commands/TabIndex.java index 4571714f755..423d79c2155 100644 --- a/src/main/java/seedu/address/logic/commands/TabIndex.java +++ b/src/main/java/seedu/address/logic/commands/TabIndex.java @@ -3,5 +3,5 @@ public enum TabIndex { Developer, Client, - Project + developers, Project } diff --git a/src/main/java/seedu/address/logic/commands/UndoCommand.java b/src/main/java/seedu/address/logic/commands/UndoCommand.java new file mode 100644 index 00000000000..2305fce5774 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/UndoCommand.java @@ -0,0 +1,18 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; + +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.AddressBook; +import seedu.address.model.Model; + +public class UndoCommand extends Command { + public static final String COMMAND_WORD = "undo"; + public static final String MESSAGE_SUCCESS = "Undo successful!"; + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + model.undoAddressBook(model); + return new CommandResult(MESSAGE_SUCCESS, TabIndex.Developer); + } +} diff --git a/src/main/java/seedu/address/logic/commands/add/AddClientCommand.java b/src/main/java/seedu/address/logic/commands/add/AddClientCommand.java index 42c988a2ab9..1638bd1d8b9 100644 --- a/src/main/java/seedu/address/logic/commands/add/AddClientCommand.java +++ b/src/main/java/seedu/address/logic/commands/add/AddClientCommand.java @@ -71,6 +71,7 @@ public CommandResult execute(Model model) throws CommandException { } model.addClient(toAdd); + model.commitAddressBook(model); return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)), TabIndex.Client); } diff --git a/src/main/java/seedu/address/logic/commands/add/AddDeveloperCommand.java b/src/main/java/seedu/address/logic/commands/add/AddDeveloperCommand.java index 9a654a3de22..14c9464e9c8 100644 --- a/src/main/java/seedu/address/logic/commands/add/AddDeveloperCommand.java +++ b/src/main/java/seedu/address/logic/commands/add/AddDeveloperCommand.java @@ -73,6 +73,7 @@ public CommandResult execute(Model model) throws CommandException { } model.addDeveloper(toAdd); + model.commitAddressBook(model); return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)), TabIndex.Developer); } diff --git a/src/main/java/seedu/address/logic/commands/add/AddProjectCommand.java b/src/main/java/seedu/address/logic/commands/add/AddProjectCommand.java index 21c13daa612..7be980d4f40 100644 --- a/src/main/java/seedu/address/logic/commands/add/AddProjectCommand.java +++ b/src/main/java/seedu/address/logic/commands/add/AddProjectCommand.java @@ -55,6 +55,7 @@ public CommandResult execute(Model model) throws CommandException { } model.addProject(toAdd); + model.commitAddressBook(model); return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)), TabIndex.Project); } diff --git a/src/main/java/seedu/address/logic/commands/edit/EditClientCommand.java b/src/main/java/seedu/address/logic/commands/edit/EditClientCommand.java index 6ed7ab093b3..6f4e5d3fb6d 100644 --- a/src/main/java/seedu/address/logic/commands/edit/EditClientCommand.java +++ b/src/main/java/seedu/address/logic/commands/edit/EditClientCommand.java @@ -105,6 +105,7 @@ public CommandResult execute(Model model) throws CommandException { model.setClient(clientToEdit, editedClient); model.updateFilteredClientList(Model.PREDICATE_SHOW_ALL_CLIENTS); + model.commitAddressBook(model); return new CommandResult(String.format(MESSAGE_EDIT_CLIENT_SUCCESS, Messages.format(editedClient)), TabIndex.Client); } diff --git a/src/main/java/seedu/address/logic/commands/edit/EditDeveloperCommand.java b/src/main/java/seedu/address/logic/commands/edit/EditDeveloperCommand.java index 227e99d7b3f..d2d0eea74bd 100644 --- a/src/main/java/seedu/address/logic/commands/edit/EditDeveloperCommand.java +++ b/src/main/java/seedu/address/logic/commands/edit/EditDeveloperCommand.java @@ -112,6 +112,7 @@ public CommandResult execute(Model model) throws CommandException { model.setDeveloper(developerToEdit, editedDeveloper); model.updateFilteredDeveloperList(Model.PREDICATE_SHOW_ALL_DEVELOPERS); + model.commitAddressBook(model); return new CommandResult(String.format(MESSAGE_EDIT_DEVELOPER_SUCCESS, Messages.format(editedDeveloper)), TabIndex.Developer); } diff --git a/src/main/java/seedu/address/logic/commands/edit/EditProjectCommand.java b/src/main/java/seedu/address/logic/commands/edit/EditProjectCommand.java index d0ebf5466e8..52e8be928ef 100644 --- a/src/main/java/seedu/address/logic/commands/edit/EditProjectCommand.java +++ b/src/main/java/seedu/address/logic/commands/edit/EditProjectCommand.java @@ -94,6 +94,7 @@ public CommandResult execute(Model model) throws CommandException { model.setProject(projectToEdit, editedProject); model.updateFilteredProjectList(Model.PREDICATE_SHOW_ALL_PROJECTS); + model.commitAddressBook(model); return new CommandResult(String.format(MESSAGE_EDIT_PROJECT_SUCCESS, Messages.format(editedProject)), TabIndex.Project); } diff --git a/src/main/java/seedu/address/logic/commands/find/FindClientCommand.java b/src/main/java/seedu/address/logic/commands/find/FindClientCommand.java index bdd239b821f..a6bd4b89226 100644 --- a/src/main/java/seedu/address/logic/commands/find/FindClientCommand.java +++ b/src/main/java/seedu/address/logic/commands/find/FindClientCommand.java @@ -72,4 +72,5 @@ public String toString() { .add("predicate", predicate) .toString(); } -} \ No newline at end of file +} + diff --git a/src/main/java/seedu/address/logic/commands/find/FindDeveloperCommand.java b/src/main/java/seedu/address/logic/commands/find/FindDeveloperCommand.java index 40495b252e5..f80489ca869 100644 --- a/src/main/java/seedu/address/logic/commands/find/FindDeveloperCommand.java +++ b/src/main/java/seedu/address/logic/commands/find/FindDeveloperCommand.java @@ -1,6 +1,7 @@ package seedu.address.logic.commands.find; import static java.util.Objects.requireNonNull; + import static seedu.address.logic.Messages.getMessageDevelopersListedOverview; import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; import static seedu.address.logic.parser.CliSyntax.PREFIX_DATEJOINED; @@ -16,6 +17,7 @@ import java.util.function.Predicate; import seedu.address.commons.util.ToStringBuilder; + import seedu.address.logic.commands.Command; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.TabIndex; @@ -76,4 +78,5 @@ public String toString() { .add("predicate", predicate) .toString(); } -} \ No newline at end of file +} + diff --git a/src/main/java/seedu/address/logic/commands/find/FindProjectCommand.java b/src/main/java/seedu/address/logic/commands/find/FindProjectCommand.java index 3059a62dc61..5545af6f7ed 100644 --- a/src/main/java/seedu/address/logic/commands/find/FindProjectCommand.java +++ b/src/main/java/seedu/address/logic/commands/find/FindProjectCommand.java @@ -1,6 +1,7 @@ package seedu.address.logic.commands.find; import static java.util.Objects.requireNonNull; + import static seedu.address.logic.Messages.getMessageProjectsListedOverview; import static seedu.address.logic.parser.CliSyntax.PREFIX_DEADLINE; import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION; diff --git a/src/main/java/seedu/address/logic/commands/ListClientCommand.java b/src/main/java/seedu/address/logic/commands/list/ListClientCommand.java similarity index 78% rename from src/main/java/seedu/address/logic/commands/ListClientCommand.java rename to src/main/java/seedu/address/logic/commands/list/ListClientCommand.java index 91de72b4f03..2351487cf9d 100644 --- a/src/main/java/seedu/address/logic/commands/ListClientCommand.java +++ b/src/main/java/seedu/address/logic/commands/list/ListClientCommand.java @@ -1,8 +1,11 @@ -package seedu.address.logic.commands; +package seedu.address.logic.commands.list; import static java.util.Objects.requireNonNull; import static seedu.address.model.Model.PREDICATE_SHOW_ALL_CLIENTS; +import seedu.address.logic.commands.Command; +import seedu.address.logic.commands.CommandResult; +import seedu.address.logic.commands.TabIndex; import seedu.address.model.Model; /** diff --git a/src/main/java/seedu/address/logic/commands/ListDeveloperCommand.java b/src/main/java/seedu/address/logic/commands/list/ListDeveloperCommand.java similarity index 79% rename from src/main/java/seedu/address/logic/commands/ListDeveloperCommand.java rename to src/main/java/seedu/address/logic/commands/list/ListDeveloperCommand.java index 1c44ca87385..9ffa6d489dc 100644 --- a/src/main/java/seedu/address/logic/commands/ListDeveloperCommand.java +++ b/src/main/java/seedu/address/logic/commands/list/ListDeveloperCommand.java @@ -1,8 +1,11 @@ -package seedu.address.logic.commands; +package seedu.address.logic.commands.list; import static java.util.Objects.requireNonNull; import static seedu.address.model.Model.PREDICATE_SHOW_ALL_DEVELOPERS; +import seedu.address.logic.commands.Command; +import seedu.address.logic.commands.CommandResult; +import seedu.address.logic.commands.TabIndex; import seedu.address.model.Model; /** diff --git a/src/main/java/seedu/address/logic/commands/ListProjectCommand.java b/src/main/java/seedu/address/logic/commands/list/ListProjectCommand.java similarity index 79% rename from src/main/java/seedu/address/logic/commands/ListProjectCommand.java rename to src/main/java/seedu/address/logic/commands/list/ListProjectCommand.java index 4d68948ddb7..e07bdea0b27 100644 --- a/src/main/java/seedu/address/logic/commands/ListProjectCommand.java +++ b/src/main/java/seedu/address/logic/commands/list/ListProjectCommand.java @@ -1,8 +1,11 @@ -package seedu.address.logic.commands; +package seedu.address.logic.commands.list; import static java.util.Objects.requireNonNull; import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PROJECTS; +import seedu.address.logic.commands.Command; +import seedu.address.logic.commands.CommandResult; +import seedu.address.logic.commands.TabIndex; import seedu.address.model.Model; /** diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index 0e149e43791..f2831952156 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -8,6 +8,8 @@ import java.util.regex.Pattern; import seedu.address.commons.core.LogsCenter; +import seedu.address.logic.commands.RedoCommand; +import seedu.address.logic.commands.UndoCommand; import seedu.address.logic.commands.ClearCommand; import seedu.address.logic.commands.Command; import seedu.address.logic.commands.DeleteDeveloperCommand; @@ -17,9 +19,9 @@ import seedu.address.logic.commands.find.FindProjectCommand; import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.ImportCommand; -import seedu.address.logic.commands.ListClientCommand; -import seedu.address.logic.commands.ListDeveloperCommand; -import seedu.address.logic.commands.ListProjectCommand; +import seedu.address.logic.commands.list.ListClientCommand; +import seedu.address.logic.commands.list.ListDeveloperCommand; +import seedu.address.logic.commands.list.ListProjectCommand; import seedu.address.logic.commands.add.AddClientCommand; import seedu.address.logic.commands.add.AddDeveloperCommand; import seedu.address.logic.commands.add.AddProjectCommand; @@ -35,8 +37,10 @@ import seedu.address.logic.parser.edit.EditDeveloperCommandParser; import seedu.address.logic.parser.edit.EditProjectCommandParser; import seedu.address.logic.parser.exceptions.ParseException; + import seedu.address.logic.parser.imports.ImportClientCommandParser; import seedu.address.logic.parser.imports.ImportDeveloperCommandParser; + import seedu.address.logic.parser.find.FindClientCommandParser; import seedu.address.logic.parser.find.FindDeveloperCommandParser; import seedu.address.logic.parser.find.FindProjectCommandParser; @@ -108,7 +112,7 @@ public Command parseCommand(String userInput) throws ParseException { case FindProjectCommand.COMMAND_WORD: return new FindProjectCommandParser().parse(arguments); - + case ListClientCommand.COMMAND_WORD: return new ListClientCommand(); @@ -118,6 +122,12 @@ public Command parseCommand(String userInput) throws ParseException { case ListProjectCommand.COMMAND_WORD: return new ListProjectCommand(); + case UndoCommand.COMMAND_WORD: + return new UndoCommand(); + + case RedoCommand.COMMAND_WORD: + return new RedoCommand(); + case ExitCommand.COMMAND_WORD: return new ExitCommand(); diff --git a/src/main/java/seedu/address/logic/parser/find/FindClientCommandParser.java b/src/main/java/seedu/address/logic/parser/find/FindClientCommandParser.java index eadc2ee5ea9..14d6ddd30c3 100644 --- a/src/main/java/seedu/address/logic/parser/find/FindClientCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/find/FindClientCommandParser.java @@ -1,6 +1,7 @@ package seedu.address.logic.parser.find; import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; + import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; import static seedu.address.logic.parser.CliSyntax.PREFIX_DOCUMENT; import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; @@ -99,7 +100,6 @@ private Predicate buildPredicate(ArgumentMultimap argMultimap) { String[] organisationKeywords = argMultimap.getValue(PREFIX_ORGANISATION).get().split("\\s+"); finalPredicate = finalPredicate.and(new OrganisationContainsKeywordsPredicate(Arrays.asList(organisationKeywords))); } - return finalPredicate; } } \ No newline at end of file diff --git a/src/main/java/seedu/address/logic/parser/find/FindDeveloperCommandParser.java b/src/main/java/seedu/address/logic/parser/find/FindDeveloperCommandParser.java index 617c8735772..235107890f1 100644 --- a/src/main/java/seedu/address/logic/parser/find/FindDeveloperCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/find/FindDeveloperCommandParser.java @@ -1,6 +1,7 @@ package seedu.address.logic.parser.find; import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; + import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; import static seedu.address.logic.parser.CliSyntax.PREFIX_DATEJOINED; import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; diff --git a/src/main/java/seedu/address/logic/parser/find/FindProjectCommandParser.java b/src/main/java/seedu/address/logic/parser/find/FindProjectCommandParser.java index 29caf9accc8..62b3ad997ee 100644 --- a/src/main/java/seedu/address/logic/parser/find/FindProjectCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/find/FindProjectCommandParser.java @@ -11,17 +11,21 @@ import seedu.address.logic.commands.find.FindProjectCommand; import seedu.address.logic.parser.ArgumentMultimap; import seedu.address.logic.parser.ArgumentTokenizer; + import seedu.address.logic.parser.Parser; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.project.DeadlineContainsKeywordsPredicate; import seedu.address.model.project.DescriptionContainsKeywordsPredicate; + import seedu.address.model.project.Project; + import seedu.address.model.project.ProjectNameContainsKeywordsPredicate; /** * Parses input arguments and creates a new FindProjectCommand object */ public class FindProjectCommandParser implements Parser { + public FindProjectCommand parse(String args) throws ParseException { String trimmedArgs = args.trim(); if (trimmedArgs.isEmpty()) { diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 324f51f2464..e0840e88edc 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -5,6 +5,7 @@ import javafx.collections.ObservableList; import seedu.address.commons.core.GuiSettings; +import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.client.Client; import seedu.address.model.developer.Developer; @@ -100,4 +101,7 @@ public interface Model { void updateFilteredDeveloperList(Predicate predicate); void updateFilteredClientList(Predicate predicate); void updateFilteredProjectList(Predicate predicate); + void commitAddressBook(Model model); + void undoAddressBook(Model model) throws CommandException; + void redoAddressBook(Model model) throws CommandException; } diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index fdee63d5270..fca216b28fd 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.client.Client; import seedu.address.model.developer.Developer; @@ -25,6 +26,7 @@ public class ModelManager implements Model { private final FilteredList filteredDevelopers; private final FilteredList filteredClients; private final FilteredList filteredProjects; + private final VersionedAddressBook versionedAddressBook; /** * Initializes a ModelManager with the given addressBook and userPrefs. @@ -39,6 +41,7 @@ public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyUserPrefs userPrefs filteredDevelopers = new FilteredList<>(this.addressBook.getDeveloperList()); filteredClients = new FilteredList<>(this.addressBook.getClientList()); filteredProjects = new FilteredList<>(this.addressBook.getProjectList()); + versionedAddressBook = new VersionedAddressBook(this.addressBook); } @@ -117,9 +120,9 @@ public void setDeveloper(Developer target, Developer editedDeveloper) { addressBook.setDeveloper(target, editedDeveloper); } -// Similarly, create methods for Client and Project + // Similarly, create methods for Client and Project -//=========== Filtered Developer List Accessors ============================================================= + //=========== Filtered Developer List Accessors ============================================================= /** * Returns an unmodifiable view of the list of {@code Developer} backed by the internal list of @@ -155,11 +158,10 @@ public void addClient(Client client) { @Override public void setClient(Client target, Client editedClient) { requireAllNonNull(target, editedClient); - addressBook.setClient(target, editedClient); } -//=========== Filtered Client List Accessors ============================================================= + //=========== Filtered Client List Accessors ============================================================= /** * Returns an unmodifiable view of the list of {@code Client} backed by the internal list of @@ -199,7 +201,7 @@ public void setProject(seedu.address.model.project.Project target, seedu.address addressBook.setProject(target, editedProject); } -//=========== Filtered Project List Accessors ============================================================= + //=========== Filtered Project List Accessors ============================================================= /** * Returns an unmodifiable view of the list of {@code Project} backed by the internal list of @@ -216,6 +218,22 @@ public void updateFilteredProjectList(Predicate addressBookStateList; + private ReadOnlyAddressBook initialAddressBook; + private int currentStatePointer; + public static final String INVALID_REDO_COMMAND = "You have reached the last step, unable to redo"; + public static final String INVALID_UNDO_COMMAND = "You have reached the first step, unable to undo"; + + public VersionedAddressBook(ReadOnlyAddressBook initialAddressBook) { + addressBookStateList = new ArrayList<>(); + addressBookStateList.add(new AddressBook(initialAddressBook)); + currentStatePointer = 0; + } + + public void commit(Model model) { + if (currentStatePointer < addressBookStateList.size() - 1) { + // If we're not at the latest state, remove the states after the current one + addressBookStateList.subList(currentStatePointer + 1, addressBookStateList.size()).clear(); + } + + // Add the current state to the history + AddressBook updatedAddressBook = new AddressBook(model.getAddressBook()); + addressBookStateList.add(updatedAddressBook); + currentStatePointer++; + } + + public void undo(Model model) throws CommandException { + if (canUndo()) { + currentStatePointer--; + changeAddressBook(model); + } else { + throw new CommandException(INVALID_UNDO_COMMAND); + } + } + + public void redo(Model model) throws CommandException { + if (canRedo()) { + currentStatePointer++; + changeAddressBook(model); + } else { + throw new CommandException(INVALID_REDO_COMMAND); + } + + } + + public boolean canUndo() { + return currentStatePointer > 0; + } + + public boolean canRedo() { + return currentStatePointer < addressBookStateList.size() - 1; + } + + public AddressBook getCurrentState() { + return addressBookStateList.get(currentStatePointer); + } + public void changeAddressBook(Model model) { + AddressBook newAddressBook = addressBookStateList.get(currentStatePointer); + model.setAddressBook(newAddressBook); + } +} + diff --git a/src/test/java/seedu/address/logic/LogicManagerTest.java b/src/test/java/seedu/address/logic/LogicManagerTest.java index 7cbe7c3a63b..7244959ddd5 100644 --- a/src/test/java/seedu/address/logic/LogicManagerTest.java +++ b/src/test/java/seedu/address/logic/LogicManagerTest.java @@ -20,7 +20,7 @@ import seedu.address.logic.commands.add.AddDeveloperCommand; import seedu.address.logic.commands.CommandResult; -import seedu.address.logic.commands.ListClientCommand; +import seedu.address.logic.commands.list.ListClientCommand; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.Model; diff --git a/src/test/java/seedu/address/logic/commands/ListClientCommandTest.java b/src/test/java/seedu/address/logic/commands/ListClientCommandTest.java index 1ccd47113bd..71297755869 100644 --- a/src/test/java/seedu/address/logic/commands/ListClientCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ListClientCommandTest.java @@ -8,6 +8,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import seedu.address.logic.commands.list.ListClientCommand; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; diff --git a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java index 8263ce92fb6..6e25d0e3a3e 100644 --- a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java @@ -19,7 +19,7 @@ import seedu.address.logic.commands.ExitCommand; import seedu.address.logic.commands.find.FindDeveloperCommand; import seedu.address.logic.commands.HelpCommand; -import seedu.address.logic.commands.ListClientCommand; +import seedu.address.logic.commands.list.ListClientCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.developer.NameDeveloperContainsKeywordsPredicate; import seedu.address.model.developer.Developer;