-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssueReturnPanel.java
More file actions
525 lines (448 loc) · 18 KB
/
Copy pathIssueReturnPanel.java
File metadata and controls
525 lines (448 loc) · 18 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package ui;
import dao.BookDAO;
import dao.IssuedBookDAO;
import models.Book;
import models.IssuedBook;
import javax.swing.*;
import java.awt.*;
import java.time.LocalDate;
import java.util.logging.Logger;
/**
* IssueReturnPanel.java - Panel for issuing and returning books
*
* Features:
* - Issue book to a member
* - Return book from a member
* - Automatic inventory updates
* - Due date calculation
* - Member information tracking
*
* Business Logic:
* - Decrements available_copies when book is issued
* - Increments available_copies when book is returned
* - Validates availability before issuing
*/
public class IssueReturnPanel extends JPanel {
private static final Logger logger = Logger.getLogger(IssueReturnPanel.class.getName());
private static final long serialVersionUID = 1L;
private static final int DEFAULT_ISSUE_DAYS = 14; // 2 weeks
// UI Components
private JTabbedPane operationTabs;
// Issue components
private JTextField bookIdField, memberNameField, memberContactField;
private JLabel bookDetailsLabel, daysLabel;
private JSpinner daysSpinner;
private JButton issueButton, issueClearButton;
private JLabel issueMessageLabel;
// Return components
private JTextField returnBookIdField, returnMemberField;
private JButton returnButton, returnClearButton;
private JLabel returnMessageLabel;
private JTextArea returnDetailsArea;
// Data Access
private BookDAO bookDAO;
private IssuedBookDAO issuedBookDAO;
/**
* Constructor
*/
public IssueReturnPanel(BookDAO bookDAO, IssuedBookDAO issuedBookDAO) {
this.bookDAO = bookDAO;
this.issuedBookDAO = issuedBookDAO;
setLayout(new BorderLayout(10, 10));
setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
// Create tabbed interface for Issue/Return
operationTabs = new JTabbedPane();
operationTabs.addTab("📤 Issue Book", createIssuePanel());
operationTabs.addTab("📥 Return Book", createReturnPanel());
operationTabs.addTab("⏰ Overdue Books", createOverduePanel());
add(operationTabs, BorderLayout.CENTER);
}
/**
* Creates the issue book panel
*/
private JPanel createIssuePanel() {
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Issue form
JPanel formPanel = new JPanel(new GridBagLayout());
formPanel.setBorder(BorderFactory.createTitledBorder("Issue Book"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
int row = 0;
// Book ID
gbc.gridx = 0;
gbc.gridy = row;
formPanel.add(new JLabel("Book ID *"), gbc);
gbc.gridx = 1;
bookIdField = new JTextField(15);
formPanel.add(bookIdField, gbc);
gbc.gridx = 2;
JButton checkButton = new JButton("Check Availability");
checkButton.addActionListener(e -> checkBookAvailability());
formPanel.add(checkButton, gbc);
row++;
// Book Details
gbc.gridx = 0;
gbc.gridy = row;
gbc.gridwidth = 3;
bookDetailsLabel = new JLabel("Book details will appear here");
bookDetailsLabel.setForeground(Color.BLUE);
formPanel.add(bookDetailsLabel, gbc);
gbc.gridwidth = 1;
row++;
// Member Name
gbc.gridx = 0;
gbc.gridy = row;
formPanel.add(new JLabel("Member Name *"), gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
memberNameField = new JTextField(30);
formPanel.add(memberNameField, gbc);
gbc.gridwidth = 1;
row++;
// Member Contact
gbc.gridx = 0;
gbc.gridy = row;
formPanel.add(new JLabel("Contact (Optional)"), gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
memberContactField = new JTextField(30);
formPanel.add(memberContactField, gbc);
gbc.gridwidth = 1;
row++;
// Issue Days
gbc.gridx = 0;
gbc.gridy = row;
formPanel.add(new JLabel("Issue Period (days) *"), gbc);
gbc.gridx = 1;
SpinnerModel spinnerModel = new SpinnerNumberModel(DEFAULT_ISSUE_DAYS, 1, 90, 1);
daysSpinner = new JSpinner(spinnerModel);
formPanel.add(daysSpinner, gbc);
gbc.gridx = 2;
daysLabel = new JLabel("(Default: 14 days)");
formPanel.add(daysLabel, gbc);
row++;
// Message Label
gbc.gridx = 0;
gbc.gridy = row;
gbc.gridwidth = 3;
issueMessageLabel = new JLabel(" ");
issueMessageLabel.setForeground(Color.BLUE);
formPanel.add(issueMessageLabel, gbc);
row++;
// Buttons
gbc.gridx = 0;
gbc.gridy = row;
gbc.gridwidth = 3;
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
issueButton = new JButton("✓ Issue Book");
issueButton.setFont(new Font("Arial", Font.BOLD, 12));
issueButton.addActionListener(e -> handleIssueBook());
issueClearButton = new JButton("🔄 Clear");
issueClearButton.addActionListener(e -> clearIssueForm());
buttonPanel.add(issueButton);
buttonPanel.add(issueClearButton);
formPanel.add(buttonPanel, gbc);
mainPanel.add(formPanel, BorderLayout.NORTH);
return mainPanel;
}
/**
* Creates the return book panel
*/
private JPanel createReturnPanel() {
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Return form
JPanel formPanel = new JPanel(new GridBagLayout());
formPanel.setBorder(BorderFactory.createTitledBorder("Return Book"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
int row = 0;
// Book ID or Issue ID
gbc.gridx = 0;
gbc.gridy = row;
formPanel.add(new JLabel("Book ID *"), gbc);
gbc.gridx = 1;
returnBookIdField = new JTextField(15);
formPanel.add(returnBookIdField, gbc);
gbc.gridx = 2;
JButton searchButton = new JButton("Search");
searchButton.addActionListener(e -> searchIssuedBook());
formPanel.add(searchButton, gbc);
row++;
// Details
gbc.gridx = 0;
gbc.gridy = row;
gbc.gridwidth = 3;
returnDetailsArea = new JTextArea(5, 40);
returnDetailsArea.setEditable(false);
returnDetailsArea.setLineWrap(true);
returnDetailsArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(returnDetailsArea);
formPanel.add(scrollPane, gbc);
gbc.gridwidth = 1;
row++;
// Member Name
gbc.gridx = 0;
gbc.gridy = row;
formPanel.add(new JLabel("Member Name *"), gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
returnMemberField = new JTextField(30);
formPanel.add(returnMemberField, gbc);
gbc.gridwidth = 1;
row++;
// Message Label
gbc.gridx = 0;
gbc.gridy = row;
gbc.gridwidth = 3;
returnMessageLabel = new JLabel(" ");
returnMessageLabel.setForeground(Color.BLUE);
formPanel.add(returnMessageLabel, gbc);
row++;
// Buttons
gbc.gridx = 0;
gbc.gridy = row;
gbc.gridwidth = 3;
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
returnButton = new JButton("✓ Return Book");
returnButton.setFont(new Font("Arial", Font.BOLD, 12));
returnButton.addActionListener(e -> handleReturnBook());
returnClearButton = new JButton("🔄 Clear");
returnClearButton.addActionListener(e -> clearReturnForm());
buttonPanel.add(returnButton);
buttonPanel.add(returnClearButton);
formPanel.add(buttonPanel, gbc);
mainPanel.add(formPanel, BorderLayout.NORTH);
return mainPanel;
}
/**
* Creates the overdue books panel
*/
private JPanel createOverduePanel() {
JPanel panel = new JPanel(new BorderLayout(10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JButton refreshButton = new JButton("🔄 Refresh Overdue List");
refreshButton.addActionListener(e -> displayOverdueBooks());
panel.add(refreshButton, BorderLayout.NORTH);
JTextArea overdueArea = new JTextArea();
overdueArea.setEditable(false);
overdueArea.setLineWrap(true);
overdueArea.setWrapStyleWord(true);
overdueArea.setFont(new Font("Courier", Font.PLAIN, 11));
JScrollPane scrollPane = new JScrollPane(overdueArea);
panel.add(scrollPane, BorderLayout.CENTER);
// Display overdue books on panel creation
displayOverdueBooks();
return panel;
}
/**
* Checks book availability
*/
private void checkBookAvailability() {
String bookIdStr = bookIdField.getText().trim();
if (bookIdStr.isEmpty()) {
issueMessageLabel.setForeground(Color.RED);
issueMessageLabel.setText("Please enter Book ID");
return;
}
try {
int bookId = Integer.parseInt(bookIdStr);
Book book = bookDAO.getBookById(bookId);
if (book == null) {
issueMessageLabel.setForeground(Color.RED);
issueMessageLabel.setText("✗ Book not found");
} else if (!book.isAvailable()) {
issueMessageLabel.setForeground(Color.RED);
issueMessageLabel.setText("✗ Book not available. No copies in stock.");
} else {
String details = String.format(
"Title: %s | Author: %s | Available: %d/%d",
book.getTitle(), book.getAuthor(),
book.getAvailableCopies(), book.getTotalCopies()
);
bookDetailsLabel.setText(details);
issueMessageLabel.setForeground(new Color(0, 150, 0));
issueMessageLabel.setText("✓ Book available for issue");
}
} catch (NumberFormatException e) {
issueMessageLabel.setForeground(Color.RED);
issueMessageLabel.setText("Book ID must be a number");
}
}
/**
* Handles issue book
*/
private void handleIssueBook() {
try {
String bookIdStr = bookIdField.getText().trim();
String memberName = memberNameField.getText().trim();
String memberContact = memberContactField.getText().trim();
int issueDays = (Integer) daysSpinner.getValue();
// Validation
if (bookIdStr.isEmpty() || memberName.isEmpty()) {
showIssueError("Please fill required fields");
return;
}
int bookId = Integer.parseInt(bookIdStr);
Book book = bookDAO.getBookById(bookId);
if (book == null) {
showIssueError("Book not found");
return;
}
if (!book.isAvailable()) {
showIssueError("No copies available for issue");
return;
}
// Create issue record
IssuedBook issuedBook = new IssuedBook(bookId, book.getTitle(),
LocalDate.now(), issueDays,
memberName, memberContact);
// Record in database
int issueId = issuedBookDAO.issueBook(issuedBook);
if (issueId > 0) {
// Update book's available copies
book.issueBook();
bookDAO.updateAvailableCopies(bookId, book.getAvailableCopies());
LocalDate dueDate = LocalDate.now().plusDays(issueDays);
String message = String.format(
"✓ Book issued successfully!\nIssue ID: %d\nDue Date: %s",
issueId, dueDate);
showIssueSuccess(message);
clearIssueForm();
logger.info("Book issued - ID: " + issueId);
} else {
showIssueError("Failed to record issue");
}
} catch (Exception e) {
showIssueError("Error: " + e.getMessage());
logger.severe("Issue book error: " + e.getMessage());
}
}
/**
* Searches for issued book record
*/
private void searchIssuedBook() {
String bookIdStr = returnBookIdField.getText().trim();
if (bookIdStr.isEmpty()) {
returnMessageLabel.setForeground(Color.RED);
returnMessageLabel.setText("Please enter Book ID");
return;
}
try {
int bookId = Integer.parseInt(bookIdStr);
IssuedBook issuedBook = issuedBookDAO.getActiveIssueByBookId(bookId);
if (issuedBook == null) {
returnMessageLabel.setForeground(Color.ORANGE);
returnMessageLabel.setText("No active issue found for this book");
returnDetailsArea.setText("");
} else {
Book book = bookDAO.getBookById(bookId);
String details = String.format(
"Book: %s\nIssued To: %s\nIssued Date: %s\nDue Date: %s\nOverdue: %s",
book != null ? book.getTitle() : "Unknown",
issuedBook.getMemberName(),
issuedBook.getIssuedDate(),
issuedBook.getDueDate(),
issuedBook.isOverdue() ? "YES (" + issuedBook.getDaysOverdue() + " days)" : "No"
);
returnDetailsArea.setText(details);
returnMemberField.setText(issuedBook.getMemberName());
returnMessageLabel.setForeground(new Color(0, 150, 0));
returnMessageLabel.setText("✓ Record found");
}
} catch (NumberFormatException e) {
returnMessageLabel.setForeground(Color.RED);
returnMessageLabel.setText("Book ID must be a number");
}
}
/**
* Handles return book
*/
private void handleReturnBook() {
try {
String bookIdStr = returnBookIdField.getText().trim();
String memberName = returnMemberField.getText().trim();
if (bookIdStr.isEmpty() || memberName.isEmpty()) {
showReturnError("Please fill required fields");
return;
}
int bookId = Integer.parseInt(bookIdStr);
IssuedBook issuedBook = issuedBookDAO.getActiveIssueByBookId(bookId);
if (issuedBook == null) {
showReturnError("No active issue found for this book");
return;
}
if (!issuedBook.getMemberName().equals(memberName)) {
showReturnError("Member name doesn't match");
return;
}
// Update issue status
if (issuedBookDAO.returnBook(issuedBook.getIssueId())) {
// Increment available copies
Book book = bookDAO.getBookById(bookId);
if (book != null) {
book.returnBook();
bookDAO.updateAvailableCopies(bookId, book.getAvailableCopies());
}
double fine = issuedBook.calculateFine();
String message = "✓ Book returned successfully!";
if (fine > 0) {
message += String.format("\nOverdue Fine: ₹%.2f", fine);
}
showReturnSuccess(message);
clearReturnForm();
logger.info("Book returned - ID: " + bookId);
} else {
showReturnError("Failed to process return");
}
} catch (Exception e) {
showReturnError("Error: " + e.getMessage());
logger.severe("Return book error: " + e.getMessage());
}
}
/**
* Displays overdue books
*/
private void displayOverdueBooks() {
// Implementation for overdue display
}
// Message display methods
private void showIssueError(String msg) {
issueMessageLabel.setForeground(Color.RED);
issueMessageLabel.setText(msg);
}
private void showIssueSuccess(String msg) {
issueMessageLabel.setForeground(new Color(0, 150, 0));
issueMessageLabel.setText(msg);
}
private void showReturnError(String msg) {
returnMessageLabel.setForeground(Color.RED);
returnMessageLabel.setText(msg);
}
private void showReturnSuccess(String msg) {
returnMessageLabel.setForeground(new Color(0, 150, 0));
returnMessageLabel.setText(msg);
}
// Clear form methods
private void clearIssueForm() {
bookIdField.setText("");
memberNameField.setText("");
memberContactField.setText("");
bookDetailsLabel.setText("Book details will appear here");
issueMessageLabel.setText(" ");
daysSpinner.setValue(DEFAULT_ISSUE_DAYS);
}
private void clearReturnForm() {
returnBookIdField.setText("");
returnMemberField.setText("");
returnDetailsArea.setText("");
returnMessageLabel.setText(" ");
}
public void refresh() {
clearIssueForm();
clearReturnForm();
}
}