diff --git a/src/main/java/seedu/moneygowhere/logic/commands/BudgetCommand.java b/src/main/java/seedu/moneygowhere/logic/commands/BudgetCommand.java index 4925d5eb099..d31bc6e0ff4 100644 --- a/src/main/java/seedu/moneygowhere/logic/commands/BudgetCommand.java +++ b/src/main/java/seedu/moneygowhere/logic/commands/BudgetCommand.java @@ -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. @@ -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 diff --git a/src/main/java/seedu/moneygowhere/model/budget/Budget.java b/src/main/java/seedu/moneygowhere/model/budget/Budget.java index d81a6236422..b13006bad12 100644 --- a/src/main/java/seedu/moneygowhere/model/budget/Budget.java +++ b/src/main/java/seedu/moneygowhere/model/budget/Budget.java @@ -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(); } diff --git a/src/main/java/seedu/moneygowhere/ui/BudgetPanel.java b/src/main/java/seedu/moneygowhere/ui/BudgetPanel.java index 22f287f9744..a2cd0df4573 100644 --- a/src/main/java/seedu/moneygowhere/ui/BudgetPanel.java +++ b/src/main/java/seedu/moneygowhere/ui/BudgetPanel.java @@ -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. @@ -29,11 +30,14 @@ public class BudgetPanel extends UiPart { 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(); } @@ -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; @@ -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); } } diff --git a/src/main/java/seedu/moneygowhere/ui/MainWindow.java b/src/main/java/seedu/moneygowhere/ui/MainWindow.java index e9d07cdc286..552008a396b 100644 --- a/src/main/java/seedu/moneygowhere/ui/MainWindow.java +++ b/src/main/java/seedu/moneygowhere/ui/MainWindow.java @@ -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; @@ -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()); @@ -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); diff --git a/src/test/java/seedu/moneygowhere/logic/commands/BudgetCommandIntegrationTest.java b/src/test/java/seedu/moneygowhere/logic/commands/BudgetCommandIntegrationTest.java new file mode 100644 index 00000000000..9ca7488d0dd --- /dev/null +++ b/src/test/java/seedu/moneygowhere/logic/commands/BudgetCommandIntegrationTest.java @@ -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); + } +} diff --git a/src/test/java/seedu/moneygowhere/logic/commands/BudgetCommandTest.java b/src/test/java/seedu/moneygowhere/logic/commands/BudgetCommandTest.java index ed97ca3a479..5696c0920a9 100644 --- a/src/test/java/seedu/moneygowhere/logic/commands/BudgetCommandTest.java +++ b/src/test/java/seedu/moneygowhere/logic/commands/BudgetCommandTest.java @@ -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);