forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpenseTracker.java
74 lines (65 loc) · 2.47 KB
/
ExpenseTracker.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Expense {
private String description;
private double amount;
public Expense(String description, double amount) {
this.description = description;
this.amount = amount;
}
public String getDescription() {
return description;
}
public double getAmount() {
return amount;
}
@Override
public String toString() {
return "Description: " + description + ", Amount: $" + amount;
}
}
public class ExpenseTracker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Expense> expenses = new ArrayList<>();
double totalExpenses = 0.0;
while (true) {
System.out.println("Expense Tracker Menu:");
System.out.println("1. Add an expense");
System.out.println("2. View expenses");
System.out.println("3. Exit");
System.out.print("Select an option (1/2/3): ");
int option = scanner.nextInt();
switch (option) {
case 1:
scanner.nextLine(); // Consume the newline character
System.out.print("Enter expense description: ");
String description = scanner.nextLine();
System.out.print("Enter expense amount: $");
double amount = scanner.nextDouble();
expenses.add(new Expense(description, amount));
totalExpenses += amount;
System.out.println("Expense added successfully.");
break;
case 2:
if (expenses.isEmpty()) {
System.out.println("No expenses recorded yet.");
} else {
System.out.println("List of Expenses:");
for (Expense expense : expenses) {
System.out.println(expense);
}
System.out.println("Total Expenses: $" + totalExpenses);
}
break;
case 3:
System.out.println("Exiting the Expense Tracker.");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid option. Please select 1, 2, or 3.");
}
}
}
}