From e14e4340face65a4c240f8df067e359d4d7011c0 Mon Sep 17 00:00:00 2001 From: AjeyAshok Date: Sun, 10 Nov 2019 21:34:32 +0800 Subject: [PATCH] CommandEdit feature optimised. Pending optimisation for CommandHelp and CommandExpendedDay Signed-off-by: AjeyAshok --- data.csv | 2 + .../command/CommandGetSpendingByDay.java | 67 +++++++++++++++++-- 2 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 data.csv diff --git a/data.csv b/data.csv new file mode 100644 index 0000000000..5994e3b4c2 --- /dev/null +++ b/data.csv @@ -0,0 +1,2 @@ +"ID","Tag","Amount","Date" +"1.","[Expenses]","4.44","2019-10-10" diff --git a/src/main/java/executor/command/CommandGetSpendingByDay.java b/src/main/java/executor/command/CommandGetSpendingByDay.java index 164ce205ce..47f904f2a2 100644 --- a/src/main/java/executor/command/CommandGetSpendingByDay.java +++ b/src/main/java/executor/command/CommandGetSpendingByDay.java @@ -1,6 +1,7 @@ package executor.command; import duke.exception.DukeException; +import interpreter.Parser; import storage.StorageManager; import ui.UiCode; @@ -8,6 +9,9 @@ public class CommandGetSpendingByDay extends Command { protected String userInput; + protected Double totalMoney = 0.0; + protected String userDateInput = null; + public boolean isFutureDate = false; /** * Constructor to show what the class is able to do. @@ -23,17 +27,68 @@ public CommandGetSpendingByDay(String userInput) { @Override public void execute(StorageManager storageManager) { - String currDate = LocalDate.now().toString(); - Double totalMoney; + if (storageManager.getWallet().getReceipts().size() == 0) { + outputError("No receipts found in storage"); + return; + } try { - totalMoney = storageManager.getReceiptsByDate(currDate).getNettCashSpent(); + userDateInput = Parser.parseForPrimaryInput(CommandType.EXPENDEDDAY, userInput); + + checkIfDateIsInFuture(userDateInput); + if (isFutureDate) { + totalMoney = storageManager.getReceiptsByDate(userDateInput).getNettCashSpent(); + outputMessageOnGui("The total amount of money spent on " + + userDateInput + " is $" + totalMoney + + "\nNOTE : The date input is in the future"); +// outputError("Date input is in the future"); + return; + } + if (userDateInput.equals("today")) { + String dateToday = LocalDate.now().toString(); + totalMoney = storageManager.getReceiptsByDate(dateToday).getNettCashSpent(); + outputMessageOnGui("The total amount of money spent today " + + "(" + dateToday + ") " + "is $" + totalMoney); + return; + } else if (userDateInput.equals("yesterday")) { + String dateYesterday = LocalDate.now().minusDays(1).toString(); + totalMoney = storageManager.getReceiptsByDate(dateYesterday).getNettCashSpent(); + outputMessageOnGui("The total amount of money spent yesterday " + + "(" + dateYesterday + ") " + "is $" + totalMoney); + return; + } else { + totalMoney = storageManager.getReceiptsByDate(userDateInput).getNettCashSpent(); + outputMessageOnGui("The total amount of money spent on " + + userDateInput + " is $" + totalMoney); + } } catch (DukeException e) { this.infoCapsule.setCodeError(); this.infoCapsule.setOutputStr(e.getMessage()); return; } - this.infoCapsule.setUiCode(UiCode.CLI); - this.infoCapsule.setOutputStr("The total amount of money spent today" - + "(" + currDate + ") " + "is $" + totalMoney); + } + + /** + * Function to output a String message as a pop-up below GUI when an error is encountered. + * @param data is the output message + */ + public void outputError(String data) { + this.infoCapsule.setCodeError(); + this.infoCapsule.setOutputStr(data); + } + + /** + * Function to output a String message on the GUI when an error is encountered. + * @param errorMessage is the output message + */ + public void outputMessageOnGui(String errorMessage) { + this.infoCapsule.setCodeCli(); + this.infoCapsule.setOutputStr(errorMessage); + } + + public void checkIfDateIsInFuture(String userDateInput) { + LocalDate tempDate = LocalDate.parse(userDateInput); + if(tempDate.isAfter(LocalDate.now())){ + isFutureDate = true; + } } }