-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryManagement.java
More file actions
457 lines (380 loc) · 17 KB
/
LibraryManagement.java
File metadata and controls
457 lines (380 loc) · 17 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class LibraryManagement extends JFrame {
// Color scheme
private static final Color PRIMARY_COLOR = new Color(41, 128, 185);
private static final Color SECONDARY_COLOR = new Color(52, 73, 94);
private static final Color SUCCESS_COLOR = new Color(46, 204, 113);
private static final Color DANGER_COLOR = new Color(231, 76, 60);
private static final Color BG_COLOR = new Color(236, 240, 241);
private JTextField tfBookId, tfTitle, tfAuthor, tfPublisher, tfYear, tfIsbn, tfCopies, tfSearch;
private JButton btnAdd, btnEdit, btnDelete, btnClear, btnSearch;
private ArrayList<String[]> books = new ArrayList<>();
private JTable table;
private DefaultTableModel tableModel;
private JLabel statusLabel;
private JTabbedPane tabbedPane;
public LibraryManagement() {
setTitle("Library Management System");
setSize(1300, 750);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
getContentPane().setBackground(BG_COLOR);
// Create tabbed pane
tabbedPane = new JTabbedPane();
tabbedPane.setFont(new Font("Segoe UI", Font.BOLD, 16));
// Add tabs
tabbedPane.addTab("📚 Manage Books", createManagePanel());
tabbedPane.addTab("📋 View All Books", createViewPanel());
add(tabbedPane, BorderLayout.CENTER);
add(createStatusBar(), BorderLayout.SOUTH);
setVisible(true);
}
private JPanel createManagePanel() {
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBackground(BG_COLOR);
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// Title
JLabel titleLabel = new JLabel("Book Information", SwingConstants.CENTER);
titleLabel.setFont(new Font("Segoe UI", Font.BOLD, 28));
titleLabel.setForeground(SECONDARY_COLOR);
mainPanel.add(titleLabel, BorderLayout.NORTH);
// Form panel
JPanel formPanel = new JPanel(new GridBagLayout());
formPanel.setBackground(Color.WHITE);
formPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(189, 195, 199), 2, true),
BorderFactory.createEmptyBorder(30, 30, 30, 30)
));
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(8, 8, 8, 8);
Font labelFont = new Font("Segoe UI", Font.BOLD, 16);
Font fieldFont = new Font("Segoe UI", Font.PLAIN, 16);
// Create fields
tfBookId = createStyledTextField(fieldFont);
tfTitle = createStyledTextField(fieldFont);
tfAuthor = createStyledTextField(fieldFont);
tfPublisher = createStyledTextField(fieldFont);
tfYear = createStyledTextField(fieldFont);
tfIsbn = createStyledTextField(fieldFont);
tfCopies = createStyledTextField(fieldFont);
// Add components
addFormRow(formPanel, "Book ID:", tfBookId, gbc, 0, labelFont);
addFormRow(formPanel, "Book Title:", tfTitle, gbc, 1, labelFont);
addFormRow(formPanel, "Author:", tfAuthor, gbc, 2, labelFont);
addFormRow(formPanel, "Publisher:", tfPublisher, gbc, 3, labelFont);
addFormRow(formPanel, "Year:", tfYear, gbc, 4, labelFont);
addFormRow(formPanel, "ISBN:", tfIsbn, gbc, 5, labelFont);
addFormRow(formPanel, "Copies:", tfCopies, gbc, 6, labelFont);
mainPanel.add(formPanel, BorderLayout.CENTER);
// Button panel
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
buttonPanel.setBackground(BG_COLOR);
btnAdd = createStyledButton("➕ Add", SUCCESS_COLOR);
btnEdit = createStyledButton("✏️ Edit", PRIMARY_COLOR);
btnDelete = createStyledButton("🗑️ Delete", DANGER_COLOR);
btnClear = createStyledButton("🔄 Clear", SECONDARY_COLOR);
btnAdd.addActionListener(e -> addBook());
btnEdit.addActionListener(e -> editBook());
btnDelete.addActionListener(e -> deleteBook());
btnClear.addActionListener(e -> clearFields());
buttonPanel.add(btnAdd);
buttonPanel.add(btnEdit);
buttonPanel.add(btnDelete);
buttonPanel.add(btnClear);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
return mainPanel;
}
private JPanel createViewPanel() {
JPanel panel = new JPanel(new BorderLayout(10, 10));
panel.setBackground(BG_COLOR);
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// Search panel
JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
searchPanel.setBackground(BG_COLOR);
JLabel searchLabel = new JLabel("🔍 Search:");
searchLabel.setFont(new Font("Segoe UI", Font.BOLD, 16));
tfSearch = createStyledTextField(new Font("Segoe UI", Font.PLAIN, 16));
tfSearch.setPreferredSize(new Dimension(300, 35));
btnSearch = createStyledButton("Search", PRIMARY_COLOR);
btnSearch.addActionListener(e -> searchBooks());
JButton btnShowAll = createStyledButton("Show All", SECONDARY_COLOR);
btnShowAll.addActionListener(e -> refreshTable());
searchPanel.add(searchLabel);
searchPanel.add(tfSearch);
searchPanel.add(btnSearch);
searchPanel.add(btnShowAll);
panel.add(searchPanel, BorderLayout.NORTH);
// Table
String[] columns = {"Book ID", "Title", "Author", "Publisher", "Year", "ISBN", "Copies"};
tableModel = new DefaultTableModel(columns, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
table = new JTable(tableModel);
table.setFont(new Font("Segoe UI", Font.PLAIN, 15));
table.setRowHeight(40);
table.setGridColor(new Color(189, 195, 199));
table.setSelectionBackground(new Color(52, 152, 219));
table.setSelectionForeground(Color.WHITE);
table.setShowGrid(true);
table.setIntercellSpacing(new Dimension(1, 1));
// Center align some columns
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
table.getColumnModel().getColumn(0).setCellRenderer(centerRenderer); // Book ID
table.getColumnModel().getColumn(4).setCellRenderer(centerRenderer); // Year
table.getColumnModel().getColumn(6).setCellRenderer(centerRenderer); // Copies
// Header styling
JTableHeader header = table.getTableHeader();
header.setFont(new Font("Segoe UI", Font.BOLD, 16));
header.setBackground(SECONDARY_COLOR);
header.setForeground(Color.WHITE);
header.setPreferredSize(new Dimension(header.getPreferredSize().width, 45));
header.setReorderingAllowed(false);
// Custom renderer for header to ensure text is visible
DefaultTableCellRenderer headerRenderer = new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
label.setBackground(SECONDARY_COLOR);
label.setForeground(Color.WHITE);
label.setFont(new Font("Segoe UI", Font.BOLD, 16));
label.setHorizontalAlignment(JLabel.CENTER);
label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
label.setOpaque(true);
return label;
}
};
// Apply custom renderer to all columns
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumnModel().getColumn(i).setHeaderRenderer(headerRenderer);
}
// Column widths
table.getColumnModel().getColumn(0).setPreferredWidth(90); // Book ID
table.getColumnModel().getColumn(1).setPreferredWidth(280); // Title
table.getColumnModel().getColumn(2).setPreferredWidth(180); // Author
table.getColumnModel().getColumn(3).setPreferredWidth(180); // Publisher
table.getColumnModel().getColumn(4).setPreferredWidth(70); // Year
table.getColumnModel().getColumn(5).setPreferredWidth(140); // ISBN
table.getColumnModel().getColumn(6).setPreferredWidth(80); // Copies
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBorder(BorderFactory.createLineBorder(new Color(189, 195, 199), 2));
scrollPane.getViewport().setBackground(Color.WHITE);
// Ensure header is visible
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane.setColumnHeaderView(table.getTableHeader());
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
private JPanel createStatusBar() {
JPanel statusBar = new JPanel(new FlowLayout(FlowLayout.LEFT));
statusBar.setBackground(SECONDARY_COLOR);
statusBar.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
statusLabel = new JLabel("📚 Total Books: 0");
statusLabel.setFont(new Font("Segoe UI", Font.BOLD, 14));
statusLabel.setForeground(Color.WHITE);
statusBar.add(statusLabel);
return statusBar;
}
private JTextField createStyledTextField(Font font) {
JTextField field = new JTextField(20);
field.setFont(font);
field.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(189, 195, 199), 1),
BorderFactory.createEmptyBorder(5, 10, 5, 10)
));
return field;
}
private JButton createStyledButton(String text, Color color) {
JButton button = new JButton(text);
button.setFont(new Font("Segoe UI", Font.BOLD, 16));
button.setBackground(color);
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setPreferredSize(new Dimension(140, 45));
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
button.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
button.setBackground(color.darker());
}
public void mouseExited(MouseEvent e) {
button.setBackground(color);
}
});
return button;
}
private void addFormRow(JPanel panel, String label, JTextField field,
GridBagConstraints gbc, int row, Font labelFont) {
gbc.gridx = 0;
gbc.gridy = row;
gbc.weightx = 0.3;
JLabel lbl = new JLabel(label);
lbl.setFont(labelFont);
lbl.setForeground(SECONDARY_COLOR);
panel.add(lbl, gbc);
gbc.gridx = 1;
gbc.weightx = 0.7;
panel.add(field, gbc);
}
private void addBook() {
if (!validateFields()) {
return;
}
// Check for duplicate Book ID
String bookId = tfBookId.getText().trim();
for (String[] book : books) {
if (book[0].equals(bookId)) {
showMessage("Error", "Book ID already exists!", JOptionPane.ERROR_MESSAGE);
return;
}
}
String[] book = {
bookId,
tfTitle.getText().trim(),
tfAuthor.getText().trim(),
tfPublisher.getText().trim(),
tfYear.getText().trim(),
tfIsbn.getText().trim(),
tfCopies.getText().trim()
};
books.add(book);
refreshTable();
clearFields();
showMessage("Success", "Book added successfully!", JOptionPane.INFORMATION_MESSAGE);
}
private void editBook() {
String bookId = tfBookId.getText().trim();
if (bookId.isEmpty()) {
showMessage("Error", "Please enter Book ID to edit!", JOptionPane.ERROR_MESSAGE);
return;
}
for (int i = 0; i < books.size(); i++) {
if (books.get(i)[0].equals(bookId)) {
if (!validateFields()) {
return;
}
String[] updatedBook = {
bookId,
tfTitle.getText().trim(),
tfAuthor.getText().trim(),
tfPublisher.getText().trim(),
tfYear.getText().trim(),
tfIsbn.getText().trim(),
tfCopies.getText().trim()
};
books.set(i, updatedBook);
refreshTable();
clearFields();
showMessage("Success", "Book updated successfully!", JOptionPane.INFORMATION_MESSAGE);
return;
}
}
showMessage("Error", "Book not found! Would you like to add it?", JOptionPane.ERROR_MESSAGE);
}
private void deleteBook() {
String bookId = tfBookId.getText().trim();
if (bookId.isEmpty()) {
showMessage("Error", "Please enter Book ID to delete!", JOptionPane.ERROR_MESSAGE);
return;
}
for (int i = 0; i < books.size(); i++) {
if (books.get(i)[0].equals(bookId)) {
int confirm = JOptionPane.showConfirmDialog(
this,
"Are you sure you want to delete this book?",
"Confirm Delete",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE
);
if (confirm == JOptionPane.YES_OPTION) {
books.remove(i);
refreshTable();
clearFields();
showMessage("Success", "Book deleted successfully!", JOptionPane.INFORMATION_MESSAGE);
}
return;
}
}
showMessage("Error", "Book not found!", JOptionPane.ERROR_MESSAGE);
}
private void searchBooks() {
String searchTerm = tfSearch.getText().trim().toLowerCase();
if (searchTerm.isEmpty()) {
refreshTable();
return;
}
tableModel.setRowCount(0);
for (String[] book : books) {
for (String field : book) {
if (field.toLowerCase().contains(searchTerm)) {
tableModel.addRow(book);
break;
}
}
}
}
private void refreshTable() {
tableModel.setRowCount(0);
for (String[] book : books) {
tableModel.addRow(book);
}
updateStatus();
}
private void updateStatus() {
statusLabel.setText("📚 Total Books: " + books.size());
}
private boolean validateFields() {
if (tfBookId.getText().trim().isEmpty() ||
tfTitle.getText().trim().isEmpty() ||
tfAuthor.getText().trim().isEmpty()) {
showMessage("Validation Error", "Book ID, Title, and Author are required!",
JOptionPane.WARNING_MESSAGE);
return false;
}
// Validate year
String year = tfYear.getText().trim();
if (!year.isEmpty() && !year.matches("\\d{4}")) {
showMessage("Validation Error", "Year must be a 4-digit number!",
JOptionPane.WARNING_MESSAGE);
return false;
}
// Validate copies
String copies = tfCopies.getText().trim();
if (!copies.isEmpty() && !copies.matches("\\d+")) {
showMessage("Validation Error", "Number of copies must be a valid number!",
JOptionPane.WARNING_MESSAGE);
return false;
}
return true;
}
private void clearFields() {
tfBookId.setText("");
tfTitle.setText("");
tfAuthor.setText("");
tfPublisher.setText("");
tfYear.setText("");
tfIsbn.setText("");
tfCopies.setText("");
}
private void showMessage(String title, String message, int messageType) {
JOptionPane.showMessageDialog(this, message, title, messageType);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(() -> new LibraryManagement());
}
}