Skip to content

Commit

Permalink
CommandEdit feature optimised.
Browse files Browse the repository at this point in the history
Pending optimisation for CommandHelp and CommandExpendedDay

Signed-off-by: AjeyAshok <[email protected]>
  • Loading branch information
AjeyAshok committed Nov 10, 2019
1 parent 8605521 commit e14e434
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
2 changes: 2 additions & 0 deletions data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"ID","Tag","Amount","Date"
"1.","[Expenses]","4.44","2019-10-10"
67 changes: 61 additions & 6 deletions src/main/java/executor/command/CommandGetSpendingByDay.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package executor.command;

import duke.exception.DukeException;
import interpreter.Parser;
import storage.StorageManager;
import ui.UiCode;

import java.time.LocalDate;

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.
Expand All @@ -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;
}
}
}

0 comments on commit e14e434

Please sign in to comment.