-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssueBook.java
More file actions
98 lines (82 loc) · 3.52 KB
/
Copy pathIssueBook.java
File metadata and controls
98 lines (82 loc) · 3.52 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
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class IssueBook extends JFrame {
JComboBox<BookItem> bookDropdown;
JTextArea info;
IssueBook() {
setTitle("Issue Book");
setSize(560, 360);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel root = LibraryUI.createContentPanel();
root.setLayout(new BorderLayout(0, 16));
setContentPane(root);
JPanel header = new JPanel();
header.setOpaque(false);
header.setLayout(new BoxLayout(header, BoxLayout.Y_AXIS));
header.add(LibraryUI.createTitle("Issue Book"));
header.add(Box.createVerticalStrut(4));
header.add(LibraryUI.createSubtitle("Choose an available book from the dropdown so typing mistakes are gone."));
root.add(header, BorderLayout.NORTH);
JPanel card = LibraryUI.createCardPanel();
card.setLayout(new BoxLayout(card, BoxLayout.Y_AXIS));
root.add(card, BorderLayout.CENTER);
JLabel label = new JLabel("Available Books");
label.setFont(LibraryUI.LABEL_FONT);
label.setForeground(LibraryUI.TEXT);
card.add(label);
card.add(Box.createVerticalStrut(8));
bookDropdown = LibraryUI.createBookDropdown();
card.add(bookDropdown);
card.add(Box.createVerticalStrut(16));
JPanel buttonRow = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
buttonRow.setOpaque(false);
JButton issue = LibraryUI.createPrimaryButton("Issue Book");
JButton refresh = LibraryUI.createSecondaryButton("Refresh List");
buttonRow.add(issue);
buttonRow.add(Box.createHorizontalStrut(10));
buttonRow.add(refresh);
card.add(buttonRow);
card.add(Box.createVerticalStrut(16));
info = LibraryUI.createInfoArea(6, 34);
info.setText("Only available books are listed here. Pick one and issue it in a single click.");
card.add(LibraryUI.wrap(info));
issue.addActionListener(e -> issueBook());
refresh.addActionListener(e -> refreshBooks());
refreshBooks();
setVisible(true);
}
private void issueBook() {
BookItem selectedBook = (BookItem) bookDropdown.getSelectedItem();
if (selectedBook == null) {
JOptionPane.showMessageDialog(this, "No available book found to issue.");
return;
}
Connection con = DBConnection.getConnection();
if (con == null) {
return;
}
try (PreparedStatement ps = con.prepareStatement("UPDATE books SET issued=true WHERE id=?")) {
ps.setInt(1, selectedBook.getId());
int rows = ps.executeUpdate();
if (rows > 0) {
info.setText("Book issued successfully.\n\n"
+ "ID: " + selectedBook.getId()
+ "\nName: " + selectedBook.getName()
+ "\nAuthor: " + selectedBook.getAuthor()
+ "\nStatus: Issued");
JOptionPane.showMessageDialog(this, "Book issued successfully.");
refreshBooks();
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Unable to issue book.\n" + ex.getMessage());
}
}
private void refreshBooks() {
BookRepository.loadBooksInto(bookDropdown, true, false);
if (bookDropdown.getItemCount() == 0) {
info.setText("No available books right now. Add a new book or return an issued book.");
}
}
}