-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookManagementPanel.java
More file actions
284 lines (250 loc) · 10.9 KB
/
BookManagementPanel.java
File metadata and controls
284 lines (250 loc) · 10.9 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
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
public class BookManagementPanel extends JPanel {
private int userId;
private boolean isDarkMode;
private JTable booksTable;
private DefaultTableModel tableModel;
private JTextField titleField, authorField, categoryField, quantityField, isbnField;
private Color darkBackground = new Color(33, 33, 33);
private Color lightBackground = new Color(242, 242, 242);
public BookManagementPanel(int userId, boolean isDarkMode) {
this.userId = userId;
this.isDarkMode = isDarkMode;
setLayout(new BorderLayout(10, 10));
setBackground(isDarkMode ? darkBackground : lightBackground);
initializeComponents();
loadBooks();
}
private void initializeComponents() {
// Input Panel
JPanel inputPanel = new JPanel(new GridLayout(5, 2, 10, 10));
inputPanel.setBackground(isDarkMode ? darkBackground : lightBackground);
// Create text fields
titleField = new JTextField(20);
authorField = new JTextField(20);
categoryField = new JTextField(20);
quantityField = new JTextField(20);
isbnField = new JTextField(20);
// Add components to input panel
addLabelAndField(inputPanel, "Title:", titleField);
addLabelAndField(inputPanel, "Author:", authorField);
addLabelAndField(inputPanel, "Category:", categoryField);
addLabelAndField(inputPanel, "Quantity:", quantityField);
addLabelAndField(inputPanel, "ISBN:", isbnField);
// Create table
tableModel = new DefaultTableModel(
new String[]{"ID", "Title", "Author", "Category", "Quantity", "Available"}, 0
) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
booksTable = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(booksTable);
// Button Panel
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
buttonPanel.setBackground(isDarkMode ? darkBackground : lightBackground);
JButton addButton = createButton("Add Book");
JButton updateButton = createButton("Update Book");
JButton deleteButton = createButton("Delete Book");
JButton clearButton = createButton("Clear Fields");
addButton.addActionListener(e -> addBook());
updateButton.addActionListener(e -> updateBook());
deleteButton.addActionListener(e -> deleteBook());
clearButton.addActionListener(e -> clearFields());
buttonPanel.add(addButton);
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
buttonPanel.add(clearButton);
// Add components to main panel
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.setBackground(isDarkMode ? darkBackground : lightBackground);
topPanel.add(inputPanel, BorderLayout.CENTER);
topPanel.add(buttonPanel, BorderLayout.SOUTH);
add(topPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
// Add table selection listener
booksTable.getSelectionModel().addListSelectionListener(e -> {
if (!e.getValueIsAdjusting()) {
int row = booksTable.getSelectedRow();
if (row != -1) {
titleField.setText((String) tableModel.getValueAt(row, 1));
authorField.setText((String) tableModel.getValueAt(row, 2));
categoryField.setText((String) tableModel.getValueAt(row, 3));
quantityField.setText(String.valueOf(tableModel.getValueAt(row, 4)));
}
}
});
}
private void addLabelAndField(JPanel panel, String labelText, JTextField field) {
JLabel label = new JLabel(labelText);
label.setForeground(isDarkMode ? Color.WHITE : Color.BLACK);
panel.add(label);
panel.add(field);
}
private JButton createButton(String text) {
JButton button = new JButton(text);
button.setBackground(new Color(70, 130, 180));
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
return button;
}
private void loadBooks() {
tableModel.setRowCount(0);
try {
Connection conn = DatabaseConnection.getConnection();
String sql = "SELECT * FROM books WHERE is_active = 1";
try (PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
Object[] row = {
rs.getInt("book_id"),
rs.getString("title"),
rs.getString("author"),
rs.getString("category"),
rs.getInt("quantity"),
rs.getInt("available_quantity")
};
tableModel.addRow(row);
}
}
} catch (SQLException ex) {
showError("Error loading books: " + ex.getMessage());
}
}
private void addBook() {
if (!validateInputs()) return;
try {
Connection conn = DatabaseConnection.getConnection();
String sql = "INSERT INTO books (title, author, category, quantity, available_quantity, isbn, is_active) VALUES (?, ?, ?, ?, ?, ?, 1)";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int quantity = Integer.parseInt(quantityField.getText().trim());
stmt.setString(1, titleField.getText().trim());
stmt.setString(2, authorField.getText().trim());
stmt.setString(3, categoryField.getText().trim());
stmt.setInt(4, quantity);
stmt.setInt(5, quantity);
stmt.setString(6, isbnField.getText().trim());
stmt.executeUpdate();
showSuccess("Book added successfully");
clearFields();
loadBooks();
}
} catch (SQLException ex) {
if (ex.getMessage().contains("Duplicate entry")) {
showError("A book with this ISBN already exists");
} else {
showError("Error adding book: " + ex.getMessage());
}
} catch (NumberFormatException ex) {
showError("Quantity must be a number");
}
}
private void updateBook() {
int row = booksTable.getSelectedRow();
if (row == -1) {
showError("Please select a book to update");
return;
}
if (!validateInputs()) return;
try {
Connection conn = DatabaseConnection.getConnection();
String sql = "UPDATE books SET title = ?, author = ?, category = ?, quantity = ?, " +
"available_quantity = available_quantity + (? - quantity) WHERE book_id = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int bookId = (Integer) tableModel.getValueAt(row, 0);
int newQuantity = Integer.parseInt(quantityField.getText().trim());
stmt.setString(1, titleField.getText().trim());
stmt.setString(2, authorField.getText().trim());
stmt.setString(3, categoryField.getText().trim());
stmt.setInt(4, newQuantity);
stmt.setInt(5, newQuantity);
stmt.setInt(6, bookId);
int result = stmt.executeUpdate();
if (result > 0) {
showSuccess("Book updated successfully");
clearFields();
loadBooks();
}
}
} catch (SQLException ex) {
showError("Error updating book: " + ex.getMessage());
}
}
private void deleteBook() {
int row = booksTable.getSelectedRow();
if (row == -1) {
showError("Please select a book to delete");
return;
}
int confirm = JOptionPane.showConfirmDialog(this,
"Are you sure you want to delete this book?",
"Confirm Delete",
JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
try {
Connection conn = DatabaseConnection.getConnection();
String sql = "UPDATE books SET is_active = 0 WHERE book_id = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int bookId = (Integer) tableModel.getValueAt(row, 0);
stmt.setInt(1, bookId);
int result = stmt.executeUpdate();
if (result > 0) {
showSuccess("Book deleted successfully");
clearFields();
loadBooks();
}
}
} catch (SQLException ex) {
showError("Error deleting book: " + ex.getMessage());
}
}
}
private boolean validateInputs() {
if (titleField.getText().trim().isEmpty()) {
showError("Title is required");
return false;
}
if (authorField.getText().trim().isEmpty()) {
showError("Author is required");
return false;
}
if (isbnField.getText().trim().isEmpty()) {
showError("ISBN is required");
return false;
}
if (quantityField.getText().trim().isEmpty()) {
showError("Quantity is required");
return false;
}
try {
int quantity = Integer.parseInt(quantityField.getText().trim());
if (quantity <= 0) {
showError("Quantity must be greater than 0");
return false;
}
} catch (NumberFormatException ex) {
showError("Quantity must be a number");
return false;
}
return true;
}
private void clearFields() {
titleField.setText("");
authorField.setText("");
categoryField.setText("");
quantityField.setText("");
isbnField.setText("");
booksTable.clearSelection();
}
private void showError(String message) {
JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE);
}
private void showSuccess(String message) {
JOptionPane.showMessageDialog(this, message, "Success", JOptionPane.INFORMATION_MESSAGE);
}
}