From 1530e7f27bf1421adcfc3088706447ec1d640054 Mon Sep 17 00:00:00 2001 From: Mudaafi Date: Sat, 9 Nov 2019 19:55:49 +0800 Subject: [PATCH] Added Storage Support Signed-off-by: Mudaafi --- savedWallet.txt | 7 +-- .../executor/command/CommandTrackTag.java | 10 ++++- .../executor/command/CommandUntrackTag.java | 8 +++- src/main/java/storage/StorageWallet.java | 12 +++++ src/main/java/ui/ReceiptTracker.java | 4 +- src/main/java/ui/gui/MainWindow.java | 6 ++- src/test/java/CommandMajorExpenseTest.java | 6 +-- src/test/java/ui/CommandTrackTagTest.java | 35 +++++++++++++++ src/test/java/ui/CommandUntrackTagTest.java | 45 +++++++++++++++++++ testWalletDataLoad.txt | 1 + testWalletDataSave.txt | 1 + 11 files changed, 121 insertions(+), 14 deletions(-) create mode 100644 src/test/java/ui/CommandTrackTagTest.java create mode 100644 src/test/java/ui/CommandUntrackTagTest.java diff --git a/savedWallet.txt b/savedWallet.txt index 2076c3a989..928bb4204d 100644 --- a/savedWallet.txt +++ b/savedWallet.txt @@ -1,8 +1,5 @@ 1000.0 -out $4.5 /date 2019-11-01 /tags food -out $120.0 /date 2019-11-01 /tags travel -out $5.0 /date 2019-11-01 /tags food -out $30.0 /date 2019-11-01 /tags dating +angbao-dating-travel-food- out $20.0 /date 2010-11-01 /tags gift out $3.0 /date 2019-11-02 /tags food out $25.0 /date 2019-11-02 /tags groceries @@ -28,4 +25,4 @@ out $15.0 /date 2010-11-01 /tags gift out $12.0 /date 2019-11-08 /tags food out $45.0 /date 2019-11-08 /tags travel concession in $45.0 /date 2019-11-08 /tags angbao -out $100.0 /date 2019-11-08 /tags unlucky misplaced \ No newline at end of file +out $100.0 /date 2019-11-08 /tags unlucky misplaced diff --git a/src/main/java/executor/command/CommandTrackTag.java b/src/main/java/executor/command/CommandTrackTag.java index 5c7b67857f..f81975636e 100644 --- a/src/main/java/executor/command/CommandTrackTag.java +++ b/src/main/java/executor/command/CommandTrackTag.java @@ -5,6 +5,8 @@ import storage.StorageManager; import ui.UiCode; +import java.util.ArrayList; + public class CommandTrackTag extends Command { private String inputStr; @@ -22,7 +24,13 @@ public CommandTrackTag(String userInput) { @Override public void execute(StorageManager storageManager) { - for (String tag : this.inputStr.split(" ")) { + String[] tagsToTrack = this.inputStr.split(" "); + if (tagsToTrack[0].equals("")) { + this.infoCapsule.setUiCode(UiCode.ERROR); + this.infoCapsule.setOutputStr("Please enter a tag to track"); + return; + } + for (String tag : tagsToTrack) { try { storageManager.trackTag(tag); } catch (DukeException e) { diff --git a/src/main/java/executor/command/CommandUntrackTag.java b/src/main/java/executor/command/CommandUntrackTag.java index 7519f37b2a..e41c779c37 100644 --- a/src/main/java/executor/command/CommandUntrackTag.java +++ b/src/main/java/executor/command/CommandUntrackTag.java @@ -22,7 +22,13 @@ public CommandUntrackTag(String userInput) { @Override public void execute(StorageManager storageManager) { - for (String tag : this.inputStr.split(" ")) { + String[] tagsToTrack = this.inputStr.split(" "); + if (tagsToTrack[0].equals("")) { + this.infoCapsule.setUiCode(UiCode.ERROR); + this.infoCapsule.setOutputStr("Please enter a tag to untrack"); + return; + } + for (String tag : tagsToTrack) { try { storageManager.untrackTag(tag); } catch (DukeException e) { diff --git a/src/main/java/storage/StorageWallet.java b/src/main/java/storage/StorageWallet.java index e3ad96ba2a..9feccab817 100644 --- a/src/main/java/storage/StorageWallet.java +++ b/src/main/java/storage/StorageWallet.java @@ -32,6 +32,14 @@ public void saveData(Wallet wallet) throws DukeException { try { FileWriter writer = new FileWriter(this.filePath); writer.write(wallet.getBalance().toString() + "\n"); + + StringBuilder folderNames = new StringBuilder(); + for (String folderName : wallet.getFolders().keySet()) { + if (!folderName.equals("Income") && !folderName.equals("Expenses")) { + folderNames.append(folderName).append("-"); + } + } + writer.write(folderNames.toString() + "\n"); for (Receipt receipt : wallet.getReceipts()) { String strSave = Parser.encodeReceipt(receipt); writer.write(strSave); @@ -58,6 +66,7 @@ public void loadData(Wallet wallet) throws DukeException { throw new DukeException("Balance cannot be read"); } wallet.setBalance(storedBalanceDouble); + String folderNames = scanner.nextLine(); while (scanner.hasNextLine()) { String loadedInput = scanner.nextLine(); if (loadedInput.equals("")) { @@ -65,6 +74,9 @@ public void loadData(Wallet wallet) throws DukeException { } parseAddReceiptFromStorageString(wallet, loadedInput); } + for (String folderName : folderNames.split("-")) { + wallet.addFolder(folderName); + } } catch (Exception e) { throw new DukeException("No Previously Saved Wallet Data."); } diff --git a/src/main/java/ui/ReceiptTracker.java b/src/main/java/ui/ReceiptTracker.java index 91433300ea..b1ad4b12bd 100644 --- a/src/main/java/ui/ReceiptTracker.java +++ b/src/main/java/ui/ReceiptTracker.java @@ -1,8 +1,6 @@ package ui; import duke.exception.DukeException; -import main.Duke; - import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashMap; @@ -99,7 +97,7 @@ public void addFolder(String tag) throws DukeException { */ public void removeFolder(String tag) throws DukeException { if (!isRegisteredTag(tag)) { - throw new DukeException("This tag is not being tracked!"); + throw new DukeException("<" + tag + "> is not being tracked!"); } this.getFolders().remove(tag); } diff --git a/src/main/java/ui/gui/MainWindow.java b/src/main/java/ui/gui/MainWindow.java index f7769fd53e..2876ef6887 100644 --- a/src/main/java/ui/gui/MainWindow.java +++ b/src/main/java/ui/gui/MainWindow.java @@ -52,6 +52,7 @@ void initialize(Stage stage, String taskPath, String walletPath) { this.fetchStoredImages(); this.showHomeDisplay(); + this.refresh(); } @FXML @@ -62,7 +63,7 @@ private void handleUserInput() { this.updateGui(infoCapsule); if (this.displayType == DisplayType.HOME) { this.updateHomeDisplay(); - this.updateHomeDisplay(); // Javafx won't update completely + this.refresh(); // Javafx won't update completely } this.userInput.clear(); if (this.exitRequest) { @@ -150,6 +151,9 @@ void saveAllData() { this.interpreterLayer.requestSave(); } + void refresh() { + this.updateHomeDisplay(); + } /** * Fetches Images stored in application for display in slots for features yet to be developed. diff --git a/src/test/java/CommandMajorExpenseTest.java b/src/test/java/CommandMajorExpenseTest.java index d4ec40f4d7..30fb037ec5 100644 --- a/src/test/java/CommandMajorExpenseTest.java +++ b/src/test/java/CommandMajorExpenseTest.java @@ -32,8 +32,8 @@ void execute() { m1.execute(storageManager); String output = m1.getInfoCapsule().getOutputStr(); assertEquals("These are your receipts above/equal to" + " " + "$" + 40 + "\n" - + "1. [transport] 40.0 2019-02-01\n" - + "2. [transport] 100.0 2019-05-02\n", output); + + "1. [transport] $40.00 2019-02-01\n" + + "2. [transport] $100.00 2019-05-02\n", output); CommandMajorExpense m2 = new CommandMajorExpense("majorexpense -5.0"); m2.execute(storageManager); @@ -44,7 +44,7 @@ void execute() { m3.execute(storageManager); String result1 = m3.getInfoCapsule().getOutputStr(); assertEquals("These are your receipts above/equal to $" + 100 + "\n" - + "1. [transport] 100.0 2019-05-02\n", result1); + + "1. [transport] $100.00 2019-05-02\n", result1); CommandMajorExpense m4 = new CommandMajorExpense("majorexpense 34df5"); m4.execute(storageManager); diff --git a/src/test/java/ui/CommandTrackTagTest.java b/src/test/java/ui/CommandTrackTagTest.java new file mode 100644 index 0000000000..7adcd32038 --- /dev/null +++ b/src/test/java/ui/CommandTrackTagTest.java @@ -0,0 +1,35 @@ +package ui; + +import executor.command.CommandTrackTag; +import org.junit.jupiter.api.Test; +import storage.StorageManager; + +import java.time.LocalDate; +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CommandTrackTagTest { + @Test + void executeTest() { + StorageManager store = new StorageManager(); + CommandTrackTag emptyArg = new CommandTrackTag("Track"); + emptyArg.execute(store); + assertEquals(emptyArg.getInfoCapsule().getUiCode(), UiCode.ERROR); + assertEquals(emptyArg.getInfoCapsule().getOutputStr(), "Please enter a tag to track"); + + ArrayList tags = new ArrayList<>(); + tags.add("food"); + store.getWallet().addReceipt(new Receipt(100.0, LocalDate.now(), tags)); + store.getWallet().addReceipt(new Receipt(100.0, LocalDate.now(), tags)); + CommandTrackTag correctArg = new CommandTrackTag("Track food"); + correctArg.execute(store); + assertEquals(correctArg.getInfoCapsule().getUiCode(), UiCode.TOAST); + assertEquals(correctArg.getInfoCapsule().getOutputStr(), "Tracking tags: food"); + + CommandTrackTag multipleArgs = new CommandTrackTag("Track food dating"); + multipleArgs.execute(store); + assertEquals(multipleArgs.getInfoCapsule().getUiCode(), UiCode.TOAST); + assertEquals(multipleArgs.getInfoCapsule().getOutputStr(), "Tracking tags: food dating"); + } +} diff --git a/src/test/java/ui/CommandUntrackTagTest.java b/src/test/java/ui/CommandUntrackTagTest.java new file mode 100644 index 0000000000..c0dc6607fb --- /dev/null +++ b/src/test/java/ui/CommandUntrackTagTest.java @@ -0,0 +1,45 @@ +package ui; + +import duke.exception.DukeException; +import executor.command.CommandUntrackTag; +import org.junit.jupiter.api.Test; +import storage.StorageManager; + +import java.time.LocalDate; +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CommandUntrackTagTest { + @Test + void executeTest() { + StorageManager store = new StorageManager(); + CommandUntrackTag emptyArg = new CommandUntrackTag("Untrack"); + emptyArg.execute(store); + assertEquals(UiCode.ERROR, emptyArg.getInfoCapsule().getUiCode()); + assertEquals("Please enter a tag to untrack", emptyArg.getInfoCapsule().getOutputStr()); + + try { + ArrayList tags = new ArrayList<>(); + tags.add("food"); + store.getWallet().addReceipt(new Receipt(100.0, LocalDate.now(), tags)); + tags.remove("food"); + store.getWallet().addReceipt(new Receipt(100.0, LocalDate.now(), tags)); + store.getWallet().getReceipts().addFolder("food"); + } catch (DukeException e) { + e.printStackTrace(); + } + + CommandUntrackTag correctUntrack = new CommandUntrackTag("Untrack food"); + correctUntrack.execute(store); + assertEquals("Untracked tags: food", correctUntrack.getInfoCapsule().getOutputStr()); + assertEquals(UiCode.TOAST, correctUntrack.getInfoCapsule().getUiCode()); + + // Removing a non-existent tracking tag + CommandUntrackTag notTracked = new CommandUntrackTag("Untrack food"); + notTracked.execute(store); + assertEquals(notTracked.getInfoCapsule().getUiCode(), UiCode.ERROR); + assertEquals(" is not being tracked!\nIf you wish to track this tag, try TRACK .", + notTracked.getInfoCapsule().getOutputStr()); + } +} diff --git a/testWalletDataLoad.txt b/testWalletDataLoad.txt index e4131cde8e..b8c7fa3ecd 100644 --- a/testWalletDataLoad.txt +++ b/testWalletDataLoad.txt @@ -1,4 +1,5 @@ 100.0 + in $5.0 /date 2019-08-30 /tags street out $10.0 /date 2001-08-25 /tags out $15.0 /date 2019-12-10 /tags dogfood puppy monthly diff --git a/testWalletDataSave.txt b/testWalletDataSave.txt index fd13acf3f5..b5cc79fb59 100644 --- a/testWalletDataSave.txt +++ b/testWalletDataSave.txt @@ -1,4 +1,5 @@ 100.0 +- in $5.0 /date 2019-08-30 /tags street out $10.0 /date 2001-08-25 /tags out $15.0 /date 2019-12-10 /tags dogfood puppy monthly