Skip to content

Commit

Permalink
Merge pull request #255 from priyan-coder/create-budget
Browse files Browse the repository at this point in the history
Able to set budget and display percentage details based on expenditure to resolve issues #253 , #254
  • Loading branch information
priyan-coder authored Nov 7, 2019
2 parents cedd8d9 + 64dea8e commit 6c4e10d
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 10 deletions.
4 changes: 1 addition & 3 deletions savedTask.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
TODO Refactor Code ####false
EVENT Demo/at tmr####false
DEADLINE Submit PPP/by Monday####false
TODO test ####false
5 changes: 3 additions & 2 deletions savedWallet.txt
Original file line number Diff line number Diff line change
@@ -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
101 changes: 101 additions & 0 deletions src/main/java/executor/command/CommandBudget.java
Original file line number Diff line number Diff line change
@@ -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 $<amount>\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 $<amount> \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;
}
}
3 changes: 1 addition & 2 deletions src/main/java/executor/command/CommandType.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package executor.command;

import ui.gui.CommandLineDisplay;

public enum CommandType {
TASK(CommandNewTask.class),
BYE(CommandBye.class),
Expand All @@ -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),
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/executor/command/CommandUpdateBalance.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

public class CommandUpdateBalance extends Command {


private Double newBalance;

/**
Expand All @@ -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 $<amount>";
}

@Override
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/ui/ReceiptTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class ReceiptTracker extends ArrayList<Receipt> {
private HashMap<String, ReceiptTracker> folders;
private Double totalCashSpent;
private Double budget;

/**
* Overloaded Constructor for ReceiptTracker.
Expand Down Expand Up @@ -231,4 +232,12 @@ public String getPrintableReceipts() {
}
return outputStr.toString();
}

public Double getBudget() {
return budget;
}

public void setBudget(Double budget) {
this.budget = budget;
}
}

0 comments on commit 6c4e10d

Please sign in to comment.