forked from nusCS2113-AY1920S1/PersonalAssistant-Duke
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from priyan-coder/create-budget
Able to set budget and display percentage details based on expenditure to resolve issues #253 , #254
- Loading branch information
Showing
6 changed files
with
117 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters