-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExpenseDataFetcher.java
98 lines (77 loc) · 3.84 KB
/
ExpenseDataFetcher.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
94
95
96
97
98
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class ExpenseDataFetcher {
public static Map<String, Double> getMonthlyExpenses(String year, String month) throws SQLException {
Map<String, Double> expenses = new HashMap<>();
String query = "SELECT category, SUM(amount) AS total " +
"FROM expenditures WHERE YEAR(date) = ? AND MONTH(date) = ? GROUP BY category";
try (Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, year);
statement.setString(2, month);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
String category = resultSet.getString("category");
double total = resultSet.getDouble("total");
expenses.put(category, total);
}
}
return expenses;
}
public static Map<String, Double> getYearlyExpenses(String year) throws SQLException {
Map<String, Double> expenses = new HashMap<>();
String query = "SELECT category, SUM(amount) AS total " +
"FROM expenditures WHERE YEAR(date) = ? GROUP BY category";
try (Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, year);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
String category = resultSet.getString("category");
double total = resultSet.getDouble("total");
expenses.put(category, total);
}
}
return expenses;
}
public static Map<String, Double> getYearlyMonthlyExpenses(String year) throws SQLException {
Map<String, Double> expenses = new HashMap<>();
String query = "SELECT MONTH(date) AS month, SUM(amount) AS total " +
"FROM expenditures WHERE YEAR(date) = ? GROUP BY MONTH(date)";
try (Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, year);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
String month = resultSet.getString("month");
double total = resultSet.getDouble("total");
// Convert month number to month name
String monthName = new java.text.DateFormatSymbols().getMonths()[Integer.parseInt(month) - 1];
expenses.put(monthName, total);
}
}
return expenses;
}
public static Map<String, Double> getDailyExpenses(String year, String month) throws SQLException {
Map<String, Double> expenses = new HashMap<>();
String query = "SELECT DAY(date) AS day, SUM(amount) AS total " +
"FROM expenditures WHERE YEAR(date) = ? AND MONTH(date) = ? " +
"GROUP BY DAY(date)";
try (Connection connection = DatabaseConnection.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, year);
statement.setString(2, month);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
String day = resultSet.getString("day");
double total = resultSet.getDouble("total");
expenses.put(day, total);
}
}
return expenses;
}
}