-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssueBooksPanel.java
More file actions
325 lines (281 loc) · 12.8 KB
/
IssueBooksPanel.java
File metadata and controls
325 lines (281 loc) · 12.8 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
import java.util.Date;
import java.util.Calendar;
public class IssueBooksPanel extends JPanel {
private int userId;
private boolean isDarkMode;
private Color darkBackground = new Color(33, 33, 33);
private Color lightBackground = new Color(242, 242, 242);
private JTable booksTable;
private DefaultTableModel tableModel;
private JTextField studentIdField;
private JTextField searchField;
public IssueBooksPanel(int userId, boolean isDarkMode) {
this.userId = userId;
this.isDarkMode = isDarkMode;
setLayout(new BorderLayout(10, 10));
setBackground(isDarkMode ? darkBackground : lightBackground);
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
createComponents();
loadBooks();
}
private void createComponents() {
// Title Panel
JLabel titleLabel = new JLabel("Issue Books", SwingConstants.CENTER);
titleLabel.setFont(new Font("Segoe UI", Font.BOLD, 24));
titleLabel.setForeground(isDarkMode ? Color.WHITE : Color.BLACK);
add(titleLabel, BorderLayout.NORTH);
// Create top panel for student ID and search
JPanel topPanel = new JPanel(new BorderLayout(10, 10));
topPanel.setBackground(isDarkMode ? darkBackground : lightBackground);
// Student ID panel
JPanel studentPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
studentPanel.setBackground(isDarkMode ? darkBackground : lightBackground);
JLabel studentLabel = new JLabel("Student ID:");
studentLabel.setForeground(isDarkMode ? Color.WHITE : Color.BLACK);
studentIdField = new JTextField(10);
studentPanel.add(studentLabel);
studentPanel.add(studentIdField);
// Search panel
JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
searchPanel.setBackground(isDarkMode ? darkBackground : lightBackground);
JLabel searchLabel = new JLabel("Search Books:");
searchLabel.setForeground(isDarkMode ? Color.WHITE : Color.BLACK);
searchField = new JTextField(20);
JButton searchButton = new JButton("Search");
styleButton(searchButton);
searchButton.addActionListener(e -> searchBooks());
searchPanel.add(searchLabel);
searchPanel.add(searchField);
searchPanel.add(searchButton);
topPanel.add(studentPanel, BorderLayout.WEST);
topPanel.add(searchPanel, BorderLayout.EAST);
add(topPanel, BorderLayout.NORTH);
// Create table
String[] columns = {"Book ID", "Title", "Author", "Category", "Available Quantity"};
tableModel = new DefaultTableModel(columns, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
booksTable = new JTable(tableModel);
booksTable.setFont(new Font("Segoe UI", Font.PLAIN, 12));
booksTable.getTableHeader().setFont(new Font("Segoe UI", Font.BOLD, 12));
booksTable.setRowHeight(25);
booksTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(booksTable);
add(scrollPane, BorderLayout.CENTER);
// Create buttons panel
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
buttonPanel.setBackground(isDarkMode ? darkBackground : lightBackground);
JButton issueButton = new JButton("Issue Book");
JButton clearButton = new JButton("Clear");
JButton refreshButton = new JButton("Refresh");
styleButton(issueButton);
styleButton(clearButton);
styleButton(refreshButton);
issueButton.addActionListener(e -> issueBook());
clearButton.addActionListener(e -> clearFields());
refreshButton.addActionListener(e -> loadBooks());
buttonPanel.add(issueButton);
buttonPanel.add(clearButton);
buttonPanel.add(refreshButton);
add(buttonPanel, BorderLayout.SOUTH);
}
private void styleButton(JButton button) {
button.setBackground(new Color(70, 130, 180));
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
button.setFont(new Font("Segoe UI", Font.PLAIN, 14));
}
private void loadBooks() {
tableModel.setRowCount(0);
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(
"SELECT book_id, title, author, category, available_quantity " +
"FROM books WHERE is_active = 1 AND available_quantity > 0"
);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Object[] row = {
rs.getInt("book_id"),
rs.getString("title"),
rs.getString("author"),
rs.getString("category"),
rs.getInt("available_quantity")
};
tableModel.addRow(row);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Error loading books: " + ex.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void searchBooks() {
String searchText = searchField.getText().trim();
if (searchText.isEmpty()) {
loadBooks();
return;
}
tableModel.setRowCount(0);
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(
"SELECT book_id, title, author, category, available_quantity " +
"FROM books WHERE is_active = 1 AND available_quantity > 0 " +
"AND (title LIKE ? OR author LIKE ? OR category LIKE ?)"
);
String pattern = "%" + searchText + "%";
stmt.setString(1, pattern);
stmt.setString(2, pattern);
stmt.setString(3, pattern);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Object[] row = {
rs.getInt("book_id"),
rs.getString("title"),
rs.getString("author"),
rs.getString("category"),
rs.getInt("available_quantity")
};
tableModel.addRow(row);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Error searching books: " + ex.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void issueBook() {
int selectedRow = booksTable.getSelectedRow();
String studentIdText = studentIdField.getText().trim();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this,
"Please select a book to issue",
"No Selection",
JOptionPane.WARNING_MESSAGE);
return;
}
if (studentIdText.isEmpty()) {
JOptionPane.showMessageDialog(this,
"Please enter a student ID",
"Missing Information",
JOptionPane.WARNING_MESSAGE);
return;
}
try {
int studentId = Integer.parseInt(studentIdText);
int bookId = (int) tableModel.getValueAt(selectedRow, 0);
Connection conn = DatabaseConnection.getConnection();
conn.setAutoCommit(false);
try {
// Verify student exists and is active
PreparedStatement checkStudent = conn.prepareStatement(
"SELECT is_active FROM users WHERE user_id = ? AND role = 'STUDENT'"
);
checkStudent.setInt(1, studentId);
ResultSet studentRs = checkStudent.executeQuery();
if (!studentRs.next()) {
throw new Exception("Student ID not found");
}
if (!studentRs.getBoolean("is_active")) {
throw new Exception("Student account is not active");
}
// Check if student has any overdue books
PreparedStatement checkOverdue = conn.prepareStatement(
"SELECT COUNT(*) FROM book_borrowings " +
"WHERE user_id = ? AND status = 'BORROWED' AND due_date < CURRENT_DATE"
);
checkOverdue.setInt(1, studentId);
ResultSet overdueRs = checkOverdue.executeQuery();
overdueRs.next();
if (overdueRs.getInt(1) > 0) {
throw new Exception("Student has overdue books");
}
// Check if student already has this book
PreparedStatement checkBorrowed = conn.prepareStatement(
"SELECT COUNT(*) FROM book_borrowings " +
"WHERE user_id = ? AND book_id = ? AND status = 'BORROWED'"
);
checkBorrowed.setInt(1, studentId);
checkBorrowed.setInt(2, bookId);
ResultSet borrowedRs = checkBorrowed.executeQuery();
borrowedRs.next();
if (borrowedRs.getInt(1) > 0) {
throw new Exception("Student already has this book");
}
// Update book quantity
PreparedStatement updateBook = conn.prepareStatement(
"UPDATE books SET available_quantity = available_quantity - 1 " +
"WHERE book_id = ? AND available_quantity > 0"
);
updateBook.setInt(1, bookId);
int updated = updateBook.executeUpdate();
if (updated == 0) {
throw new Exception("Book not available");
}
// Create borrowing record
Calendar cal = Calendar.getInstance();
Date borrowDate = new Date();
cal.setTime(borrowDate);
cal.add(Calendar.DAY_OF_MONTH, 14); // 14 days borrowing period
Date dueDate = cal.getTime();
PreparedStatement insertBorrowing = conn.prepareStatement(
"INSERT INTO book_borrowings (book_id, user_id, borrow_date, due_date, status) " +
"VALUES (?, ?, ?, ?, 'BORROWED')"
);
insertBorrowing.setInt(1, bookId);
insertBorrowing.setInt(2, studentId);
insertBorrowing.setDate(3, new java.sql.Date(borrowDate.getTime()));
insertBorrowing.setDate(4, new java.sql.Date(dueDate.getTime()));
insertBorrowing.executeUpdate();
// Create notification
PreparedStatement insertNotification = conn.prepareStatement(
"INSERT INTO notifications (user_id, message, is_read) " +
"VALUES (?, ?, false)"
);
insertNotification.setInt(1, studentId);
insertNotification.setString(2, "Book '" + tableModel.getValueAt(selectedRow, 1) +
"' has been issued to you. Due date: " + new java.sql.Date(dueDate.getTime()));
insertNotification.executeUpdate();
conn.commit();
JOptionPane.showMessageDialog(this,
"Book issued successfully",
"Success",
JOptionPane.INFORMATION_MESSAGE);
loadBooks(); // Refresh the table
clearFields();
} catch (Exception ex) {
conn.rollback();
throw ex;
} finally {
conn.setAutoCommit(true);
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this,
"Invalid student ID format",
"Error",
JOptionPane.ERROR_MESSAGE);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this,
"Error issuing book: " + ex.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void clearFields() {
studentIdField.setText("");
searchField.setText("");
booksTable.clearSelection();
}
}