-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReissueBooksPanel.java
More file actions
138 lines (122 loc) · 5.35 KB
/
ReissueBooksPanel.java
File metadata and controls
138 lines (122 loc) · 5.35 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
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.sql.*;
public class ReissueBooksPanel extends JPanel {
private JTable borrowedBooksTable;
private DefaultTableModel tableModel;
private int userId;
public ReissueBooksPanel(int userId) {
this.userId = userId;
setLayout(new BorderLayout(10, 10));
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
initializeComponents();
loadBorrowedBooks();
}
private void initializeComponents() {
// Create table
String[] columns = {"Borrowing ID", "Book Title", "Borrow Date", "Due Date", "Status"};
tableModel = new DefaultTableModel(columns, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
borrowedBooksTable = new JTable(tableModel);
borrowedBooksTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane(borrowedBooksTable);
// Create buttons panel
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton reissueButton = createStyledButton("Reissue Book");
JButton refreshButton = createStyledButton("Refresh");
buttonPanel.add(reissueButton);
buttonPanel.add(refreshButton);
// Add components
add(scrollPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
// Add listeners
reissueButton.addActionListener(e -> reissueBook());
refreshButton.addActionListener(e -> loadBorrowedBooks());
}
private JButton createStyledButton(String text) {
JButton button = new JButton(text);
button.setFont(new Font("Segoe UI", Font.PLAIN, 12));
button.setPreferredSize(new Dimension(120, 30));
button.setBackground(new Color(70, 130, 180));
button.setForeground(Color.WHITE);
button.setFocusPainted(false);
button.setBorderPainted(false);
return button;
}
private void loadBorrowedBooks() {
tableModel.setRowCount(0);
try {
Connection conn = DatabaseConnection.getConnection();
PreparedStatement stmt = conn.prepareStatement(
"SELECT bb.borrowing_id, b.title, bb.borrow_date, bb.due_date, bb.status " +
"FROM book_borrowings bb " +
"JOIN books b ON bb.book_id = b.book_id " +
"WHERE bb.user_id = ? AND bb.status = 'BORROWED' " +
"ORDER BY bb.due_date"
);
stmt.setInt(1, userId);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Object[] row = {
rs.getInt("borrowing_id"),
rs.getString("title"),
rs.getDate("borrow_date"),
rs.getDate("due_date"),
rs.getString("status")
};
tableModel.addRow(row);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error loading borrowed books: " + ex.getMessage());
}
}
private void reissueBook() {
int selectedRow = borrowedBooksTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "Please select a book to reissue");
return;
}
int borrowingId = (int) tableModel.getValueAt(selectedRow, 0);
String bookTitle = (String) tableModel.getValueAt(selectedRow, 1);
try {
Connection conn = DatabaseConnection.getConnection();
// Check if book has already been reissued
PreparedStatement checkStmt = conn.prepareStatement(
"SELECT COUNT(*) FROM book_borrowings " +
"WHERE borrowing_id = ? AND due_date > DATE_ADD(borrow_date, INTERVAL 14 DAY)"
);
checkStmt.setInt(1, borrowingId);
ResultSet rs = checkStmt.executeQuery();
rs.next();
if (rs.getInt(1) > 0) {
JOptionPane.showMessageDialog(this,
"This book has already been reissued once. Cannot reissue again.");
return;
}
// Extend due date by 7 days
PreparedStatement updateStmt = conn.prepareStatement(
"UPDATE book_borrowings SET due_date = DATE_ADD(due_date, INTERVAL 7 DAY) " +
"WHERE borrowing_id = ? AND status = 'BORROWED'"
);
updateStmt.setInt(1, borrowingId);
int result = updateStmt.executeUpdate();
if (result > 0) {
JOptionPane.showMessageDialog(this,
"Book '" + bookTitle + "' has been reissued successfully.\n" +
"Due date extended by 7 days.");
loadBorrowedBooks();
} else {
JOptionPane.showMessageDialog(this, "Failed to reissue book. Please try again.");
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error reissuing book: " + ex.getMessage());
}
}
}