Skip to content

Commit

Permalink
Merge pull request #195 from Dheekshitha2/shree-warningExceedBudget
Browse files Browse the repository at this point in the history
Add warning if user attempts to add expense which exceeds budget
  • Loading branch information
Dheekshitha2 authored Apr 9, 2024
2 parents 21231cb + 9661342 commit 33e14e1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions data/ExpenseFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2024-04-07 | Transport | 100.00 | bus
2024-04-07 | Transport | 60.00 | train
10 changes: 10 additions & 0 deletions src/main/java/seedu/budgetbuddy/Ui.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.budgetbuddy;

import java.util.Scanner;

public class Ui {
private static final String DIVIDER = "__________________________________________________";

Expand Down Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/seedu/budgetbuddy/commons/ExpenseList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 33e14e1

Please sign in to comment.