-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBorrowBooksPanel.java
More file actions
176 lines (155 loc) · 6.88 KB
/
BorrowBooksPanel.java
File metadata and controls
176 lines (155 loc) · 6.88 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
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
import java.time.LocalDate;
public class BorrowBooksPanel extends JPanel {
private int userId;
private JTable bookTable;
private DefaultTableModel tableModel;
private JTextField searchField;
public BorrowBooksPanel(int userId) {
this.userId = userId;
setLayout(new BorderLayout());
// Create search panel
JPanel searchPanel = createSearchPanel();
add(searchPanel, BorderLayout.NORTH);
// Create table
createBookTable();
JScrollPane scrollPane = new JScrollPane(bookTable);
add(scrollPane, BorderLayout.CENTER);
// Create borrow button panel
JPanel buttonPanel = new JPanel();
JButton borrowButton = new JButton("Borrow Selected Book");
borrowButton.addActionListener(e -> borrowBook());
buttonPanel.add(borrowButton);
add(buttonPanel, BorderLayout.SOUTH);
// Load available books
loadAvailableBooks();
}
private JPanel createSearchPanel() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel.add(new JLabel("Search:"));
searchField = new JTextField(30);
searchField.addActionListener(e -> searchBooks());
panel.add(searchField);
JButton searchButton = new JButton("Search");
searchButton.addActionListener(e -> searchBooks());
panel.add(searchButton);
return panel;
}
private void createBookTable() {
String[] columns = {"ID", "ISBN", "Title", "Author", "Available Quantity"};
tableModel = new DefaultTableModel(columns, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
bookTable = new JTable(tableModel);
}
private void loadAvailableBooks() {
tableModel.setRowCount(0);
try {
Connection conn = DatabaseConnection.getConnection();
String query = "SELECT * FROM books WHERE available_quantity > 0 AND is_active = true";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
Object[] row = {
rs.getInt("book_id"),
rs.getString("isbn"),
rs.getString("title"),
rs.getString("author"),
rs.getInt("available_quantity")
};
tableModel.addRow(row);
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error loading books: " + e.getMessage());
}
}
private void searchBooks() {
String searchTerm = searchField.getText().trim();
tableModel.setRowCount(0);
try {
Connection conn = DatabaseConnection.getConnection();
String query = "SELECT * FROM books WHERE (title LIKE ? OR author LIKE ? OR isbn LIKE ?) " +
"AND available_quantity > 0 AND is_active = true";
PreparedStatement pstmt = conn.prepareStatement(query);
String searchPattern = "%" + searchTerm + "%";
pstmt.setString(1, searchPattern);
pstmt.setString(2, searchPattern);
pstmt.setString(3, searchPattern);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Object[] row = {
rs.getInt("book_id"),
rs.getString("isbn"),
rs.getString("title"),
rs.getString("author"),
rs.getInt("available_quantity")
};
tableModel.addRow(row);
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error searching books: " + e.getMessage());
}
}
private void borrowBook() {
int selectedRow = bookTable.getSelectedRow();
if (selectedRow < 0) {
JOptionPane.showMessageDialog(this, "Please select a book to borrow");
return;
}
try {
Connection conn = DatabaseConnection.getConnection();
// Check if user has any overdue books
String overdueCheck = "SELECT COUNT(*) FROM book_borrowings WHERE user_id = ? " +
"AND status = 'BORROWED' AND due_date < CURRENT_DATE";
PreparedStatement checkStmt = conn.prepareStatement(overdueCheck);
checkStmt.setInt(1, userId);
ResultSet checkRs = checkStmt.executeQuery();
checkRs.next();
if (checkRs.getInt(1) > 0) {
JOptionPane.showMessageDialog(this, "You have overdue books. Please return them first.");
return;
}
// Start transaction
conn.setAutoCommit(false);
try {
// Update available quantity
String updateBook = "UPDATE books SET available_quantity = available_quantity - 1 " +
"WHERE book_id = ? AND available_quantity > 0";
PreparedStatement updateStmt = conn.prepareStatement(updateBook);
int bookId = (Integer) bookTable.getValueAt(selectedRow, 0);
updateStmt.setInt(1, bookId);
int updated = updateStmt.executeUpdate();
if (updated > 0) {
// Create borrowing record
String insertBorrowing = "INSERT INTO book_borrowings (book_id, user_id, borrow_date, due_date, status) " +
"VALUES (?, ?, CURRENT_DATE, DATE_ADD(CURRENT_DATE, INTERVAL 14 DAY), 'BORROWED')";
PreparedStatement borrowStmt = conn.prepareStatement(insertBorrowing);
borrowStmt.setInt(1, bookId);
borrowStmt.setInt(2, userId);
borrowStmt.executeUpdate();
conn.commit();
JOptionPane.showMessageDialog(this, "Book borrowed successfully!");
loadAvailableBooks();
} else {
throw new SQLException("Book not available");
}
} catch (SQLException e) {
conn.rollback();
throw e;
} finally {
conn.setAutoCommit(true);
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error borrowing book: " + e.getMessage());
}
}
}