Skip to content

Commit

Permalink
CommandHelp, CommandEdit, CommandExpended optimised
Browse files Browse the repository at this point in the history
  • Loading branch information
AjeyAshok committed Nov 10, 2019
1 parent 13d2f2d commit e51a0b5
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 182 deletions.
7 changes: 5 additions & 2 deletions savedWallet.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
0.0
-
in $4.0 /date 2019-11-10 /tags dates

in $4.0 /date 2019-11-10 /tags dates
out $4.0 /date 2019-11-10 /tags dates
out $3.0 /date 2019-11-11 /tags other
out $5.0/date 2019-01-01
out $50.0/date 2097-01-01
21 changes: 9 additions & 12 deletions src/main/java/executor/command/CommandEdit.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ public void execute(StorageManager storageManager) {
index = Integer.parseInt(Parser.parseForPrimaryInput(CommandType.EDIT, userInput)) - 1;
receiptNumber = index + 1;
indexChecker(storageManager);
newTag = Parser.parseForFlag("tag", this.userInput);
newValue = Parser.parseForFlag("value", this.userInput);
newDate = Parser.parseForFlag("date", this.userInput);
checkAndUpdateFlag(storageManager);
} catch (NumberFormatException e) {
outputError("Index has be an INTEGER");
} catch (DukeException f) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr(f.getMessage());
} catch (NumberFormatException e) {
outputError("Index has to be an INTEGER"
+ "\nFORMAT : edit <index> /<part to be edited> <new-input>");
}
}

