-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFineManagementPanel.java
More file actions
236 lines (196 loc) · 9.04 KB
/
FineManagementPanel.java
File metadata and controls
236 lines (196 loc) · 9.04 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
public class FineManagementPanel extends JPanel {
private JTable fineTable;
private DefaultTableModel tableModel;
private JTextField studentIdField;
private JLabel totalFinesLabel;
public FineManagementPanel() {
setLayout(new BorderLayout());
// Create search panel
JPanel searchPanel = createSearchPanel();
add(searchPanel, BorderLayout.NORTH);
// Create table
createFineTable();
JScrollPane scrollPane = new JScrollPane(fineTable);
add(scrollPane, BorderLayout.CENTER);
// Create bottom panel with total and buttons
JPanel bottomPanel = createBottomPanel();
add(bottomPanel, BorderLayout.SOUTH);
}
private JPanel createSearchPanel() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel.add(new JLabel("Student ID:"));
studentIdField = new JTextField(10);
panel.add(studentIdField);
JButton searchButton = new JButton("Search");
searchButton.addActionListener(e -> searchFines());
panel.add(searchButton);
return panel;
}
private void createFineTable() {
String[] columns = {"Fine ID", "Student Name", "Book Title", "Due Date", "Days Late", "Amount", "Status"};
tableModel = new DefaultTableModel(columns, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
fineTable = new JTable(tableModel);
}
private JPanel createBottomPanel() {
JPanel panel = new JPanel(new BorderLayout());
// Total fines label
totalFinesLabel = new JLabel("Total Outstanding Fines: $0.00");
totalFinesLabel.setFont(new Font("Arial", Font.BOLD, 14));
totalFinesLabel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
panel.add(totalFinesLabel, BorderLayout.WEST);
// Buttons panel
JPanel buttonsPanel = new JPanel();
JButton payButton = new JButton("Record Payment");
JButton waiveButton = new JButton("Waive Fine");
payButton.addActionListener(e -> recordPayment());
waiveButton.addActionListener(e -> waiveFine());
buttonsPanel.add(payButton);
buttonsPanel.add(waiveButton);
panel.add(buttonsPanel, BorderLayout.EAST);
return panel;
}
private void searchFines() {
String studentId = studentIdField.getText().trim();
if (studentId.isEmpty()) {
loadAllFines();
return;
}
tableModel.setRowCount(0);
try {
Connection conn = DatabaseConnection.getConnection();
String query = "SELECT f.fine_id, u.full_name, b.title, bb.due_date, " +
"DATEDIFF(IFNULL(bb.return_date, CURRENT_DATE), bb.due_date) as days_late, " +
"f.amount, f.status " +
"FROM fines f " +
"JOIN book_borrowings bb ON f.borrow_id = bb.borrow_id " +
"JOIN users u ON bb.user_id = u.user_id " +
"JOIN books b ON bb.book_id = b.book_id " +
"WHERE u.user_id = ?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, studentId);
ResultSet rs = pstmt.executeQuery();
double totalFines = 0;
while (rs.next()) {
Object[] row = {
rs.getInt("fine_id"),
rs.getString("full_name"),
rs.getString("title"),
rs.getDate("due_date"),
rs.getInt("days_late"),
String.format("$%.2f", rs.getDouble("amount")),
rs.getString("status")
};
tableModel.addRow(row);
if ("PENDING".equals(rs.getString("status"))) {
totalFines += rs.getDouble("amount");
}
}
updateTotalFines(totalFines);
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error searching fines: " + e.getMessage());
}
}
private void loadAllFines() {
tableModel.setRowCount(0);
try {
Connection conn = DatabaseConnection.getConnection();
String query = "SELECT f.fine_id, u.full_name, b.title, bb.due_date, " +
"DATEDIFF(IFNULL(bb.return_date, CURRENT_DATE), bb.due_date) as days_late, " +
"f.amount, f.status " +
"FROM fines f " +
"JOIN book_borrowings bb ON f.borrow_id = bb.borrow_id " +
"JOIN users u ON bb.user_id = u.user_id " +
"JOIN books b ON bb.book_id = b.book_id " +
"ORDER BY f.created_at DESC";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
double totalFines = 0;
while (rs.next()) {
Object[] row = {
rs.getInt("fine_id"),
rs.getString("full_name"),
rs.getString("title"),
rs.getDate("due_date"),
rs.getInt("days_late"),
String.format("$%.2f", rs.getDouble("amount")),
rs.getString("status")
};
tableModel.addRow(row);
if ("PENDING".equals(rs.getString("status"))) {
totalFines += rs.getDouble("amount");
}
}
updateTotalFines(totalFines);
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error loading fines: " + e.getMessage());
}
}
private void recordPayment() {
int selectedRow = fineTable.getSelectedRow();
if (selectedRow < 0) {
JOptionPane.showMessageDialog(this, "Please select a fine to record payment");
return;
}
if (!"PENDING".equals(tableModel.getValueAt(selectedRow, 6))) {
JOptionPane.showMessageDialog(this, "Can only process payment for pending fines");
return;
}
try {
Connection conn = DatabaseConnection.getConnection();
String query = "UPDATE fines SET status = 'PAID', paid_at = CURRENT_TIMESTAMP " +
"WHERE fine_id = ?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setInt(1, (Integer) tableModel.getValueAt(selectedRow, 0));
pstmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Payment recorded successfully!");
searchFines();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error recording payment: " + e.getMessage());
}
}
private void waiveFine() {
int selectedRow = fineTable.getSelectedRow();
if (selectedRow < 0) {
JOptionPane.showMessageDialog(this, "Please select a fine to waive");
return;
}
if (!"PENDING".equals(tableModel.getValueAt(selectedRow, 6))) {
JOptionPane.showMessageDialog(this, "Can only waive pending fines");
return;
}
int confirm = JOptionPane.showConfirmDialog(this,
"Are you sure you want to waive this fine?",
"Confirm Waive",
JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
try {
Connection conn = DatabaseConnection.getConnection();
String query = "UPDATE fines SET status = 'WAIVED', paid_at = CURRENT_TIMESTAMP " +
"WHERE fine_id = ?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setInt(1, (Integer) tableModel.getValueAt(selectedRow, 0));
pstmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Fine waived successfully!");
searchFines();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error waiving fine: " + e.getMessage());
}
}
}
private void updateTotalFines(double total) {
totalFinesLabel.setText(String.format("Total Outstanding Fines: $%.2f", total));
}
}