Skip to content

Commit

Permalink
Added Storage Support
Browse files Browse the repository at this point in the history
Signed-off-by: Mudaafi <[email protected]>
  • Loading branch information
Mudaafi committed Nov 9, 2019
1 parent be0f7ad commit 1530e7f
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 14 deletions.
7 changes: 2 additions & 5 deletions savedWallet.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
out $100.0 /date 2019-11-08 /tags unlucky misplaced
10 changes: 9 additions & 1 deletion src/main/java/executor/command/CommandTrackTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import storage.StorageManager;
import ui.UiCode;

import java.util.ArrayList;

public class CommandTrackTag extends Command {
private String inputStr;

Expand All @@ -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) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/executor/command/CommandUntrackTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/storage/StorageWallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -58,13 +66,17 @@ 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("")) {
break;
}
parseAddReceiptFromStorageString(wallet, loadedInput);
}
for (String folderName : folderNames.split("-")) {
wallet.addFolder(folderName);
}
} catch (Exception e) {
throw new DukeException("No Previously Saved Wallet Data.");
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/ui/ReceiptTracker.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/ui/gui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void initialize(Stage stage, String taskPath, String walletPath) {

this.fetchStoredImages();
this.showHomeDisplay();
this.refresh();
}

@FXML
Expand All @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/CommandMajorExpenseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/ui/CommandTrackTagTest.java
Original file line number Diff line number Diff line change
@@ -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<String> 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");
}
}
45 changes: 45 additions & 0 deletions src/test/java/ui/CommandUntrackTagTest.java
Original file line number Diff line number Diff line change
@@ -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<String> 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("<food> is not being tracked!\nIf you wish to track this tag, try TRACK <tag>.",
notTracked.getInfoCapsule().getOutputStr());
}
}
1 change: 1 addition & 0 deletions testWalletDataLoad.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions testWalletDataSave.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 1530e7f

Please sign in to comment.