Expand All @@ -63,6 +61,9 @@ public void execute(StorageManager storageManager) {
* @throws DukeException is the error message
*/
private void checkAndUpdateFlag(StorageManager storageManager) throws DukeException {
newTag = Parser.parseForFlag("tag", this.userInput);
newValue = Parser.parseForFlag("value", this.userInput);
newDate = Parser.parseForFlag("date", this.userInput);
if (newTag != null) {
updateTag(storageManager);
} else if (newValue != null) {
Expand Down Expand Up @@ -103,15 +104,11 @@ private boolean isAlpha(String name) {
/**
* Function to round up a Double from more than 2 DP to 2 DP.
* @param value is the String value given by user
* @param places is the number of decimal places to which the user input is rounded up to
* @return is the rounded up value of the user input
*/
private static double round(double value, int places) {
if (places < 0) {
throw new IllegalArgumentException();
}
private static double round(double value) {
BigDecimal bd = BigDecimal.valueOf(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
bd = bd.setScale(2, RoundingMode.HALF_UP);
return bd.doubleValue();
}

Expand All @@ -121,7 +118,7 @@ private static double round(double value, int places) {
*/
private void checkAndChangeToTwoDP() {
if (BigDecimal.valueOf(changeValue).scale() > 2) {
roundedChangeValue = round(changeValue, 2);
roundedChangeValue = round(changeValue);
moreThanTwoDP = true;
}
}
Expand Down
34 changes: 10 additions & 24 deletions src/main/java/executor/command/CommandGetSpendingByDay.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,9 @@ public void execute(StorageManager storageManager) {
} catch (DukeException e) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr(e.getMessage());
return;
}
}

/**
* 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
*/
private void outputMessageOnGui(String errorMessage) {
this.infoCapsule.setCodeCli();
this.infoCapsule.setOutputStr(errorMessage);
}

/**
* Function to check the user input.
* @param storageManager is the class that contains all the getter functions for the wallet
Expand All @@ -62,7 +43,8 @@ private void outputMessageOnGui(String errorMessage) {
private void checkUserInput(StorageManager storageManager) throws DukeException {
userDateInput = Parser.parseForPrimaryInput(CommandType.EXPENDEDDAY, userInput);
if (storageManager.getWallet().getReceipts().size() == 0) {
outputError("No receipts found in storage");
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("No receipts found in storage");
return;
}
if (!userDateInput.equals("today") && (!userDateInput.equals("yesterday"))) {
Expand Down Expand Up @@ -115,7 +97,8 @@ private void outputExpenditureForInput(StorageManager storageManager) throws Duk
private void expenditureForDateInFuture(StorageManager storageManager) throws DukeException {
String tempDate = dateInLocal.toString();
totalMoney = storageManager.getReceiptsByDate(tempDate).getNettCashSpent();
outputMessageOnGui("The total amount of money spent on "
this.infoCapsule.setCodeCli();
this.infoCapsule.setOutputStr("The total amount of money spent on "
+ dateInLocal + " is $" + totalMoney
+ "\nNOTE : The date input is in the future");
}
Expand All @@ -128,7 +111,8 @@ private void expenditureForDateInFuture(StorageManager storageManager) throws Du
private void expenditureForYesterday(StorageManager storageManager) throws DukeException {
String dateYesterday = LocalDate.now().minusDays(1).toString();
totalMoney = storageManager.getReceiptsByDate(dateYesterday).getNettCashSpent();
outputMessageOnGui("The total amount of money spent yesterday "
this.infoCapsule.setCodeCli();
this.infoCapsule.setOutputStr("The total amount of money spent yesterday "
+ "(" + dateYesterday + ") " + "is $" + totalMoney);
}

Expand All @@ -141,7 +125,8 @@ private void expenditureForToday(StorageManager storageManager) throws DukeExcep

String dateToday = LocalDate.now().toString();
totalMoney = storageManager.getReceiptsByDate(dateToday).getNettCashSpent();
outputMessageOnGui("The total amount of money spent today "
this.infoCapsule.setCodeCli();
this.infoCapsule.setOutputStr("The total amount of money spent today "
+ "(" + dateToday + ") " + "is $" + totalMoney);
}

Expand All @@ -152,7 +137,8 @@ private void expenditureForToday(StorageManager storageManager) throws DukeExcep
*/
private void expenditureForDate(StorageManager storageManager) throws DukeException {
totalMoney = storageManager.getReceiptsByDate(userDateInput).getNettCashSpent();
outputMessageOnGui("The total amount of money spent on "
this.infoCapsule.setCodeCli();
this.infoCapsule.setOutputStr("The total amount of money spent on "
+ userDateInput + " is $" + totalMoney);
}
}
166 changes: 107 additions & 59 deletions src/main/java/executor/command/CommandGetSpendingByMonth.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package executor.command;

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

import java.time.LocalDate;
import java.time.Year;
import java.time.YearMonth;

public class CommandGetSpendingByMonth extends Command {
protected String userInput;
private String monthStr;
private int month;
private int year;
private int daysRemaining;
private Double totalMoney;
private String yearStr;
private boolean isThisMonth = false;

/**
* Constructor to explain about the command.
Expand All @@ -24,67 +34,15 @@ public CommandGetSpendingByMonth(String userInput) {
@Override
public void execute(StorageManager storageManager) {
try {
String monthStr = (Parser.parseForPrimaryInput(CommandType.EXPENDEDMONTH, userInput)).toLowerCase();
if (monthStr.isEmpty()) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("No month input detected. FORMAT : expendedmonth <month> /year <year>");
return;
}

int month = monthStrToInt(monthStr);
if (month == 0) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("Wrong month input. Check Spelling");
return;
}

String yearStr = Parser.parseForFlag("year", userInput);
if (yearStr.isEmpty()) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("No year input detected. FORMAT : expendedmonth <month> /year <year>");
return;
}

if (yearStr.length() != 4) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("Year input contains lesser/extra number of variables. "
+ "\nFORMAT : expendedmonth <month> /year <year>");
return;
}

try {
int year = Integer.parseInt(yearStr);
if (year > Year.now().getValue()) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("Future year entered is invalid!");
return;
}

if (year < 1800) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("Year is too far back into the past");
return;
}
Double totalMoney = storageManager.getReceiptsByMonthDate(month, year).getTotalExpenses();
this.infoCapsule.setUiCode(UiCode.CLI);
this.infoCapsule.setOutputStr("The total amount of money spent in "
+ monthStr
+ " "
+ year
+ " : $"
+ totalMoney
+ "\n");

} catch (Exception e) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("Year input is either a double or contains String values.\n "
+ "FORMAT : expendedmonth <month> /year <year>\n");
return;
}
checkMonthInput();
checkIfYearIsCorrect();
outputOfExpenditure(storageManager);
} catch (DukeException e) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr(e.getMessage());
} catch (Exception e) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr("Wrong format! FORMAT : expendedmonth <month> /year <year>");
return;
}
}

Expand All @@ -93,7 +51,7 @@ public void execute(StorageManager storageManager) {
* @param month is the name of month from the userInput
* @return the value of month, eg: march --> 3.
*/
public int monthStrToInt(String month) {
private int monthStrToInt(String month) {
switch (month) {
case "january":
return 1;
Expand Down Expand Up @@ -123,4 +81,94 @@ public int monthStrToInt(String month) {
return 0;
}
}

/**
* Function to output the expenditure.
* @param storageManager is the class that contains all the getter functions for the wallet
* @throws DukeException is the error message
*/
private void outputOfExpenditure(StorageManager storageManager) throws DukeException {
try {
year = Integer.parseInt(yearStr);
checkCredibilityOfYear();
totalMoney = storageManager.getReceiptsByMonthDate(month, year).getTotalExpenses();
if (isThisMonth) {
this.infoCapsule.setUiCode(UiCode.CLI);
this.infoCapsule.setOutputStr(outputMessage() + "Number of day(s) left in this month is/are "
+ daysRemaining);
} else {
this.infoCapsule.setUiCode(UiCode.CLI);
this.infoCapsule.setOutputStr(outputMessage());
}
} catch (Exception e) {
throw new DukeException("Year input is either a double or contains String values.\n "
+ "FORMAT : expendedmonth <month> /year <year>\n");
}
}

private String outputMessage() {
return "The total amount of money spent in "
+ monthStr
+ " "
+ year
+ " : $"
+ totalMoney
+ "."
+ "\n";
}

/**
* Function to check if the month input is correct.
* @throws DukeException is the error message
*/
private void checkMonthInput() throws DukeException {
monthStr = (Parser.parseForPrimaryInput(CommandType.EXPENDEDMONTH, userInput)).toLowerCase();
if (monthStr.isEmpty()) {
throw new DukeException("No month input detected. FORMAT : expendedmonth <month> /year <year>");
}

if (monthStr.equals(LocalDate.now().getMonth().toString().toLowerCase())) {
isThisMonth = true;
YearMonth yearMonthObject = YearMonth.of(LocalDate.now().getYear(), LocalDate.now().getMonthValue());
int daysInMonth = yearMonthObject.lengthOfMonth();
int currday = LocalDate.now().getDayOfMonth();
daysRemaining = daysInMonth - currday;
}

month = monthStrToInt(monthStr);
if (month == 0) {
throw new DukeException("Wrong month input. Check Spelling");
}
}

/**
* Function to check if the year input is correct.
* @throws DukeException is the error message
*/
private void checkIfYearIsCorrect() throws DukeException {
yearStr = Parser.parseForFlag("year", userInput);
assert yearStr != null;
if (yearStr.isEmpty()) {
throw new DukeException("No year input detected. FORMAT : expendedmonth <month> /year <year>");
}

if (yearStr.length() != 4) {
throw new DukeException("Year input contains lesser/extra number of variables. "
+ "\nFORMAT : expendedmonth <month> /year <year>");
}
}

/**
* Function to check if the year input taken in is credible.
* @throws DukeException is the error message
*/
private void checkCredibilityOfYear() throws DukeException {
if (year > Year.now().getValue()) {
throw new DukeException("Future year entered is invalid!");
}

if (year < 1800) {
throw new DukeException("Year is too far back into the past");
}
}
}
Loading

0 comments on commit e51a0b5

Please sign in to comment.