This repository was archived by the owner on Jul 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpenseTracker.java
More file actions
55 lines (46 loc) · 1.44 KB
/
Copy pathExpenseTracker.java
File metadata and controls
55 lines (46 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.*;
class Expense {
String category;
double amount;
Expense(String category, double amount) {
this.category = category;
this.amount = amount;
}
}
public class ExpenseTracker {
static ArrayList<Expense> expenses = new ArrayList<>();
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("\n1.Add Expense 2.View Expenses 3.Total 4.Exit");
int choice = sc.nextInt();
switch (choice) {
case 1 -> addExpense();
case 2 -> viewExpenses();
case 3 -> totalExpense();
case 4 -> System.exit(0);
default -> System.out.println("Invalid choice");
}
}
}
static void addExpense() {
System.out.print("Category: ");
String category = sc.next();
System.out.print("Amount: ");
double amount = sc.nextDouble();
expenses.add(new Expense(category, amount));
System.out.println("Expense Added");
}
static void viewExpenses() {
for (Expense e : expenses) {
System.out.println(e.category + " : ₹" + e.amount);
}
}
static void totalExpense() {
double total = 0;
for (Expense e : expenses) {
total += e.amount;
}
System.out.println("Total Expense: ₹" + total);
}
}