diff --git a/data/DefaultCurrency.txt b/data/DefaultCurrency.txt index e69de29bb2..822166bb78 100644 --- a/data/DefaultCurrency.txt +++ b/data/DefaultCurrency.txt @@ -0,0 +1 @@ +Default Currency: SGD \ No newline at end of file diff --git a/data/ExpenseFile.txt b/data/ExpenseFile.txt index e69de29bb2..78db31319c 100644 --- a/data/ExpenseFile.txt +++ b/data/ExpenseFile.txt @@ -0,0 +1,2 @@ +2024-04-07 | Transport | 100.00 | bus +2024-04-07 | Transport | 60.00 | train diff --git a/src/main/java/seedu/budgetbuddy/Ui.java b/src/main/java/seedu/budgetbuddy/Ui.java index 274bfc664b..7f9e0a57e5 100644 --- a/src/main/java/seedu/budgetbuddy/Ui.java +++ b/src/main/java/seedu/budgetbuddy/Ui.java @@ -1,5 +1,7 @@ package seedu.budgetbuddy; +import java.util.Scanner; + public class Ui { private static final String DIVIDER = "__________________________________________________"; @@ -41,6 +43,14 @@ public void showMenuTitles() { System.out.println(DIVIDER); } + // Method to get user confirmation from the console + public boolean getUserConfirmation() { + Scanner scanner = new Scanner(System.in); + System.out.println("Do you want to proceed with adding this expense? (yes/no)"); + String userInput = scanner.nextLine().trim().toLowerCase(); + return userInput.equals("yes"); + } + /** * Displays the menu item based on the given index. * @param index The index of the menu item to display. diff --git a/src/main/java/seedu/budgetbuddy/commons/ExpenseList.java b/src/main/java/seedu/budgetbuddy/commons/ExpenseList.java index 27a0575afe..ab5d031fc9 100644 --- a/src/main/java/seedu/budgetbuddy/commons/ExpenseList.java +++ b/src/main/java/seedu/budgetbuddy/commons/ExpenseList.java @@ -173,6 +173,32 @@ public void addExpense(String category, String amount, String description) throw throw new BudgetBuddyException("Expenses should not be negative."); } + // Check against the budget before adding the expense + Budget budgetForCategory = budgets.stream() + .filter(budget -> budget.getCategory().equalsIgnoreCase(category)) + .findFirst() + .orElse(null); + + if (budgetForCategory != null) { + double totalSpent = expenses.stream() + .filter(expense -> expense.getCategory().equalsIgnoreCase(category)) + .mapToDouble(Expense::getAmount) + .sum(); + double projectedTotal = totalSpent + amountAsDouble; + + if (projectedTotal > budgetForCategory.getBudget()) { + ui.printDivider(); + System.out.println("Warning: Adding this expense will exceed your budget for " + category); + ui.printDivider(); + + // Replace with actual user confirmation in your application context + if (!ui.getUserConfirmation()) { + System.out.println("Expense not added due to budget constraints."); + return; // Exit without adding the expense + } + } + } + Expense expense = new Expense(category, amountAsDouble, description); expenses.add(expense);