Skip to content

Commit

Permalink
Merge pull request #256 from AjeyAshok/week-expend
Browse files Browse the repository at this point in the history
Feature to get Expenditure for week + checkstyle testing done
  • Loading branch information
AjeyAshok authored Nov 8, 2019
2 parents da00e4f + ff1a23a commit 34d36d1
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 4 deletions.
9 changes: 8 additions & 1 deletion savedWallet.txt
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
500.0

1000.0
out $250.0 /date 2019-11-07 /tags
out $100.0 /date 2019-11-07 /tags
in $100.0 /date 2019-11-07 /tags
out $5.0 /date 2019-11-07 /tags food
out $-0.0 /date 2019-11-07 /tags

4 changes: 3 additions & 1 deletion src/main/java/executor/command/CommandGetSpendingByDay.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

import java.time.LocalDate;

public class CommandGetSpendingByDay extends Command {
Expand Down Expand Up @@ -30,7 +32,7 @@ public void execute(StorageManager storageManager) {
this.infoCapsule.setOutputStr(e.getMessage());
return;
}
this.infoCapsule.setCodeToast();
this.infoCapsule.setUiCode(UiCode.CLI);
this.infoCapsule.setOutputStr("The total amount of money spent today"
+ "(" + currDate + ") " + "is $" + totalMoney);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import interpreter.Parser;
import storage.StorageManager;
import ui.UiCode;

import java.time.Year;

public class CommandGetSpendingByMonth extends Command {
Expand Down Expand Up @@ -64,7 +66,7 @@ public void execute(StorageManager storageManager) {
return;
}
Double totalMoney = storageManager.getReceiptsByMonthDate(month, year).getTotalCashSpent();
this.infoCapsule.setCodeToast();
this.infoCapsule.setUiCode(UiCode.CLI);
this.infoCapsule.setOutputStr("The total amount of money spent in "
+ monthStr
+ " "
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/executor/command/CommandGetSpendingByWeek.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package executor.command;

import interpreter.Parser;
import storage.StorageManager;
import ui.ReceiptTracker;
import ui.UiCode;
import ui.Wallet;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;

public class CommandGetSpendingByWeek extends Command {
protected String userInput;

/**
* Constructor to explain about the command.
* @param userInput is the user's input
*/
public CommandGetSpendingByWeek(String userInput) {
super();
this.commandType = CommandType.EXPENDEDWEEK;
this.userInput = userInput;
this.description = "Provides the user the total expenditure for the current week. \n"
+ "FORMAT : expendedweek";
}

@Override
public void execute(StorageManager storageManager) {

String checker = Parser.parseForPrimaryInput(CommandType.HELP, userInput);
if (!checker.isEmpty()) {
this.infoCapsule.setCodeToast();
this.infoCapsule.setOutputStr("Extra variables added. FORMAT : expendedweek");
}
String day = LocalDate.now().getDayOfWeek().toString().toLowerCase();
String dayDate = LocalDate.now().toString();
ArrayList<String> dateList = new ArrayList<String>();
Double total = 0.0;
int dayInt = dayStrToInt(day);
if (dayInt > 0 && dayInt < 8) {
LocalDate dateTracker = LocalDate.parse(dayDate);
for (int i = 0; i < dayInt; i++) {
dateList.add(dateTracker.minusDays(i).toString());
}
}
for (String a : dateList) {
try {
total += storageManager.getReceiptsByDate(a).getTotalCashSpent();
} catch (Exception e) {
this.infoCapsule.setCodeError();
this.infoCapsule.setOutputStr(e.getMessage());
}
}
int remainingDaysOfWeek = 7 - dayInt;
String out = total.toString();
this.infoCapsule.setUiCode(UiCode.CLI);
this.infoCapsule.setOutputStr("The total amount spent this week is $"
+ out
+ " and there is/are "
+ remainingDaysOfWeek
+ "day(s) to end of week");
}

/**
* Function to get the number of day in the week according to the day.
* @param day is the string input of the current day of the week
* @return is the number value of the day of the week
*/
public int dayStrToInt(String day) {
switch (day) {
case "monday":
return 1;
case "tuesday":
return 2;
case "wednesday":
return 3;
case "thursday":
return 4;
case "friday":
return 5;
case "saturday":
return 6;
case "sunday":
return 7;
default:
return 0;
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/executor/command/CommandGetSpendingByYear.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import interpreter.Parser;
import storage.StorageManager;
import ui.UiCode;

import java.time.Year;

public class CommandGetSpendingByYear extends Command {
Expand Down Expand Up @@ -48,7 +50,7 @@ public void execute(StorageManager storageManager) {
return;
}
Double totalMoney = storageManager.getReceiptsByYear(year).getTotalCashSpent();
this.infoCapsule.setCodeToast();
this.infoCapsule.setUiCode(UiCode.CLI);
this.infoCapsule.setOutputStr("The total amount of money spent in " + year + " : $" + totalMoney + "\n");

} catch (Exception e) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/executor/command/CommandType.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum CommandType {
FDURATION(CommandNewTask.class),
TAGLIST(CommandTagList.class),
EXPENDEDDAY(CommandGetSpendingByDay.class),
EXPENDEDWEEK(CommandGetSpendingByWeek.class),
EXPENDEDMONTH(CommandGetSpendingByMonth.class),
EXPENDEDYEAR(CommandGetSpendingByYear.class),
CONVERT(CommandConvert.class),
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/utils/InfoCapsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import ui.UiCode;
import ui.Wallet;

import java.time.DayOfWeek;

public class InfoCapsule {
private UiCode uiCode;
private String outputStr;
Expand Down

0 comments on commit 34d36d1

Please sign in to comment.