Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expense tracker #21

Open
Faizan6016 opened this issue Mar 6, 2024 · 0 comments
Open

Expense tracker #21

Faizan6016 opened this issue Mar 6, 2024 · 0 comments

Comments

@Faizan6016
Copy link

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

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();
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant