From cdd3b865d41ee5ee984859afc7836e2e128161c4 Mon Sep 17 00:00:00 2001 From: maxxyx96 Date: Wed, 16 Oct 2019 20:56:11 +0800 Subject: [PATCH 1/2] Implemented and optimised budget for GUI --- data/budget.txt | 13 +++ src/main/java/duke/Duke.java | 2 +- .../java/duke/command/AddBudgetCommand.java | 20 ++--- src/main/java/duke/command/BackupCommand.java | 5 +- ...etCommand.java => ResetBudgetCommand.java} | 25 +++--- .../java/duke/command/ViewBudgetCommand.java | 8 +- src/main/java/duke/parser/Parser.java | 5 +- src/main/java/duke/ui/Ui.java | 81 ++++++++++++++----- 8 files changed, 109 insertions(+), 50 deletions(-) create mode 100644 data/budget.txt rename src/main/java/duke/command/{UpdateBudgetCommand.java => ResetBudgetCommand.java} (71%) diff --git a/data/budget.txt b/data/budget.txt new file mode 100644 index 0000000000..9dae2d8268 --- /dev/null +++ b/data/budget.txt @@ -0,0 +1,13 @@ +20.0 +0.0 +10.0 +-10.0 +-100.0 +50.0 +-10.0 +-10.0 +10.0 +10.0 +10.0 +-100.0 +100.0 diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index 7adb815b22..410e8f2fe9 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -165,11 +165,11 @@ public void run() { || cmd instanceof SetPriorityCommand) { cmd.execute(items, priorityList, ui); } else if (cmd instanceof BackupCommand) { - ui.showBeforeBackupMsg(); priorityStorage.write(priorityList); budgetStorage.write(budgetList); contactStorage.write(contactList); storage.write(items); + cmd.execute(items, ui); cmd.executeStorage(items, ui, storage); } else if (cmd instanceof AddContactsCommand) { cmd.execute(items, contactList, ui); diff --git a/src/main/java/duke/command/AddBudgetCommand.java b/src/main/java/duke/command/AddBudgetCommand.java index a97ea460a1..37a9fc5583 100644 --- a/src/main/java/duke/command/AddBudgetCommand.java +++ b/src/main/java/duke/command/AddBudgetCommand.java @@ -9,8 +9,9 @@ import java.io.IOException; public class AddBudgetCommand extends Command { - + protected BudgetList budgetList; protected Ui ui = new Ui(); + protected float amount; /** * Adds the amount specified into the budgetList. @@ -19,26 +20,26 @@ public class AddBudgetCommand extends Command { * @param amount amount to be updated in the user's budget. */ public AddBudgetCommand(BudgetList budgetList, float amount) { - budgetList.addToBudget(amount); - ui.showBudget(budgetList.getBudget()); + this.budgetList = budgetList; + this.amount = amount; } /** - * Executes a command with task list and ui. - * (not used) + * Executes the command to add a certain amount to the existing budget. * * @param items The task list that contains a list of tasks. * @param ui To tell the user that it is executed successfully. */ @Override public void execute(TaskList items, Ui ui) { - + ui.showAddBudget(amount, budgetList.getBudget()); + budgetList.addToBudget(amount); + ui.showBudget(budgetList.getBudget()); } /** - * Executes a command with task list and ui (GUI). - * (not used) + * Executes the command to add a certain amount to the existing budget. * * @param items The task list that contains a list of tasks. * @param ui To tell the user that it is executed successfully. @@ -46,7 +47,8 @@ public void execute(TaskList items, Ui ui) { */ @Override public String executeGui(TaskList items, Ui ui) { - return null; + budgetList.addToBudget(amount); + return ui.showAddBudgetGui(amount, budgetList.getBudget()) + "\n" + ui.showBudgetGui(budgetList.getBudget()); } /** diff --git a/src/main/java/duke/command/BackupCommand.java b/src/main/java/duke/command/BackupCommand.java index 8e99818eb4..3264fc698b 100644 --- a/src/main/java/duke/command/BackupCommand.java +++ b/src/main/java/duke/command/BackupCommand.java @@ -35,6 +35,8 @@ public static void openBackupFolder() throws IOException { */ @Override public void execute(TaskList items, Ui ui) { + ui.showBackupMessage(); + ui.showBackupFolderMessage(); } /** @@ -47,7 +49,7 @@ public void execute(TaskList items, Ui ui) { */ @Override public String executeGui(TaskList items, Ui ui) { - return null; + return ui.showBackupMessageGui(); } /** @@ -61,7 +63,6 @@ public String executeGui(TaskList items, Ui ui) { @Override public void executeStorage(TaskList items, Ui ui, Storage storage) throws IOException { openBackupFolder(); - ui.showAfterBackupMsg(); } } \ No newline at end of file diff --git a/src/main/java/duke/command/UpdateBudgetCommand.java b/src/main/java/duke/command/ResetBudgetCommand.java similarity index 71% rename from src/main/java/duke/command/UpdateBudgetCommand.java rename to src/main/java/duke/command/ResetBudgetCommand.java index 6b19c41f5f..94f1b7d8d7 100644 --- a/src/main/java/duke/command/UpdateBudgetCommand.java +++ b/src/main/java/duke/command/ResetBudgetCommand.java @@ -7,24 +7,21 @@ import java.io.IOException; -public class UpdateBudgetCommand extends Command { +public class ResetBudgetCommand extends Command { protected BudgetList budgetList; protected Ui ui = new Ui(); + protected float amount; + /** - * Updates the current budget with the input amount, and prompts the user to confirm - * his/her actions before carrying on with the action. + * Obtaining the parameters of budget from budget corner. * * @param budgetList The list of budget that is stored by Duke Manager. * @param amount The amount to be updated. */ - public UpdateBudgetCommand(BudgetList budgetList, float amount) { - if (ui.isBudgetResetTrue()) { - budgetList.resetBudget(amount); - ui.showBudget(budgetList.getBudget()); - } else { - ui.rejectBudgetResetMessage(); - } + public ResetBudgetCommand(BudgetList budgetList, float amount) { + this.budgetList = budgetList; + this.amount = amount; } @@ -37,7 +34,9 @@ public UpdateBudgetCommand(BudgetList budgetList, float amount) { */ @Override public void execute(TaskList items, Ui ui) { - + ui.showResetBudget(budgetList.getBudget()); + budgetList.resetBudget(amount); + ui.showBudget(budgetList.getBudget()); } /** @@ -50,7 +49,9 @@ public void execute(TaskList items, Ui ui) { */ @Override public String executeGui(TaskList items, Ui ui) { - return null; + Float previousBudget = budgetList.getBudget(); + budgetList.resetBudget(amount); + return ui.showResetBudgetGui(previousBudget) + "\n" + ui.showBudgetGui(budgetList.getBudget()); } /** diff --git a/src/main/java/duke/command/ViewBudgetCommand.java b/src/main/java/duke/command/ViewBudgetCommand.java index 4195a81c4d..9b679eefdd 100644 --- a/src/main/java/duke/command/ViewBudgetCommand.java +++ b/src/main/java/duke/command/ViewBudgetCommand.java @@ -11,6 +11,7 @@ public class ViewBudgetCommand extends Command { protected Ui ui = new Ui(); + protected BudgetList budgetList; /** * Command that allows the user to view the budget that he/she currently has. @@ -18,10 +19,9 @@ public class ViewBudgetCommand extends Command { * @param budgetList The list of budget that is stored by Duke Manager. */ public ViewBudgetCommand(BudgetList budgetList) { - ui.showBudget(budgetList.getBudget()); + this.budgetList = budgetList; } - /** * Executes a command with task list and ui. * (not used) @@ -31,7 +31,7 @@ public ViewBudgetCommand(BudgetList budgetList) { */ @Override public void execute(TaskList items, Ui ui) { - + ui.showBudget(budgetList.getBudget()); } /** @@ -44,7 +44,7 @@ public void execute(TaskList items, Ui ui) { */ @Override public String executeGui(TaskList items, Ui ui) { - return null; + return ui.showBudgetGui(budgetList.getBudget()); } /** diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index eff9ac9398..152f8f30b9 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -17,7 +17,7 @@ import duke.command.AddContactsCommand; import duke.command.ListContactsCommand; import duke.command.AddBudgetCommand; -import duke.command.UpdateBudgetCommand; +import duke.command.ResetBudgetCommand; import duke.command.ViewBudgetCommand; import duke.task.TaskList; @@ -33,7 +33,6 @@ import duke.task.Contacts; import duke.task.BudgetList; -import java.text.ParseException; import java.util.ArrayList; /** @@ -403,7 +402,7 @@ public static Command parse(String sentence, TaskList items, BudgetList budgetLi try { String budgetAmount = budgetCommandString.split(" ", 2)[1]; if (budgetCommand.trim().equals("new") || budgetCommand.trim().equals("reset")) { - return new UpdateBudgetCommand(budgetList, Float.parseFloat(budgetAmount)); + return new ResetBudgetCommand(budgetList, Float.parseFloat(budgetAmount)); } else if (budgetCommand.trim().equals("add") || budgetCommand.trim().equals("+")) { return new AddBudgetCommand(budgetList, Float.parseFloat(budgetAmount)); } else if (budgetCommand.trim().equals("minus") || budgetCommand.trim().equals("-")) { diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 09888c5e0b..14266345b8 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -339,17 +339,16 @@ public void showDuplicateMsg() { /** * Outputs a message to the user to let it know that it is updating. */ - public void showBeforeBackupMsg() { - out.println(" Updating duke.txt..."); + public void showBackupMessage() { + out.println(" Duke Manager has been backed up!"); } - /** - * Outputs a message to the user to let it know that it has finished updating, - * and the file is shown in a folder. - */ - public void showAfterBackupMsg() { - out.println(" Updated " + BACKUP_FILENAME + " with the current items in Duke Manager!"); - out.println(" Directory of the file opened in explorer!"); + public void showBackupFolderMessage() { + out.println(" Duke has opened the backup file location in file explorer!"); + } + + public String showBackupMessageGui() { + return " Duke Manager has been backed up!"; } /** @@ -379,26 +378,70 @@ public void showAddedContact(ContactList contactList) { } } + /** + * Shows the current budget of the user. + * + * @param amount the budget the user currently has. + */ public void showBudget(float amount) { out.println(" Your budget is : $" + amount); } - public void rejectBudgetResetMessage() { - out.println(" Budget reset cancelled."); + /** + * Shows the current budget of the user. + * + * @param amount the budget the user currently has. + * @return the message to be shown to the user. + */ + public String showBudgetGui(float amount) { + return " Your current Budget is : $" + amount; } /** - * Checks if the user is certain about resetting the budget by prompting the user to confirm his/her actions. + * Shows the user the amount that is added/subtracted to the existing budget. * - * @return returns true if user pressed Y, and false otherwise. + * @param amount The amount that is to be added/subtracted. + * @param budget The existing budget of the user. */ - public boolean isBudgetResetTrue() { - out.println(" You have an existing budget, are you sure you want to do this? Y/N"); - String choice = readCommand(); - if (choice.equals("Y") || choice.equals("y")) { - return true; + public void showAddBudget(float amount, float budget) { + if (amount > 0) { + out.println(" You are adding $" + amount + " to your current budget of $" + budget); } else { - return false; + out.println(" You are subtracting $" + -amount + " from your current budget of $" + budget); } } + + /** + * Shows the user the amount that is added/subtracted to the existing budget. (GUI) + * + * @param amount The amount that is to be added/subtracted. + * @param budget The existing budget of the user. + * @return String of the text to show user. + */ + public String showAddBudgetGui(float amount, float budget) { + if (amount > 0) { + return " You are adding $" + amount + " to your current budget of $" + budget; + } else { + return " You are subtracting $" + -amount + " from your current budget of $" + budget; + } + } + + /** + * Shows the budget that the user has before the changes. + * + * @param budget The budget of the user. + */ + public void showResetBudget(float budget) { + out.println(" Your previous budget of $" + budget + " has been reset."); + } + + /** + * Shows the budget that the user has before the changes. + * + * @param budget The budget of the user. + * @return String of the reset message. + */ + public String showResetBudgetGui(float budget) { + return " Your previous budget of " + budget + " has been reset."; + } } \ No newline at end of file From ead2f5965bfaeab37ae864500bd6e376ae4543e5 Mon Sep 17 00:00:00 2001 From: maxxyx96 Date: Wed, 16 Oct 2019 21:03:49 +0800 Subject: [PATCH 2/2] Budget.txt cleanup --- data/budget.txt | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/data/budget.txt b/data/budget.txt index 9dae2d8268..f4c6c52b10 100644 --- a/data/budget.txt +++ b/data/budget.txt @@ -1,13 +1,2 @@ 20.0 0.0 -10.0 --10.0 --100.0 -50.0 --10.0 --10.0 -10.0 -10.0 -10.0 --100.0 -100.0