-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExpenseTracker.java
More file actions
119 lines (99 loc) · 3.7 KB
/
ExpenseTracker.java
File metadata and controls
119 lines (99 loc) · 3.7 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.ArrayList;
import java.util.Scanner;
class Expense {
String date;
String description;
double amount;
public Expense(String date, String description, double amount) {
this.date = date;
this.description = description;
this.amount = amount;
}
}
public class ExpenseTracker {
private static ArrayList<Expense> expenses = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your budget: ");
double budget;
while (true) {
try {
budget = scanner.nextDouble();
break;
} catch (Exception e) {
scanner.nextLine();
}
}
while (true) {
System.out.println("\nExpense Tracker Menu:");
System.out.println("1. Add Expense");
System.out.println("2. View Expenses and Analyze");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice;
while (true) {
try {
choice = scanner.nextInt();
break;
} catch (Exception e) {
scanner.nextLine();
}
}
switch (choice) {
case 1:
addExpense(scanner);
break;
case 2:
viewExpenses(budget);
break;
case 3:
System.out.println("Exiting Expense Tracker. Goodbye!");
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a valid option.");
}
}
}
private static void addExpense(Scanner scanner) {
System.out.println("\nEnter Expense Details:");
System.out.print("Date (D/M/Y): ");
String date = scanner.next();
System.out.print("Description: ");
scanner.nextLine();
String description = scanner.nextLine();
System.out.print("Amount: ");
double amount;
while (true) {
try {
amount = scanner.nextDouble();
break;
} catch (Exception e) {
scanner.nextLine();
}
}
Expense expense = new Expense(date, description, amount);
expenses.add(expense);
System.out.println("Expense added successfully!");
}
private static void viewExpenses(double budget) {
if (expenses.isEmpty()) {
System.out.println("No expenses to display.");
return;
}
System.out.println("\nExpense Summary:");
System.out.printf("%-15s %-20s %-10s\n", "Date", "Description", "Amount (in rupees)");
System.out.println("-------------------------------------------------------");
double totalAmount = 0;
for (Expense expense : expenses) {
System.out.printf("%-15s %-20s %-10.2f\n", expense.date, expense.description, expense.amount);
totalAmount += expense.amount;
}
System.out.println("-------------------------------------------------------");
System.out.printf("%-15s %-20s %-10.2f\n", "Total", "", totalAmount);
if (budget <= totalAmount) {
System.out.printf("Your expenses have exceeded your budget by: %.2f/-\n", totalAmount - budget);
} else {
System.out.printf("You are under your budget by: %.2f/-\n", budget - totalAmount);
}
}
}