Repository navigation
Expand file tree
/
Copy pathReturnBook.java
More file actions
98 lines (82 loc) · 3.51 KB
/
Copy pathReturnBook.java
File metadata and controls
98 lines (82 loc) · 3.51 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 ReturnBook extends JFrame {
JComboBox<BookItem> bookDropdown;
JTextArea info;
ReturnBook() {
setTitle("Return 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("Return Book"));
header.add(Box.createVerticalStrut(4));
header.add(LibraryUI.createSubtitle("Choose an issued book from the dropdown and return it without typing."));
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("Issued 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 ret = LibraryUI.createPrimaryButton("Return Book");
JButton refresh = LibraryUI.createSecondaryButton("Refresh List");
buttonRow.add(ret);
buttonRow.add(Box.createHorizontalStrut(10));
buttonRow.add(refresh);
card.add(buttonRow);
card.add(Box.createVerticalStrut(16));
info = LibraryUI.createInfoArea(6, 34);
info.setText("Only issued books are listed here. Select one and mark it available again.");
card.add(LibraryUI.wrap(info));
ret.addActionListener(e -> returnBook());
refresh.addActionListener(e -> refreshBooks());
refreshBooks();
setVisible(true);
}
private void returnBook() {
BookItem selectedBook = (BookItem) bookDropdown.getSelectedItem();
if (selectedBook == null) {
JOptionPane.showMessageDialog(this, "No issued book found to return.");
return;
}
Connection con = DBConnection.getConnection();
if (con == null) {
return;
}
try (PreparedStatement ps = con.prepareStatement("UPDATE books SET issued=false WHERE id=?")) {
ps.setInt(1, selectedBook.getId());
int rows = ps.executeUpdate();
if (rows > 0) {
info.setText("Book returned successfully.\n\n"
+ "ID: " + selectedBook.getId()
+ "\nName: " + selectedBook.getName()
+ "\nAuthor: " + selectedBook.getAuthor()
+ "\nStatus: Available");
JOptionPane.showMessageDialog(this, "Book returned successfully.");
refreshBooks();
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Unable to return book.\n" + ex.getMessage());
}
}
private void refreshBooks() {
BookRepository.loadBooksInto(bookDropdown, false, true);
if (bookDropdown.getItemCount() == 0) {
info.setText("No issued books right now. Issue a book first to see it here.");
}
}
}