From b66a590955d5988bc7d642568ece11f3d136762c Mon Sep 17 00:00:00 2001 From: JJtan <95962077+JJtan2002@users.noreply.github.com> Date: Fri, 18 Oct 2024 23:14:01 +0800 Subject: [PATCH 1/4] Add functions to save and load manually --- src/main/java/seedu/address/MainApp.java | 3 +- .../java/seedu/address/model/UserPrefs.java | 10 +++++ .../address/model/VersionedAddressBook.java | 29 +++++++++++++++ .../address/storage/AddressBookStorage.java | 1 + .../storage/JsonAddressBookStorage.java | 8 +++- .../seedu/address/storage/StorageManager.java | 37 +++++++++++++++++++ .../seedu/address/logic/LogicManagerTest.java | 5 ++- .../storage/JsonAddressBookStorageTest.java | 6 +-- .../address/storage/StorageManagerTest.java | 2 +- 9 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 src/main/java/seedu/address/model/VersionedAddressBook.java diff --git a/src/main/java/seedu/address/MainApp.java b/src/main/java/seedu/address/MainApp.java index 678ddc8c218..dc04b986ff8 100644 --- a/src/main/java/seedu/address/MainApp.java +++ b/src/main/java/seedu/address/MainApp.java @@ -57,7 +57,8 @@ public void init() throws Exception { UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath()); UserPrefs userPrefs = initPrefs(userPrefsStorage); - AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath()); + AddressBookStorage addressBookStorage = + new JsonAddressBookStorage(userPrefs.getAddressBookFilePath(), userPrefs.getManualSaveFilePath()); storage = new StorageManager(addressBookStorage, userPrefsStorage); model = initModelManager(storage, userPrefs); diff --git a/src/main/java/seedu/address/model/UserPrefs.java b/src/main/java/seedu/address/model/UserPrefs.java index 6be655fb4c7..adb38990be4 100644 --- a/src/main/java/seedu/address/model/UserPrefs.java +++ b/src/main/java/seedu/address/model/UserPrefs.java @@ -15,6 +15,7 @@ public class UserPrefs implements ReadOnlyUserPrefs { private GuiSettings guiSettings = new GuiSettings(); private Path addressBookFilePath = Paths.get("data" , "addressbook.json"); + private Path manualSaveFilePath = Paths.get("data", "save.json"); /** * Creates a {@code UserPrefs} with default values. @@ -56,6 +57,15 @@ public void setAddressBookFilePath(Path addressBookFilePath) { this.addressBookFilePath = addressBookFilePath; } + public Path getManualSaveFilePath() { + return manualSaveFilePath; + } + + public void setManualSaveFilePath(Path manualSaveFilePath) { + requireNonNull(manualSaveFilePath); + this.manualSaveFilePath = manualSaveFilePath; + } + @Override public boolean equals(Object other) { if (other == this) { diff --git a/src/main/java/seedu/address/model/VersionedAddressBook.java b/src/main/java/seedu/address/model/VersionedAddressBook.java new file mode 100644 index 00000000000..6e0735de296 --- /dev/null +++ b/src/main/java/seedu/address/model/VersionedAddressBook.java @@ -0,0 +1,29 @@ +package seedu.address.model; + +import java.util.ArrayList; +import java.util.List; + +/** + * {@code AddressBook} that tracks its state across command executions + */ +public class VersionedAddressBook extends AddressBook { + private final List addressBookStates; + private int currentStatePointer; + + /** + * Constructs a {@code VersionedAddressBook} with the initial state of the address book. + * The address book will start with the specified initial state and the current state pointer + * will be set to the initial state. + * + * @param initialState The initial state of the address book to be used as the starting point. + * Cannot be null. + */ + public VersionedAddressBook(ReadOnlyAddressBook initialState) { + super(initialState); + + addressBookStates = new ArrayList<>(); + addressBookStates.add(new AddressBook(initialState)); + currentStatePointer = 0; + } + +} diff --git a/src/main/java/seedu/address/storage/AddressBookStorage.java b/src/main/java/seedu/address/storage/AddressBookStorage.java index f2e015105ae..334a5e1474e 100644 --- a/src/main/java/seedu/address/storage/AddressBookStorage.java +++ b/src/main/java/seedu/address/storage/AddressBookStorage.java @@ -42,4 +42,5 @@ public interface AddressBookStorage { */ void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath) throws IOException; + Path getManualSaveFilePath(); } diff --git a/src/main/java/seedu/address/storage/JsonAddressBookStorage.java b/src/main/java/seedu/address/storage/JsonAddressBookStorage.java index 41e06f264e1..5ea20b27e97 100644 --- a/src/main/java/seedu/address/storage/JsonAddressBookStorage.java +++ b/src/main/java/seedu/address/storage/JsonAddressBookStorage.java @@ -22,15 +22,21 @@ public class JsonAddressBookStorage implements AddressBookStorage { private static final Logger logger = LogsCenter.getLogger(JsonAddressBookStorage.class); private Path filePath; + private Path manualSaveFilePath; - public JsonAddressBookStorage(Path filePath) { + public JsonAddressBookStorage(Path filePath, Path manualSaveFilePath) { this.filePath = filePath; + this.manualSaveFilePath = manualSaveFilePath; } public Path getAddressBookFilePath() { return filePath; } + public Path getManualSaveFilePath() { + return manualSaveFilePath; + } + @Override public Optional readAddressBook() throws DataLoadingException { return readAddressBook(filePath); diff --git a/src/main/java/seedu/address/storage/StorageManager.java b/src/main/java/seedu/address/storage/StorageManager.java index 8b84a9024d5..1cf0633ea08 100644 --- a/src/main/java/seedu/address/storage/StorageManager.java +++ b/src/main/java/seedu/address/storage/StorageManager.java @@ -20,6 +20,9 @@ public class StorageManager implements Storage { private AddressBookStorage addressBookStorage; private UserPrefsStorage userPrefsStorage; + // Path for the manual save/load file + private Path manualSaveFilePath; + /** * Creates a {@code StorageManager} with the given {@code AddressBookStorage} and {@code UserPrefStorage}. */ @@ -75,4 +78,38 @@ public void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath) thro addressBookStorage.saveAddressBook(addressBook, filePath); } + // ================ Manual Save/Load Methods ============================== + + public Path getManualSaveFilePath() { + return addressBookStorage.getManualSaveFilePath(); + } + + /** + * Manually saves the address book to the specified file path. + * + * @param addressBook The address book data to save. + * @throws IOException If there is an error saving the file. + */ + public void saveAddressBookManually(ReadOnlyAddressBook addressBook) throws IOException { + if (manualSaveFilePath == null) { + throw new IOException("Manual save file path is not set."); + } + logger.fine("Attempting to manually write to file: " + manualSaveFilePath); + addressBookStorage.saveAddressBook(addressBook, manualSaveFilePath); + } + + /** + * Manually loads the address book from the specified file path. + * + * @return An optional containing the address book if successfully loaded, otherwise an empty optional. + * @throws DataLoadingException If there is an error loading the file. + */ + public Optional loadAddressBookManually() throws Exception { + if (manualSaveFilePath == null) { + throw new Exception("Manual load file path is not set."); + } + logger.fine("Attempting to manually read from file: " + manualSaveFilePath); + return addressBookStorage.readAddressBook(manualSaveFilePath); + } + } diff --git a/src/test/java/seedu/address/logic/LogicManagerTest.java b/src/test/java/seedu/address/logic/LogicManagerTest.java index e7b52c448ee..f5e95603a5c 100644 --- a/src/test/java/seedu/address/logic/LogicManagerTest.java +++ b/src/test/java/seedu/address/logic/LogicManagerTest.java @@ -46,7 +46,8 @@ public class LogicManagerTest { @BeforeEach public void setUp() { JsonAddressBookStorage addressBookStorage = - new JsonAddressBookStorage(temporaryFolder.resolve("addressBook.json")); + new JsonAddressBookStorage(temporaryFolder.resolve("addressBook.json"), + temporaryFolder.resolve("save.json")); JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(temporaryFolder.resolve("userPrefs.json")); StorageManager storage = new StorageManager(addressBookStorage, userPrefsStorage); logic = new LogicManager(model, storage); @@ -150,7 +151,7 @@ private void assertCommandFailureForExceptionFromStorage(IOException e, String e Path prefPath = temporaryFolder.resolve("ExceptionUserPrefs.json"); // Inject LogicManager with an AddressBookStorage that throws the IOException e when saving - JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(prefPath) { + JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(prefPath, temporaryFolder.resolve("save.json")) { @Override public void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath) throws IOException { diff --git a/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java index 4e5ce9200c8..f5602169642 100644 --- a/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java +++ b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java @@ -31,7 +31,7 @@ public void readAddressBook_nullFilePath_throwsNullPointerException() { } private java.util.Optional readAddressBook(String filePath) throws Exception { - return new JsonAddressBookStorage(Paths.get(filePath)).readAddressBook(addToTestDataPathIfNotNull(filePath)); + return new JsonAddressBookStorage(Paths.get(filePath), Paths.get("save.json")).readAddressBook(addToTestDataPathIfNotNull(filePath)); } private Path addToTestDataPathIfNotNull(String prefsFileInTestDataFolder) { @@ -64,7 +64,7 @@ public void readAddressBook_invalidAndValidPersonAddressBook_throwDataLoadingExc public void readAndSaveAddressBook_allInOrder_success() throws Exception { Path filePath = testFolder.resolve("TempAddressBook.json"); AddressBook original = getTypicalAddressBook(); - JsonAddressBookStorage jsonAddressBookStorage = new JsonAddressBookStorage(filePath); + JsonAddressBookStorage jsonAddressBookStorage = new JsonAddressBookStorage(filePath, Paths.get("save.json")); // Save in new file and read back jsonAddressBookStorage.saveAddressBook(original, filePath); @@ -96,7 +96,7 @@ public void saveAddressBook_nullAddressBook_throwsNullPointerException() { */ private void saveAddressBook(ReadOnlyAddressBook addressBook, String filePath) { try { - new JsonAddressBookStorage(Paths.get(filePath)) + new JsonAddressBookStorage(Paths.get(filePath), Paths.get("save.json")) .saveAddressBook(addressBook, addToTestDataPathIfNotNull(filePath)); } catch (IOException ioe) { throw new AssertionError("There should not be an error writing to the file.", ioe); diff --git a/src/test/java/seedu/address/storage/StorageManagerTest.java b/src/test/java/seedu/address/storage/StorageManagerTest.java index 99a16548970..fe70839c95b 100644 --- a/src/test/java/seedu/address/storage/StorageManagerTest.java +++ b/src/test/java/seedu/address/storage/StorageManagerTest.java @@ -24,7 +24,7 @@ public class StorageManagerTest { @BeforeEach public void setUp() { - JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(getTempFilePath("ab")); + JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(getTempFilePath("ab"), getTempFilePath("save")); JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(getTempFilePath("prefs")); storageManager = new StorageManager(addressBookStorage, userPrefsStorage); } From 276fb81dc08d8add393069e590cbd6cd6c476ef0 Mon Sep 17 00:00:00 2001 From: JJtan <95962077+JJtan2002@users.noreply.github.com> Date: Fri, 18 Oct 2024 23:49:53 +0800 Subject: [PATCH 2/4] Implement save and load functions --- .../seedu/address/logic/LogicManager.java | 15 ++++++++++-- .../address/logic/commands/CommandResult.java | 24 +++++++++++++++---- .../address/logic/commands/ExitCommand.java | 2 +- .../address/logic/commands/HelpCommand.java | 2 +- .../address/logic/commands/LoadCommand.java | 12 ++++++++++ .../address/logic/commands/SaveCommand.java | 12 ++++++++++ .../logic/parser/AddressBookParser.java | 16 +++++-------- .../java/seedu/address/storage/Storage.java | 3 +++ .../seedu/address/storage/StorageManager.java | 11 +++++---- 9 files changed, 74 insertions(+), 23 deletions(-) create mode 100644 src/main/java/seedu/address/logic/commands/LoadCommand.java create mode 100644 src/main/java/seedu/address/logic/commands/SaveCommand.java diff --git a/src/main/java/seedu/address/logic/LogicManager.java b/src/main/java/seedu/address/logic/LogicManager.java index 5aa3b91c7d0..6f60ea10e2a 100644 --- a/src/main/java/seedu/address/logic/LogicManager.java +++ b/src/main/java/seedu/address/logic/LogicManager.java @@ -8,6 +8,7 @@ import javafx.collections.ObservableList; import seedu.address.commons.core.GuiSettings; import seedu.address.commons.core.LogsCenter; +import seedu.address.commons.exceptions.DataLoadingException; import seedu.address.logic.commands.Command; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; @@ -49,9 +50,19 @@ public CommandResult execute(String commandText) throws CommandException, ParseE CommandResult commandResult; Command command = addressBookParser.parseCommand(commandText); commandResult = command.execute(model); - + if (commandResult.isLoad()) { + try { + storage.loadAddressBookManually(); + } catch (Exception e) { + throw new CommandException(String.format(FILE_OPS_ERROR_FORMAT, e.getMessage()), e); + } + } try { - storage.saveAddressBook(model.getAddressBook()); + if (commandResult.isSave()) { + storage.saveAddressBookManually(model.getAddressBook()); + } else { + storage.saveAddressBook(model.getAddressBook()); + } } catch (AccessDeniedException e) { throw new CommandException(String.format(FILE_OPS_PERMISSION_ERROR_FORMAT, e.getMessage()), e); } catch (IOException ioe) { diff --git a/src/main/java/seedu/address/logic/commands/CommandResult.java b/src/main/java/seedu/address/logic/commands/CommandResult.java index 249b6072d0d..c437857c800 100644 --- a/src/main/java/seedu/address/logic/commands/CommandResult.java +++ b/src/main/java/seedu/address/logic/commands/CommandResult.java @@ -19,13 +19,20 @@ public class CommandResult { /** The application should exit. */ private final boolean exit; + /** Manually save the current address book state */ + private final boolean save; + + private final boolean load; + /** * Constructs a {@code CommandResult} with the specified fields. */ - public CommandResult(String feedbackToUser, boolean showHelp, boolean exit) { + public CommandResult(String feedbackToUser, boolean showHelp, boolean exit, boolean save, boolean load) { this.feedbackToUser = requireNonNull(feedbackToUser); this.showHelp = showHelp; this.exit = exit; + this.save = save; + this.load = load; } /** @@ -33,7 +40,7 @@ public CommandResult(String feedbackToUser, boolean showHelp, boolean exit) { * and other fields set to their default value. */ public CommandResult(String feedbackToUser) { - this(feedbackToUser, false, false); + this(feedbackToUser, false, false, false, false); } public String getFeedbackToUser() { @@ -47,6 +54,13 @@ public boolean isShowHelp() { public boolean isExit() { return exit; } + public boolean isSave() { + return save; + } + public boolean isLoad() { + return load; + } + @Override public boolean equals(Object other) { @@ -62,12 +76,13 @@ public boolean equals(Object other) { CommandResult otherCommandResult = (CommandResult) other; return feedbackToUser.equals(otherCommandResult.feedbackToUser) && showHelp == otherCommandResult.showHelp - && exit == otherCommandResult.exit; + && exit == otherCommandResult.exit + && save == otherCommandResult.save; } @Override public int hashCode() { - return Objects.hash(feedbackToUser, showHelp, exit); + return Objects.hash(feedbackToUser, showHelp, exit, save); } @Override @@ -76,6 +91,7 @@ public String toString() { .add("feedbackToUser", feedbackToUser) .add("showHelp", showHelp) .add("exit", exit) + .add("save", save) .toString(); } diff --git a/src/main/java/seedu/address/logic/commands/ExitCommand.java b/src/main/java/seedu/address/logic/commands/ExitCommand.java index 3dd85a8ba90..7874f9ccfc1 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); + return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true, false, false); } } diff --git a/src/main/java/seedu/address/logic/commands/HelpCommand.java b/src/main/java/seedu/address/logic/commands/HelpCommand.java index bf824f91bd0..492a3633fbd 100644 --- a/src/main/java/seedu/address/logic/commands/HelpCommand.java +++ b/src/main/java/seedu/address/logic/commands/HelpCommand.java @@ -16,6 +16,6 @@ public class HelpCommand extends Command { @Override public CommandResult execute(Model model) { - return new CommandResult(SHOWING_HELP_MESSAGE, true, false); + return new CommandResult(SHOWING_HELP_MESSAGE, true, false, false, false); } } diff --git a/src/main/java/seedu/address/logic/commands/LoadCommand.java b/src/main/java/seedu/address/logic/commands/LoadCommand.java new file mode 100644 index 00000000000..7cbd6046540 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/LoadCommand.java @@ -0,0 +1,12 @@ +package seedu.address.logic.commands; + +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; + +public class LoadCommand extends Command{ + public static final String COMMAND_WORD = "load"; + public static final String MESSAGE_SUCCESS = "The saved address book has been loaded!"; + public CommandResult execute(Model model) throws CommandException { + return new CommandResult(MESSAGE_SUCCESS, false, false, false, true); + } +} diff --git a/src/main/java/seedu/address/logic/commands/SaveCommand.java b/src/main/java/seedu/address/logic/commands/SaveCommand.java new file mode 100644 index 00000000000..b4bcf64f90b --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/SaveCommand.java @@ -0,0 +1,12 @@ +package seedu.address.logic.commands; + +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; + +public class SaveCommand extends Command{ + public static final String COMMAND_WORD = "save"; + public static final String MESSAGE_SUCCESS = "Address book has been saved!"; + public CommandResult execute(Model model) throws CommandException { + return new CommandResult(MESSAGE_SUCCESS, false, false, true, false); + } +} diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index 574f2342f95..a393d008444 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -8,16 +8,7 @@ import java.util.regex.Pattern; import seedu.address.commons.core.LogsCenter; -import seedu.address.logic.commands.AddCommand; -import seedu.address.logic.commands.ClearCommand; -import seedu.address.logic.commands.Command; -import seedu.address.logic.commands.DeleteCommand; -import seedu.address.logic.commands.EditCommand; -import seedu.address.logic.commands.EditGameCommand; -import seedu.address.logic.commands.ExitCommand; -import seedu.address.logic.commands.FindCommand; -import seedu.address.logic.commands.HelpCommand; -import seedu.address.logic.commands.ListCommand; +import seedu.address.logic.commands.*; import seedu.address.logic.parser.exceptions.ParseException; /** @@ -81,6 +72,11 @@ public Command parseCommand(String userInput) throws ParseException { case EditGameCommand.COMMAND_WORD: return new EditGameCommandParser().parse(arguments); + case SaveCommand.COMMAND_WORD: + return new SaveCommand(); + + case LoadCommand.COMMAND_WORD: + return new LoadCommand(); default: logger.finer("This user input caused a ParseException: " + userInput); throw new ParseException(MESSAGE_UNKNOWN_COMMAND); diff --git a/src/main/java/seedu/address/storage/Storage.java b/src/main/java/seedu/address/storage/Storage.java index 9fba0c7a1d6..7f877e1d71a 100644 --- a/src/main/java/seedu/address/storage/Storage.java +++ b/src/main/java/seedu/address/storage/Storage.java @@ -29,4 +29,7 @@ public interface Storage extends AddressBookStorage, UserPrefsStorage { @Override void saveAddressBook(ReadOnlyAddressBook addressBook) throws IOException; + void saveAddressBookManually(ReadOnlyAddressBook addressBook) throws IOException; + + Optional loadAddressBookManually() throws Exception; } diff --git a/src/main/java/seedu/address/storage/StorageManager.java b/src/main/java/seedu/address/storage/StorageManager.java index 1cf0633ea08..eba9f1b6b7b 100644 --- a/src/main/java/seedu/address/storage/StorageManager.java +++ b/src/main/java/seedu/address/storage/StorageManager.java @@ -29,6 +29,7 @@ public class StorageManager implements Storage { public StorageManager(AddressBookStorage addressBookStorage, UserPrefsStorage userPrefsStorage) { this.addressBookStorage = addressBookStorage; this.userPrefsStorage = userPrefsStorage; + manualSaveFilePath = addressBookStorage.getManualSaveFilePath(); } // ================ UserPrefs methods ============================== @@ -90,6 +91,7 @@ public Path getManualSaveFilePath() { * @param addressBook The address book data to save. * @throws IOException If there is an error saving the file. */ + @Override public void saveAddressBookManually(ReadOnlyAddressBook addressBook) throws IOException { if (manualSaveFilePath == null) { throw new IOException("Manual save file path is not set."); @@ -104,12 +106,11 @@ public void saveAddressBookManually(ReadOnlyAddressBook addressBook) throws IOEx * @return An optional containing the address book if successfully loaded, otherwise an empty optional. * @throws DataLoadingException If there is an error loading the file. */ - public Optional loadAddressBookManually() throws Exception { - if (manualSaveFilePath == null) { - throw new Exception("Manual load file path is not set."); - } + @Override + public Optional loadAddressBookManually() throws DataLoadingException { + logger.fine("Attempting to manually read from file: " + manualSaveFilePath); return addressBookStorage.readAddressBook(manualSaveFilePath); } - + } From a1b78744c2e7c15050cd10a71a8f9324cea091a0 Mon Sep 17 00:00:00 2001 From: JJtan <95962077+JJtan2002@users.noreply.github.com> Date: Sat, 19 Oct 2024 00:26:58 +0800 Subject: [PATCH 3/4] Fix load command not affecting UI --- .../seedu/address/logic/LogicManager.java | 24 +++++++++++-------- .../java/seedu/address/ui/MainWindow.java | 14 ++++++++++- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main/java/seedu/address/logic/LogicManager.java b/src/main/java/seedu/address/logic/LogicManager.java index 6f60ea10e2a..0a208e61973 100644 --- a/src/main/java/seedu/address/logic/LogicManager.java +++ b/src/main/java/seedu/address/logic/LogicManager.java @@ -8,7 +8,6 @@ import javafx.collections.ObservableList; import seedu.address.commons.core.GuiSettings; import seedu.address.commons.core.LogsCenter; -import seedu.address.commons.exceptions.DataLoadingException; import seedu.address.logic.commands.Command; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; @@ -52,21 +51,26 @@ public CommandResult execute(String commandText) throws CommandException, ParseE commandResult = command.execute(model); if (commandResult.isLoad()) { try { - storage.loadAddressBookManually(); + ReadOnlyAddressBook loadedAddressBook = storage.loadAddressBookManually().get(); + model.setAddressBook(loadedAddressBook); + storage.saveAddressBook(loadedAddressBook); } catch (Exception e) { throw new CommandException(String.format(FILE_OPS_ERROR_FORMAT, e.getMessage()), e); } - } - try { - if (commandResult.isSave()) { + } else if (commandResult.isSave()) { + try { storage.saveAddressBookManually(model.getAddressBook()); - } else { + } catch (IOException ioe) { + throw new CommandException(String.format(FILE_OPS_ERROR_FORMAT, ioe.getMessage()), ioe); + } + } else { + try { storage.saveAddressBook(model.getAddressBook()); + } catch (AccessDeniedException e) { + throw new CommandException(String.format(FILE_OPS_PERMISSION_ERROR_FORMAT, e.getMessage()), e); + } catch (IOException ioe) { + throw new CommandException(String.format(FILE_OPS_ERROR_FORMAT, ioe.getMessage()), ioe); } - } catch (AccessDeniedException e) { - throw new CommandException(String.format(FILE_OPS_PERMISSION_ERROR_FORMAT, e.getMessage()), e); - } catch (IOException ioe) { - throw new CommandException(String.format(FILE_OPS_ERROR_FORMAT, ioe.getMessage()), ioe); } return commandResult; diff --git a/src/main/java/seedu/address/ui/MainWindow.java b/src/main/java/seedu/address/ui/MainWindow.java index 79e74ef37c0..bdbc3a2cd4e 100644 --- a/src/main/java/seedu/address/ui/MainWindow.java +++ b/src/main/java/seedu/address/ui/MainWindow.java @@ -181,7 +181,9 @@ private CommandResult executeCommand(String commandText) throws CommandException if (commandResult.isShowHelp()) { handleHelp(); } - + if (commandResult.isLoad()) { + refreshPersonListPanel(); + } if (commandResult.isExit()) { handleExit(); } @@ -193,4 +195,14 @@ private CommandResult executeCommand(String commandText) throws CommandException throw e; } } + private void refreshPersonListPanel() { + // Clear existing components from the placeholder + personListPanelPlaceholder.getChildren().clear(); + + // Recreate the PersonListPanel with the updated list + personListPanel = new PersonListPanel(logic.getFilteredPersonList()); + + // Add the new PersonListPanel to the placeholder + personListPanelPlaceholder.getChildren().add(personListPanel.getRoot()); + } } From c9fb3ada8000d4433e673ff15d3f096b8fa847ad Mon Sep 17 00:00:00 2001 From: JJtan <95962077+JJtan2002@users.noreply.github.com> Date: Sat, 19 Oct 2024 00:45:15 +0800 Subject: [PATCH 4/4] Fix coding quality and failing tests --- .../seedu/address/logic/commands/CommandResult.java | 1 + .../seedu/address/logic/commands/LoadCommand.java | 6 ++++-- .../seedu/address/logic/commands/SaveCommand.java | 5 ++++- .../address/logic/parser/AddressBookParser.java | 13 ++++++++++++- .../address/storage/JsonAddressBookStorage.java | 6 ++++++ .../java/seedu/address/logic/LogicManagerTest.java | 3 ++- .../address/logic/commands/CommandResultTest.java | 13 +++++++------ .../address/logic/commands/ExitCommandTest.java | 3 ++- .../address/logic/commands/HelpCommandTest.java | 2 +- .../seedu/address/model/preferredtime/DayTest.java | 8 ++++---- .../model/preferredtime/PreferredTimeTest.java | 12 ++++++------ .../seedu/address/model/preferredtime/TimeTest.java | 8 ++++---- .../address/storage/JsonAddressBookStorageTest.java | 3 ++- .../seedu/address/storage/StorageManagerTest.java | 3 ++- 14 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/CommandResult.java b/src/main/java/seedu/address/logic/commands/CommandResult.java index c437857c800..ac383d8d28a 100644 --- a/src/main/java/seedu/address/logic/commands/CommandResult.java +++ b/src/main/java/seedu/address/logic/commands/CommandResult.java @@ -92,6 +92,7 @@ public String toString() { .add("showHelp", showHelp) .add("exit", exit) .add("save", save) + .add("load", load) .toString(); } diff --git a/src/main/java/seedu/address/logic/commands/LoadCommand.java b/src/main/java/seedu/address/logic/commands/LoadCommand.java index 7cbd6046540..3ea0ba236c2 100644 --- a/src/main/java/seedu/address/logic/commands/LoadCommand.java +++ b/src/main/java/seedu/address/logic/commands/LoadCommand.java @@ -2,8 +2,10 @@ import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; - -public class LoadCommand extends Command{ +/** + * Load command that will set address book to the saved file. + */ +public class LoadCommand extends Command { public static final String COMMAND_WORD = "load"; public static final String MESSAGE_SUCCESS = "The saved address book has been loaded!"; public CommandResult execute(Model model) throws CommandException { diff --git a/src/main/java/seedu/address/logic/commands/SaveCommand.java b/src/main/java/seedu/address/logic/commands/SaveCommand.java index b4bcf64f90b..b7806e59fed 100644 --- a/src/main/java/seedu/address/logic/commands/SaveCommand.java +++ b/src/main/java/seedu/address/logic/commands/SaveCommand.java @@ -3,7 +3,10 @@ import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; -public class SaveCommand extends Command{ +/** + * Save command that saves the address book state to a save file. + */ +public class SaveCommand extends Command { public static final String COMMAND_WORD = "save"; public static final String MESSAGE_SUCCESS = "Address book has been saved!"; public CommandResult execute(Model model) throws CommandException { diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index a393d008444..d1aab1335b7 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -8,7 +8,18 @@ import java.util.regex.Pattern; import seedu.address.commons.core.LogsCenter; -import seedu.address.logic.commands.*; +import seedu.address.logic.commands.AddCommand; +import seedu.address.logic.commands.ClearCommand; +import seedu.address.logic.commands.Command; +import seedu.address.logic.commands.DeleteCommand; +import seedu.address.logic.commands.EditCommand; +import seedu.address.logic.commands.EditGameCommand; +import seedu.address.logic.commands.ExitCommand; +import seedu.address.logic.commands.FindCommand; +import seedu.address.logic.commands.HelpCommand; +import seedu.address.logic.commands.ListCommand; +import seedu.address.logic.commands.LoadCommand; +import seedu.address.logic.commands.SaveCommand; import seedu.address.logic.parser.exceptions.ParseException; /** diff --git a/src/main/java/seedu/address/storage/JsonAddressBookStorage.java b/src/main/java/seedu/address/storage/JsonAddressBookStorage.java index 5ea20b27e97..53fc78acc3b 100644 --- a/src/main/java/seedu/address/storage/JsonAddressBookStorage.java +++ b/src/main/java/seedu/address/storage/JsonAddressBookStorage.java @@ -24,6 +24,12 @@ public class JsonAddressBookStorage implements AddressBookStorage { private Path filePath; private Path manualSaveFilePath; + /** + * Creates a new JsonAddressBookStorage object. + * + * @param filePath The path to the JSON file where the address book data will be stored. + * @param manualSaveFilePath The path to the JSON file where the address book data will be manually saved. + */ public JsonAddressBookStorage(Path filePath, Path manualSaveFilePath) { this.filePath = filePath; this.manualSaveFilePath = manualSaveFilePath; diff --git a/src/test/java/seedu/address/logic/LogicManagerTest.java b/src/test/java/seedu/address/logic/LogicManagerTest.java index f5e95603a5c..4fc6cbe4415 100644 --- a/src/test/java/seedu/address/logic/LogicManagerTest.java +++ b/src/test/java/seedu/address/logic/LogicManagerTest.java @@ -151,7 +151,8 @@ private void assertCommandFailureForExceptionFromStorage(IOException e, String e Path prefPath = temporaryFolder.resolve("ExceptionUserPrefs.json"); // Inject LogicManager with an AddressBookStorage that throws the IOException e when saving - JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(prefPath, temporaryFolder.resolve("save.json")) { + JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(prefPath, + temporaryFolder.resolve("save.json")) { @Override public void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath) throws IOException { diff --git a/src/test/java/seedu/address/logic/commands/CommandResultTest.java b/src/test/java/seedu/address/logic/commands/CommandResultTest.java index 7b8c7cd4546..20903f6ad87 100644 --- a/src/test/java/seedu/address/logic/commands/CommandResultTest.java +++ b/src/test/java/seedu/address/logic/commands/CommandResultTest.java @@ -14,7 +14,7 @@ public void equals() { // same values -> returns true assertTrue(commandResult.equals(new CommandResult("feedback"))); - assertTrue(commandResult.equals(new CommandResult("feedback", false, false))); + assertTrue(commandResult.equals(new CommandResult("feedback", false, false, false, false))); // same object -> returns true assertTrue(commandResult.equals(commandResult)); @@ -29,10 +29,10 @@ public void equals() { assertFalse(commandResult.equals(new CommandResult("different"))); // different showHelp value -> returns false - assertFalse(commandResult.equals(new CommandResult("feedback", true, false))); + assertFalse(commandResult.equals(new CommandResult("feedback", true, false, false, false))); // different exit value -> returns false - assertFalse(commandResult.equals(new CommandResult("feedback", false, true))); + assertFalse(commandResult.equals(new CommandResult("feedback", false, true, false, false))); } @Test @@ -46,10 +46,10 @@ public void hashcode() { assertNotEquals(commandResult.hashCode(), new CommandResult("different").hashCode()); // different showHelp value -> returns different hashcode - assertNotEquals(commandResult.hashCode(), new CommandResult("feedback", true, false).hashCode()); + assertNotEquals(commandResult.hashCode(), new CommandResult("feedback", true, false, false, false).hashCode()); // different exit value -> returns different hashcode - assertNotEquals(commandResult.hashCode(), new CommandResult("feedback", false, true).hashCode()); + assertNotEquals(commandResult.hashCode(), new CommandResult("feedback", false, true, false, false).hashCode()); } @Test @@ -57,7 +57,8 @@ public void toStringMethod() { CommandResult commandResult = new CommandResult("feedback"); String expected = CommandResult.class.getCanonicalName() + "{feedbackToUser=" + commandResult.getFeedbackToUser() + ", showHelp=" + commandResult.isShowHelp() - + ", exit=" + commandResult.isExit() + "}"; + + ", exit=" + commandResult.isExit() + ", save=" + commandResult.isSave() + + ", load=" + commandResult.isLoad() + "}"; assertEquals(expected, commandResult.toString()); } } diff --git a/src/test/java/seedu/address/logic/commands/ExitCommandTest.java b/src/test/java/seedu/address/logic/commands/ExitCommandTest.java index 9533c473875..ea477bc17ae 100644 --- a/src/test/java/seedu/address/logic/commands/ExitCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ExitCommandTest.java @@ -14,7 +14,8 @@ public class ExitCommandTest { @Test public void execute_exit_success() { - CommandResult expectedCommandResult = new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true); + CommandResult expectedCommandResult = + new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true, false, false); assertCommandSuccess(new ExitCommand(), model, expectedCommandResult, expectedModel); } } diff --git a/src/test/java/seedu/address/logic/commands/HelpCommandTest.java b/src/test/java/seedu/address/logic/commands/HelpCommandTest.java index 4904fc4352e..ab3eb4e25a7 100644 --- a/src/test/java/seedu/address/logic/commands/HelpCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/HelpCommandTest.java @@ -14,7 +14,7 @@ public class HelpCommandTest { @Test public void execute_help_success() { - CommandResult expectedCommandResult = new CommandResult(SHOWING_HELP_MESSAGE, true, false); + CommandResult expectedCommandResult = new CommandResult(SHOWING_HELP_MESSAGE, true, false, false, false); assertCommandSuccess(new HelpCommand(), model, expectedCommandResult, expectedModel); } } diff --git a/src/test/java/seedu/address/model/preferredtime/DayTest.java b/src/test/java/seedu/address/model/preferredtime/DayTest.java index ed2e609dd7a..e12739a512e 100644 --- a/src/test/java/seedu/address/model/preferredtime/DayTest.java +++ b/src/test/java/seedu/address/model/preferredtime/DayTest.java @@ -16,11 +16,11 @@ public void constructor_null_throwsNullPointerException() { @Test public void constructor_invalidName_throwsIllegalArgumentException() { - String invalid_empty = ""; - assertThrows(IllegalArgumentException.class, () -> new Day(invalid_empty)); + String invalidEmpty = ""; + assertThrows(IllegalArgumentException.class, () -> new Day(invalidEmpty)); - String invalid_out_of_range = "Wednes"; - assertThrows(IllegalArgumentException.class, () -> new Time(invalid_out_of_range)); + String invalidOutOfRange = "Wednes"; + assertThrows(IllegalArgumentException.class, () -> new Time(invalidOutOfRange)); } @Test diff --git a/src/test/java/seedu/address/model/preferredtime/PreferredTimeTest.java b/src/test/java/seedu/address/model/preferredtime/PreferredTimeTest.java index 7418f253631..b1135715de8 100644 --- a/src/test/java/seedu/address/model/preferredtime/PreferredTimeTest.java +++ b/src/test/java/seedu/address/model/preferredtime/PreferredTimeTest.java @@ -17,14 +17,14 @@ public void constructor_null_throwsNullPointerException() { @Test public void constructor_invalidName_throwsIllegalArgumentException() { - String invalid_empty = ""; - assertThrows(IllegalArgumentException.class, () -> new PreferredTime(invalid_empty)); + String invalidEmpty = ""; + assertThrows(IllegalArgumentException.class, () -> new PreferredTime(invalidEmpty)); - String invalid_out_of_range = "Monday 4500"; - assertThrows(IllegalArgumentException.class, () -> new PreferredTime(invalid_out_of_range)); + String invalidOutOfRange = "Monday 4500"; + assertThrows(IllegalArgumentException.class, () -> new PreferredTime(invalidOutOfRange)); - String invalid_day = "weekday 1200"; - assertThrows(IllegalArgumentException.class, () -> new PreferredTime(invalid_day)); + String invalidDay = "weekday 1200"; + assertThrows(IllegalArgumentException.class, () -> new PreferredTime(invalidDay)); } @Test diff --git a/src/test/java/seedu/address/model/preferredtime/TimeTest.java b/src/test/java/seedu/address/model/preferredtime/TimeTest.java index b8aa7f9a97e..4a977648662 100644 --- a/src/test/java/seedu/address/model/preferredtime/TimeTest.java +++ b/src/test/java/seedu/address/model/preferredtime/TimeTest.java @@ -16,11 +16,11 @@ public void constructor_null_throwsNullPointerException() { @Test public void constructor_invalidName_throwsIllegalArgumentException() { - String invalidTime_empty = ""; - assertThrows(IllegalArgumentException.class, () -> new Time(invalidTime_empty)); + String invalidTimeEmpty = ""; + assertThrows(IllegalArgumentException.class, () -> new Time(invalidTimeEmpty)); - String invalidTime_out_of_range = "4500"; - assertThrows(IllegalArgumentException.class, () -> new Time(invalidTime_out_of_range)); + String invalidTimeOutOfRange = "4500"; + assertThrows(IllegalArgumentException.class, () -> new Time(invalidTimeOutOfRange)); } diff --git a/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java index f5602169642..4ad3596b462 100644 --- a/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java +++ b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java @@ -31,7 +31,8 @@ public void readAddressBook_nullFilePath_throwsNullPointerException() { } private java.util.Optional readAddressBook(String filePath) throws Exception { - return new JsonAddressBookStorage(Paths.get(filePath), Paths.get("save.json")).readAddressBook(addToTestDataPathIfNotNull(filePath)); + return new JsonAddressBookStorage(Paths.get(filePath), Paths.get("save.json")) + .readAddressBook(addToTestDataPathIfNotNull(filePath)); } private Path addToTestDataPathIfNotNull(String prefsFileInTestDataFolder) { diff --git a/src/test/java/seedu/address/storage/StorageManagerTest.java b/src/test/java/seedu/address/storage/StorageManagerTest.java index fe70839c95b..ddec2bd410d 100644 --- a/src/test/java/seedu/address/storage/StorageManagerTest.java +++ b/src/test/java/seedu/address/storage/StorageManagerTest.java @@ -24,7 +24,8 @@ public class StorageManagerTest { @BeforeEach public void setUp() { - JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(getTempFilePath("ab"), getTempFilePath("save")); + JsonAddressBookStorage addressBookStorage = + new JsonAddressBookStorage(getTempFilePath("ab"), getTempFilePath("save")); JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(getTempFilePath("prefs")); storageManager = new StorageManager(addressBookStorage, userPrefsStorage); }