-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpenseTracker.java
More file actions
192 lines (156 loc) · 6.39 KB
/
ExpenseTracker.java
File metadata and controls
192 lines (156 loc) · 6.39 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Expense Tracker Application in Java
import java.util.*;
import java.sql.*;
public class ExpenseTracker {
private static Connection connection;
public static void main(String[] args) {
try {
connectDatabase();
createTables();
Scanner scanner = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("\nExpense Tracker Menu:");
System.out.println("1. Add Expense");
System.out.println("2. Edit Expense");
System.out.println("3. Delete Expense");
System.out.println("4. View Expenses");
System.out.println("5. Generate Report");
System.out.println("6. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
addExpense(scanner);
break;
case 2:
editExpense(scanner);
break;
case 3:
deleteExpense(scanner);
break;
case 4:
viewExpenses();
break;
case 5:
generateReport();
break;
case 6:
running = false;
break;
default:
System.out.println("Invalid option. Please try again.");
}
}
scanner.close();
closeDatabase();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void connectDatabase() throws SQLException {
String url = "jdbc:sqlite:expenses.db";
connection = DriverManager.getConnection(url);
System.out.println("Connected to the database.");
}
private static void createTables() throws SQLException {
String createTableSQL = "CREATE TABLE IF NOT EXISTS expenses (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"amount REAL, " +
"category TEXT, " +
"date TEXT, " +
"description TEXT)";
Statement stmt = connection.createStatement();
stmt.execute(createTableSQL);
stmt.close();
}
private static void addExpense(Scanner scanner) throws SQLException {
System.out.print("Enter amount: ");
double amount = scanner.nextDouble();
scanner.nextLine(); // Consume newline
System.out.print("Enter category: ");
String category = scanner.nextLine();
System.out.print("Enter date (YYYY-MM-DD): ");
String date = scanner.nextLine();
System.out.print("Enter description: ");
String description = scanner.nextLine();
String insertSQL = "INSERT INTO expenses (amount, category, date, description) VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = connection.prepareStatement(insertSQL);
pstmt.setDouble(1, amount);
pstmt.setString(2, category);
pstmt.setString(3, date);
pstmt.setString(4, description);
pstmt.executeUpdate();
pstmt.close();
System.out.println("Expense added successfully.");
}
private static void editExpense(Scanner scanner) throws SQLException {
System.out.print("Enter expense ID to edit: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter new amount: ");
double amount = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter new category: ");
String category = scanner.nextLine();
System.out.print("Enter new date (YYYY-MM-DD): ");
String date = scanner.nextLine();
System.out.print("Enter new description: ");
String description = scanner.nextLine();
String updateSQL = "UPDATE expenses SET amount = ?, category = ?, date = ?, description = ? WHERE id = ?";
PreparedStatement pstmt = connection.prepareStatement(updateSQL);
pstmt.setDouble(1, amount);
pstmt.setString(2, category);
pstmt.setString(3, date);
pstmt.setString(4, description);
pstmt.setInt(5, id);
pstmt.executeUpdate();
pstmt.close();
System.out.println("Expense updated successfully.");
}
private static void deleteExpense(Scanner scanner) throws SQLException {
System.out.print("Enter expense ID to delete: ");
int id = scanner.nextInt();
String deleteSQL = "DELETE FROM expenses WHERE id = ?";
PreparedStatement pstmt = connection.prepareStatement(deleteSQL);
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
System.out.println("Expense deleted successfully.");
}
private static void viewExpenses() throws SQLException {
String selectSQL = "SELECT * FROM expenses";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(selectSQL);
System.out.println("\nExpenses:");
System.out.println("ID | Amount | Category | Date | Description");
while (rs.next()) {
System.out.printf("%d | %.2f | %s | %s | %s\n",
rs.getInt("id"),
rs.getDouble("amount"),
rs.getString("category"),
rs.getString("date"),
rs.getString("description"));
}
rs.close();
stmt.close();
}
private static void generateReport() throws SQLException {
String reportSQL = "SELECT category, SUM(amount) AS total FROM expenses GROUP BY category";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(reportSQL);
System.out.println("\nExpense Report by Category:");
while (rs.next()) {
System.out.printf("%s: %.2f\n", rs.getString("category"), rs.getDouble("total"));
}
rs.close();
stmt.close();
}
private static void closeDatabase() throws SQLException {
if (connection != null) {
connection.close();
System.out.println("Database connection closed.");
}
}
}