Skip to content

Commit

Permalink
Merge pull request #225 from austinsantoso/branch-currencyForBudget
Browse files Browse the repository at this point in the history
integrate currency with budget
  • Loading branch information
Nanosync authored Nov 7, 2019
2 parents 1421b5a + f23fc89 commit a542bd6
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import seedu.moneygowhere.logic.commands.exceptions.CommandException;
import seedu.moneygowhere.model.Model;
import seedu.moneygowhere.model.budget.Budget;
import seedu.moneygowhere.model.currency.Currency;

/**
* Sets the budget of the MoneyGoWhere list.
Expand All @@ -32,13 +33,22 @@ public BudgetCommand(Budget budget) {
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

Currency currencyInUse = model.getCurrencyInUse();

double budgetAmount = budget.getAmount();
String budgetString = budget.toString();

if (!model.getCurrencyInUse().name.equalsIgnoreCase("SGD")) {
budget = new Budget(budgetAmount / currencyInUse.rate);
}

if (budget.getAmount() < 0 || budget.getAmount() > 1000000000) {
throw new CommandException(Messages.MESSAGE_INVALID_BUDGET_AMOUNT);
}

model.setBudgetAmount(budget);

return new CommandResult(MESSAGE_SUCCESS + budget.toString());
return new CommandResult(MESSAGE_SUCCESS + currencyInUse.symbol + budgetString);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/moneygowhere/model/budget/Budget.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public int hashCode() {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(String.format("$%.2f", amount));
sb.append(String.format("%.2f", amount));
return sb.toString();
}

Expand Down
19 changes: 13 additions & 6 deletions src/main/java/seedu/moneygowhere/ui/BudgetPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import seedu.moneygowhere.model.budget.Budget;
import seedu.moneygowhere.model.currency.Currency;

/**
* The UI component that is responsible for Showing the current monthly budget and total spending.
Expand All @@ -29,11 +30,14 @@ public class BudgetPanel extends UiPart<Region> {
private double amount;
private double sum;

public BudgetPanel(Budget budget) {
private Currency currencyInUse;

public BudgetPanel(Budget budget, Currency currencyInUse) {
super(FXML);

this.amount = budget.getAmount();
this.sum = budget.getSum();
this.currencyInUse = currencyInUse;

update();
}
Expand All @@ -53,19 +57,21 @@ private void update() {
* Based on the new {@code Budget}
* @param budget the budget to get data to update.
*/
public void update(Budget budget) {
if (this.amount != budget.getAmount() || this.sum != budget.getSum()) {
public void update(Budget budget, Currency currencyInUse) {
if (this.amount != budget.getAmount() || this.sum != budget.getSum()
|| !this.currencyInUse.equals(currencyInUse)) {
this.amount = budget.getAmount();
this.sum = budget.getSum();
this.currencyInUse = currencyInUse;
update();
}
}

private String getRemainingBudgetText() {
double percentDiff = (amount - sum) / amount;
double remainingAmount = Math.abs(amount - sum);
double remainingAmount = Math.abs(amount - sum) * currencyInUse.rate;

String defaultOutput = String.format("$%.2f", remainingAmount);
String defaultOutput = currencyInUse.symbol + String.format("%.2f", remainingAmount);
if (percentDiff < 0) {
remainingBudget.setTextFill(Color.web("#FF0000"));
return "-" + defaultOutput;
Expand Down Expand Up @@ -95,6 +101,7 @@ private void updateTitleText() {
}

private String getBudgetAmount() {
return "This month's budget: $" + String.format("%.02f", amount);
return "This month's budget: " + currencyInUse.symbol
+ String.format("%.02f", amount * currencyInUse.rate);
}
}
13 changes: 7 additions & 6 deletions src/main/java/seedu/moneygowhere/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import seedu.moneygowhere.commons.core.GuiSettings;
import seedu.moneygowhere.commons.core.LogsCenter;
import seedu.moneygowhere.logic.Logic;
Expand Down Expand Up @@ -144,12 +145,12 @@ void fillInnerParts() {
CommandBox commandBox = new CommandBox(this::executeCommand, this::getPrevCommand, this::getNextCommand);
commandBoxPlaceholder.getChildren().add(commandBox.getRoot());

BudgetPanel bp = new BudgetPanel(logic.getSpendingBook().getBudget());
Currency currencyInUse = logic.getSpendingBook().getCurrencyInUse();

BudgetPanel bp = new BudgetPanel(logic.getSpendingBook().getBudget(), currencyInUse);
budgetPanel = bp;
budgetPanelPlaceholder.getChildren().add(bp.getRoot());

Currency currencyInUse = logic.getSpendingBook().getCurrencyInUse();

graphTab = new Tab("Graph");
graphPanel = new GraphPanel(logic.getGraphData(), "Graph\n", currencyInUse);
graphTab.setContent(graphPanel.getRoot());
Expand Down Expand Up @@ -216,14 +217,14 @@ private CommandResult executeCommand(String commandText) throws CommandException
logger.info("Result: " + commandResult.getFeedbackToUser());
resultDisplay.setFeedbackToUser(commandResult.getFeedbackToUser());

budgetPanel.update(logic.getSpendingBook().getBudget());
Currency currencyInUse = logic.getSpendingBook().getCurrencyInUse();

budgetPanel.update(logic.getSpendingBook().getBudget(), currencyInUse);

if (commandResult.isExit()) {
handleExit();
}

Currency currencyInUse = logic.getSpendingBook().getCurrencyInUse();

if (commandResult.isShowGraph()) {
graphPanel = new GraphPanel(logic.getGraphData(), commandResult.getFeedbackToUser(), currencyInUse);
tabPanePlaceholder.getSelectionModel().select(graphTab);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package seedu.moneygowhere.logic.commands;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static seedu.moneygowhere.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.moneygowhere.testutil.TypicalSpendings.getTypicalSpendingBook;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import seedu.moneygowhere.model.Model;
import seedu.moneygowhere.model.ModelManager;
import seedu.moneygowhere.model.UserPrefs;
import seedu.moneygowhere.model.budget.Budget;
import seedu.moneygowhere.model.currency.Currency;

public class BudgetCommandIntegrationTest {

private Model model;

@BeforeEach
public void setUp() {
model = new ModelManager(getTypicalSpendingBook(), new UserPrefs());
}

@Test
public void execute_setNewBudget_success() {
Budget budget = new Budget(10000);

ModelManager expectedModel = new ModelManager(model.getSpendingBook(), new UserPrefs());
expectedModel.setBudget(budget);

String expectedMessage = BudgetCommand.MESSAGE_SUCCESS + model.getCurrencyInUse().symbol + budget.toString();

assertCommandSuccess(new BudgetCommand(budget), model, expectedMessage, expectedModel);
}

@Test
public void execute_setNewBudgetWithChangedCurrency_success() {
Budget budget = new Budget(10000);

ModelManager expectedModel = new ModelManager(model.getSpendingBook(), new UserPrefs());
expectedModel.setBudget(budget);

Currency usdCurrency = null;
for (Currency currency : model.getSpendingBook().getCurrencies()) {
if (currency.name.equalsIgnoreCase("USD")) {
usdCurrency = currency;
}
}

assertNotNull(usdCurrency);

double updatedAmount = budget.getAmount() / usdCurrency.rate;
Budget newBudget = new Budget(updatedAmount);

expectedModel.setCurrencyInUse(usdCurrency);
expectedModel.setBudget(newBudget);

model.setCurrencyInUse(usdCurrency);

String expectedMessage = BudgetCommand.MESSAGE_SUCCESS + model.getCurrencyInUse().symbol + budget.toString();

assertCommandSuccess(new BudgetCommand(budget), model, expectedMessage, expectedModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void execute_setNewBudget_success() {
ModelManager expectedModel = new ModelManager(model.getSpendingBook(), new UserPrefs());
expectedModel.setBudget(budget);

String expectedMessage = BudgetCommand.MESSAGE_SUCCESS + budget.toString();
String expectedMessage = BudgetCommand.MESSAGE_SUCCESS + model.getCurrencyInUse().symbol + budget.toString();

try {
CommandResult result = budgetCommand.execute(model);
Expand Down

0 comments on commit a542bd6

Please sign in to comment.