-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpenseManager.java
More file actions
134 lines (85 loc) · 3.48 KB
/
ExpenseManager.java
File metadata and controls
134 lines (85 loc) · 3.48 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package ExpenseTracker;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
public class ExpenseManager {
Scanner sc = new Scanner(System.in);
public void addExpense(int userId) {
try {
System.out.print("Enter Category: ");
String category = sc.nextLine();
System.out.print("Enter Amount: ");
double amount = sc.nextDouble();
sc.nextLine();
System.out.print("Enter Description: ");
String description = sc.nextLine();
Connection con = DatabaseConnection.getConnection();
String sql = "INSERT INTO expenses(user_id,category,amount,description,date) VALUES(?,?,?,?,CURDATE())";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, userId);
ps.setString(2, category);
ps.setDouble(3, amount);
ps.setString(4, description);
ps.executeUpdate();
System.out.println("Expense Added Successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
public void viewExpenses(int userId) {
try {
Connection con = DatabaseConnection.getConnection();
String sql = "SELECT * FROM expenses WHERE user_id=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, userId);
ResultSet rs = ps.executeQuery();
System.out.println("\n---- Expense List ----");
while (rs.next()) {
System.out.println(
rs.getInt("id") + " | " +
rs.getString("category") + " | " +
rs.getDouble("amount") + " | " +
rs.getString("description") + " | " +
rs.getDate("date")
);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteExpense(int userId) {
try {
System.out.print("Enter Expense ID to delete: ");
int expenseId = sc.nextInt();
Connection con = DatabaseConnection.getConnection();
String sql = "DELETE FROM expenses WHERE id=? AND user_id=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, expenseId);
ps.setInt(2, userId);
int rows = ps.executeUpdate();
if (rows > 0) {
System.out.println("Expense Deleted Successfully");
} else {
System.out.println("Expense Not Found");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void monthlyReport(int userId) {
try {
Connection con = DatabaseConnection.getConnection();
String sql = "SELECT SUM(amount) AS total FROM expenses WHERE user_id=? AND MONTH(date)=MONTH(CURDATE())";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, userId);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
double total = rs.getDouble("total");
System.out.println("Total Expense This Month: " + total);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}