diff --git a/savedTask.txt b/savedTask.txt index ad89ce3485..e57d5d909f 100644 --- a/savedTask.txt +++ b/savedTask.txt @@ -1,3 +1 @@ -TODO Refactor Code ####false -EVENT Demo/at tmr####false -DEADLINE Submit PPP/by Monday####false +TODO test ####false diff --git a/savedWallet.txt b/savedWallet.txt index e7b29f5e69..ad201ea731 100644 --- a/savedWallet.txt +++ b/savedWallet.txt @@ -1,3 +1,4 @@ -1000.0 +500.0 +out $250.0 /date 2019-11-07 /tags out $100.0 /date 2019-11-07 /tags -out $100.0 /date 2019-11-07 /tags +in $100.0 /date 2019-11-07 /tags diff --git a/src/main/java/executor/command/CommandBudget.java b/src/main/java/executor/command/CommandBudget.java new file mode 100644 index 0000000000..e1566a7842 --- /dev/null +++ b/src/main/java/executor/command/CommandBudget.java @@ -0,0 +1,101 @@ +package executor.command; + +import duke.exception.DukeException; +import interpreter.Parser; +import storage.StorageManager; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.text.DecimalFormat; + +public class CommandBudget extends Command { + + private Double budgetAmount; + + public CommandBudget(String userInput) { + super(); + this.userInput = userInput; + this.description = " Sets user budget \n" + + "FORMAT : budget $\n"; + this.commandType = CommandType.BUDGET; + } + + @Override + public void execute(StorageManager storageManager) { + try { + this.budgetAmount = extractAmount(); + DecimalFormat decimalFormat = new DecimalFormat("#0.00"); + storageManager.getWallet().getReceipts().setBudget(this.budgetAmount); + String percentageExceeded = percentageOfBudgetExceeded(storageManager.getWalletExpenses()); + String percentageUsedUp = percentageOfBudgetUsedUp(storageManager.getWalletExpenses()); + this.infoCapsule.setCodeToast(); + this.infoCapsule.setOutputStr("Budget updated to: $" + decimalFormat.format(this.budgetAmount) +"\n" + + percentageExceeded + "\n" + + percentageUsedUp + "\n" + + "\n"); + + } catch (DukeException e) { + this.infoCapsule.setCodeError(); + this.infoCapsule.setOutputStr(e.getMessage()); + return; + } + } + + private Double extractAmount() throws DukeException { + String userBudget = Parser.parseForPrimaryInput(this.commandType, this.userInput); + try { + userBudget = userBudget.trim().replace("$", ""); + Double amount = Double.parseDouble(userBudget); + if (amount <= 0) { + throw new Exception(); + } + return amount; + } catch (Exception e) { + throw new DukeException("Please kindly follow the format : budget $ \n" + + "Please enter an amount greater than zero!\n"); + } + } + + private String percentageOfBudgetExceeded(Double amountSpent) throws DukeException { + try { + if (amountSpent <= getBudgetAmount()) { + return "You are still good and safe as you did not overspend your budget ;) \n"; + } + + Double percent = ((amountSpent - getBudgetAmount()) / getBudgetAmount()) * 100; + Double percentage = roundByDecimalPlace(percent , 2); + return "Percentage of Budget Exceeded :"+ percentage.toString() + "%"; + } catch (Exception e) { + throw new DukeException("Unable to calculate percentage overspent!\n"); + } + } + + private String percentageOfBudgetUsedUp(Double amountSpent) throws DukeException { + try { + if (amountSpent <= getBudgetAmount()) { + Double percent = ((getBudgetAmount() - amountSpent) / getBudgetAmount()) * 100; + Double percentage = roundByDecimalPlace(percent, 2); + return "Percentage of Budget Used Up : " + percentage.toString() + "%"; + } + + return "You have already exceeded your budget !! \n"; + + } catch (Exception e) { + throw new DukeException("Unable to calculate percentage overspent!\n"); + } + } + + + private double roundByDecimalPlace(double value, int places) { + if (places < 0) { + throw new IllegalArgumentException(); + } + BigDecimal bd = BigDecimal.valueOf(value); + bd = bd.setScale(places, RoundingMode.HALF_UP); + return bd.doubleValue(); + } + + private Double getBudgetAmount() { + return budgetAmount; + } +} diff --git a/src/main/java/executor/command/CommandType.java b/src/main/java/executor/command/CommandType.java index 7994e65cb3..a9c98a8ad5 100644 --- a/src/main/java/executor/command/CommandType.java +++ b/src/main/java/executor/command/CommandType.java @@ -1,7 +1,5 @@ package executor.command; -import ui.gui.CommandLineDisplay; - public enum CommandType { TASK(CommandNewTask.class), BYE(CommandBye.class), @@ -19,6 +17,7 @@ public enum CommandType { IN(CommandAddIncomeReceipt.class), OUT(CommandAddSpendingReceipt.class), SETBALANCE(CommandUpdateBalance.class), + BUDGET(CommandBudget.class), EXPENSES(CommandDisplayExpenditure.class), HELP(CommandHelp.class), DEADLINE(CommandNewTask.class), diff --git a/src/main/java/executor/command/CommandUpdateBalance.java b/src/main/java/executor/command/CommandUpdateBalance.java index 6e3b09e425..c06ccff9f3 100644 --- a/src/main/java/executor/command/CommandUpdateBalance.java +++ b/src/main/java/executor/command/CommandUpdateBalance.java @@ -7,7 +7,6 @@ public class CommandUpdateBalance extends Command { - private Double newBalance; /** @@ -18,8 +17,8 @@ public CommandUpdateBalance(String userInput) { super(); this.userInput = userInput; this.commandType = CommandType.SETBALANCE; - this.description = "Updates current balance to new balance in the wallet \n" - + "FORMAT : "; + this.description = "Updates current balance to new balance in the wallet and can only be set once \n" + + "FORMAT : setbalance $"; } @Override diff --git a/src/main/java/ui/ReceiptTracker.java b/src/main/java/ui/ReceiptTracker.java index 85a972dea6..1515ed94df 100644 --- a/src/main/java/ui/ReceiptTracker.java +++ b/src/main/java/ui/ReceiptTracker.java @@ -8,6 +8,7 @@ public class ReceiptTracker extends ArrayList { private HashMap folders; private Double totalCashSpent; + private Double budget; /** * Overloaded Constructor for ReceiptTracker. @@ -231,4 +232,12 @@ public String getPrintableReceipts() { } return outputStr.toString(); } + + public Double getBudget() { + return budget; + } + + public void setBudget(Double budget) { + this.budget = budget; + } }