You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Expense {
private String category;
private double amount;
public Expense(String category, double amount) {
this.category = category;
this.amount = amount;
}
public String getCategory() {
return category;
}
public double getAmount() {
return amount;
}
}
class ExpenseTracker {
private List expenses;
public ExpenseTracker() {
expenses = new ArrayList<>();
}
public void addExpense(String category, double amount) {
Expense expense = new Expense(category, amount);
expenses.add(expense);
}
public double getTotalExpenses() {
double total = 0;
for (Expense expense : expenses) {
total += expense.getAmount();
}
return total;
}
public void printExpenses() {
for (Expense expense : expenses) {
System.out.println("Category: " + expense.getCategory() + ", Amount: " + expense.getAmount());
}
}
}
public class Main {
public static void main(String[] args) {
ExpenseTracker expenseTracker = new ExpenseTracker();
Scanner scanner = new Scanner(System.in);
boolean isRunning = true;
while (isRunning) {
System.out.println("Enter category (or 'q' to quit):");
String category = scanner.nextLine();
if (category.equalsIgnoreCase("q")) {
isRunning = false;
break;
}
System.out.println("Enter amount:");
double amount = scanner.nextDouble();
scanner.nextLine(); // Consume the newline character
expenseTracker.addExpense(category, amount);
}
System.out.println("Total expenses: " + expenseTracker.getTotalExpenses());
System.out.println("Expense breakdown:");
expenseTracker.printExpenses();
scanner.close();
}
}
The text was updated successfully, but these errors were encountered:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Expense {
private String category;
private double amount;
}
class ExpenseTracker {
private List expenses;
}
public class Main {
public static void main(String[] args) {
ExpenseTracker expenseTracker = new ExpenseTracker();
Scanner scanner = new Scanner(System.in);
}
The text was updated successfully, but these errors were encountered: