-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expense.java
93 lines (84 loc) · 3.52 KB
/
Expense.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
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;
}
}
public class ExpenseTracker {
private static ArrayList<Expense> expenses = new ArrayList<>();
private static Map<String, Double> categoryTotals = new HashMap<>();
private static double totalExpense = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Expense Tracker");
System.out.println("1. Add Expense");
System.out.println("2. View Expenses");
System.out.println("3. View Statistics");
System.out.println("4. Set Budget");
System.out.println("5. Generate Report");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (choice) {
case 1:
System.out.print("Enter expense category: ");
String category = scanner.nextLine();
System.out.print("Enter expense amount: ");
double amount = scanner.nextDouble();
expenses.add(new Expense(category, amount));
updateTotals(category, amount);
totalExpense += amount;
System.out.println("Expense added successfully!");
break;
case 2:
System.out.println("Expenses:");
for (Expense expense : expenses) {
System.out.println("Category: " + expense.getCategory() + ", Amount: $" + expense.getAmount());
}
break;
case 3:
System.out.println("Statistics:");
System.out.println("Total Expense: $" + totalExpense);
System.out.println("Category-wise Expense:");
for (Map.Entry<String, Double> entry : categoryTotals.entrySet()) {
System.out.println(entry.getKey() + ": $" + entry.getValue());
}
break;
case 4:
System.out.print("Enter budget amount: ");
double budget = scanner.nextDouble();
// You can implement budget setting functionality here
System.out.println("Budget set successfully!");
break;
case 5:
// You can implement report generation functionality here
System.out.println("Report generation feature coming soon!");
break;
case 6:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice! Please enter a number between 1 and 6.");
}
System.out.println();
}
}
private static void updateTotals(String category, double amount) {
categoryTotals.put(category, categoryTotals.getOrDefault(category, 0.0) + amount);
}
}