-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssuedBooksPanel.java
More file actions
212 lines (183 loc) · 8.65 KB
/
IssuedBooksPanel.java
File metadata and controls
212 lines (183 loc) · 8.65 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
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class IssuedBooksPanel extends JPanel {
private int userId;
private boolean isDarkMode;
private Color darkBackground = new Color(33, 33, 33);
private Color lightBackground = new Color(242, 242, 242);
public IssuedBooksPanel(int userId, boolean isDarkMode) {
this.userId = userId;
this.isDarkMode = isDarkMode;
setLayout(new BorderLayout(10, 10));
setBackground(isDarkMode ? darkBackground : lightBackground);
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Add title
JLabel titleLabel = new JLabel("Issued Books", SwingConstants.CENTER);
titleLabel.setFont(new Font("Segoe UI", Font.BOLD, 24));
titleLabel.setForeground(isDarkMode ? Color.WHITE : Color.BLACK);
add(titleLabel, BorderLayout.NORTH);
// Create table
String[] columns = {"Book ID", "Title", "Student ID", "Student Name", "Issue Date", "Due Date", "Status"};
DefaultTableModel model = new DefaultTableModel(columns, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JTable issuedBooksTable = new JTable(model);
issuedBooksTable.setFont(new Font("Segoe UI", Font.PLAIN, 12));
issuedBooksTable.getTableHeader().setFont(new Font("Segoe UI", Font.BOLD, 12));
issuedBooksTable.setRowHeight(25);
issuedBooksTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Add search panel
JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
searchPanel.setBackground(isDarkMode ? darkBackground : lightBackground);
JTextField searchField = new JTextField(20);
searchField.setPreferredSize(new Dimension(200, 30));
JButton searchButton = new JButton("Search");
searchButton.setBackground(new Color(70, 130, 180));
searchButton.setForeground(Color.WHITE);
searchPanel.add(new JLabel("Search: "));
searchPanel.add(searchField);
searchPanel.add(searchButton);
// Add filter options
String[] filterOptions = {"All", "Currently Issued", "Overdue", "Returned"};
JComboBox<String> filterCombo = new JComboBox<>(filterOptions);
searchPanel.add(Box.createHorizontalStrut(20));
searchPanel.add(new JLabel("Filter: "));
searchPanel.add(filterCombo);
add(searchPanel, BorderLayout.NORTH);
// Load issued books
loadIssuedBooks(model, "All");
// Add table to scroll pane
JScrollPane scrollPane = new JScrollPane(issuedBooksTable);
add(scrollPane, BorderLayout.CENTER);
// Add action listeners
searchButton.addActionListener(e -> {
String searchText = searchField.getText().trim();
String filterStatus = (String) filterCombo.getSelectedItem();
searchIssuedBooks(model, searchText, filterStatus);
});
filterCombo.addActionListener(e -> {
String filterStatus = (String) filterCombo.getSelectedItem();
loadIssuedBooks(model, filterStatus);
});
// Add statistics panel
JPanel statsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
statsPanel.setBackground(isDarkMode ? darkBackground : lightBackground);
updateStatistics(statsPanel);
add(statsPanel, BorderLayout.SOUTH);
}
private void loadIssuedBooks(DefaultTableModel model, String filter) {
model.setRowCount(0);
try {
Connection conn = DatabaseConnection.getConnection();
String sql = "SELECT b.book_id, b.title, bb.user_id, u.full_name, " +
"bb.borrow_date, bb.due_date, bb.status " +
"FROM book_borrowings bb " +
"JOIN books b ON bb.book_id = b.book_id " +
"JOIN users u ON bb.user_id = u.user_id ";
if (!filter.equals("All")) {
sql += "WHERE bb.status = ? ";
}
sql += "ORDER BY bb.borrow_date DESC";
PreparedStatement stmt = conn.prepareStatement(sql);
if (!filter.equals("All")) {
stmt.setString(1, filter.toUpperCase());
}
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Object[] row = {
rs.getInt("book_id"),
rs.getString("title"),
rs.getInt("user_id"),
rs.getString("full_name"),
rs.getDate("borrow_date"),
rs.getDate("due_date"),
rs.getString("status")
};
model.addRow(row);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Error loading issued books: " + ex.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void searchIssuedBooks(DefaultTableModel model, String searchText, String filter) {
model.setRowCount(0);
try {
Connection conn = DatabaseConnection.getConnection();
String sql = "SELECT b.book_id, b.title, bb.user_id, u.full_name, " +
"bb.borrow_date, bb.due_date, bb.status " +
"FROM book_borrowings bb " +
"JOIN books b ON bb.book_id = b.book_id " +
"JOIN users u ON bb.user_id = u.user_id " +
"WHERE (b.title LIKE ? OR u.full_name LIKE ?) ";
if (!filter.equals("All")) {
sql += "AND bb.status = ? ";
}
sql += "ORDER BY bb.borrow_date DESC";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "%" + searchText + "%");
stmt.setString(2, "%" + searchText + "%");
if (!filter.equals("All")) {
stmt.setString(3, filter.toUpperCase());
}
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Object[] row = {
rs.getInt("book_id"),
rs.getString("title"),
rs.getInt("user_id"),
rs.getString("full_name"),
rs.getDate("borrow_date"),
rs.getDate("due_date"),
rs.getString("status")
};
model.addRow(row);
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Error searching issued books: " + ex.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void updateStatistics(JPanel statsPanel) {
try {
Connection conn = DatabaseConnection.getConnection();
// Get total issued books
PreparedStatement stmt = conn.prepareStatement(
"SELECT COUNT(*) as total, " +
"SUM(CASE WHEN status = 'BORROWED' THEN 1 ELSE 0 END) as current, " +
"SUM(CASE WHEN status = 'BORROWED' AND due_date < CURRENT_DATE THEN 1 ELSE 0 END) as overdue " +
"FROM book_borrowings"
);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
JLabel totalLabel = new JLabel("Total Issues: " + rs.getInt("total"));
JLabel currentLabel = new JLabel("Currently Issued: " + rs.getInt("current"));
JLabel overdueLabel = new JLabel("Overdue: " + rs.getInt("overdue"));
totalLabel.setForeground(isDarkMode ? Color.WHITE : Color.BLACK);
currentLabel.setForeground(isDarkMode ? Color.WHITE : Color.BLACK);
overdueLabel.setForeground(Color.RED);
statsPanel.add(totalLabel);
statsPanel.add(Box.createHorizontalStrut(20));
statsPanel.add(currentLabel);
statsPanel.add(Box.createHorizontalStrut(20));
statsPanel.add(overdueLabel);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}