This repository has been archived by the owner on Dec 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
UI.java
2119 lines (2008 loc) · 90.9 KB
/
UI.java
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.library.ui;
import com.library.component.*;
import com.library.main.Library;
import com.library.management.BorrowAndReturn;
import com.library.util.Day;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class UI implements ActionListener, KeyListener {
private final int SCREEN_WIDTH = 1000;
private final int SCREEN_HEIGHT = 600;
private final int GAP = 20;
private final int HEADER_WIDTH = SCREEN_WIDTH * 40 / 100;
private final int HEADER_HEIGHT = SCREEN_HEIGHT * 10 / 100;
private final int LEFT_PANEL_WIDTH = SCREEN_WIDTH * 30 / 100;
private final int LEFT_PANEL_HEIGHT = SCREEN_HEIGHT * 70 / 100;
private final int RIGHT_PANEL_WIDTH = SCREEN_WIDTH * 60 / 100;
private final int RIGHT_PANEL_HEIGHT = SCREEN_HEIGHT * 70 / 100;
private JFrame frame;
private JLabel headerLabel;
private JButton backButton;
private boolean canGoBack = false;
private final int[] enabledButton = { -1, -1, -1, -1 };
private JPanel leftPanel;
private JPanel rightPanel;
private final JButton[] mainButtons = new JButton[4]; // [Book Management, Student Management, Employee Management, Borrow and Return]
private final JButton[] bookButtons = new JButton[3]; // [INPUT, OUTPUT, FIND]
private final JButton[] studentButtons = new JButton[3]; // [INPUT, OUTPUT, FIND]
private final JButton[] employeeButtons = new JButton[3]; // [INPUT, OUTPUT, FIND]
private final JButton[] borrowAndReturnButtons = new JButton[3]; // [BORROW, RETURN, FIND]
private final JLabel[] eduBookLabels = new JLabel[6]; // [ID, Name, Remain, Price, PublishDay, Publisher]
private final JLabel[] refBookLabels = new JLabel[6]; // [ID, Name, Remain, Price, PublishDay, Author]
private final JLabel[] dictionaryLabels = new JLabel[6]; // [ID, Name, Remain, Price, PublishDay, Language]
private final JLabel[] studentLabels = new JLabel[11]; // [ID, Name, DoB, Gender, Phone, Address, Email, School, Faculty, Major, Classroom]
private final JLabel[] employeeLabels = new JLabel[10]; // [ID, Name, DoB, Gender, Phone, Address, Email, Roll, StartDate, Salary]
private final JLabel[] billLabels = new JLabel[7]; // [Status, Person ID, Person name, Books, Borrow day, Return day, Price]
private final JLabel findLabel = new JLabel();
private final JTextField findText = new JTextField();
private final JTextField[] bookText = new JTextField[6];
private final JTextField[] studentText = new JTextField[11];
private final JTextField[] employeeText = new JTextField[10];
private final JTextField[] billText = new JTextField[7];
private ButtonGroup bookButtonGroup;
private ButtonGroup findButtonGroup;
private final JRadioButton[] bookRadioButtons = new JRadioButton[3];
private final JRadioButton[] findRadioButtons = new JRadioButton[2];
private JButton addButton;
private JButton editButton;
private JButton removeButton;
private JButton findButton;
private final String[] booksTableHeader = new String[]{"CAT", "STAT", "ID", "NAME", "REM", "PRICE", "PUBLISH DAY", "NOTE"};
private final String[] studentsTableHeader = new String[]{"STAT", "ID", "NAME", "DOB", "GENDER", "PHONE", "ADDRESS", "EMAIL", "SCHOOL", "FACULTY", "MAJOR", "CLASS"};
private final String[] employeesTableHeader = new String[]{"STAT", "ID", "NAME", "DOB", "GENDER", "PHONE", "ADDRESS", "EMAIL", "ROLL", "START DATE", "SALARY"};
private final String[] billsTableHeader = new String[]{"STAT", "ID", "NAME", "BOOKS", "BORROW", "RETURN", "PRICE"};
private Book[] booksTemp;
private Bill[] billsTemp;
private String[][] booksDataTemp;
private String[][] booksData;
private String[][] studentsData;
private String[][] employeesData;
private String[][] billsData;
private JTable booksTableTemp;
private JTable booksTable;
private JTable studentsTable;
private JTable employeesTable;
private JTable billsTable;
private JScrollPane booksScrollPaneTemp;
private JScrollPane booksScrollPane;
private JScrollPane studentsScrollPane;
private JScrollPane employeesScrollPane;
private JScrollPane billsScrollPane;
private JButton addBookButton;
private JButton returnBookButton;
private JButton nextButton;
private JButton prevButton;
private JLabel steps;
private Font font;
private Border border;
public UI() {
// initialize the User Interface
initComponents();
initMainMenu();
initBookMenu();
initStudentMenu();
initEmployeeMenu();
initBorrowAndReturnMenu();
initInput();
initOutput();
initFind();
initBorrow();
// go to main menu when open the app
gotoMainMenu();
refreshScreen();
}
public void initComponents() {
frame = new JFrame("SGU Library");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
frame.setResizable(false);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("res/sgu_logo.png"));
frame.getContentPane().setBackground(Color.decode("#3e86a0"));
frame.setLayout(null);
frame.setLocationRelativeTo(null);
font = new Font("SansSerif", Font.BOLD, 16);
border = new LineBorder(Color.BLACK, 4);
headerLabel = new JLabel("SGU LIBRARY", JLabel.CENTER);
headerLabel.setBounds((SCREEN_WIDTH - HEADER_WIDTH) / 2, GAP, HEADER_WIDTH, HEADER_HEIGHT);
headerLabel.setFont(new Font("SansSerif", Font.BOLD, 50));
headerLabel.setBorder(border);
backButton = new JButton("MAIN MENU");
backButton.setBounds(GAP, GAP, SCREEN_WIDTH * 15 / 100, HEADER_HEIGHT / 2);
backButton.setFont(new Font("SansSerif", Font.BOLD, 12));
backButton.setBorder(border);
backButton.setFocusable(false);
backButton.setVisible(false);
backButton.addActionListener(this);
leftPanel = new JPanel();
leftPanel.setBackground(Color.decode("#3e86a0"));
rightPanel = new JPanel();
rightPanel.setBounds(GAP + LEFT_PANEL_WIDTH + GAP, GAP + HEADER_HEIGHT + GAP, RIGHT_PANEL_WIDTH, RIGHT_PANEL_HEIGHT);
rightPanel.setBorder(border);
rightPanel.setLayout(null);
rightPanel.setBackground(Color.WHITE);
frame.add(headerLabel);
frame.add(backButton);
frame.add(leftPanel);
frame.add(rightPanel);
frame.addKeyListener(this);
frame.setVisible(true);
}
public void initMainMenu() {
mainButtons[0] = new JButton("Book Management");
mainButtons[1] = new JButton("Student Management");
mainButtons[2] = new JButton("Employee Management");
mainButtons[3] = new JButton("Borrow and Return");
for (JButton button : mainButtons) {
button.addActionListener(this);
button.setFont(font);
button.setBorder(border);
button.setFocusable(false);
}
}
public void initBookMenu() {
bookButtons[0] = new JButton("Input Book");
bookButtons[1] = new JButton("Output Book");
bookButtons[2] = new JButton("Find Book");
for (JButton button : bookButtons) {
button.addActionListener(this);
button.setFont(font);
button.setBorder(border);
button.setFocusable(false);
}
}
public void initStudentMenu() {
studentButtons[0] = new JButton("Input Student");
studentButtons[1] = new JButton("Output Student");
studentButtons[2] = new JButton("Find Student");
for (JButton button : studentButtons) {
button.addActionListener(this);
button.setFont(font);
button.setBorder(border);
button.setFocusable(false);
}
}
public void initEmployeeMenu() {
employeeButtons[0] = new JButton("Input Employee");
employeeButtons[1] = new JButton("Output Employee");
employeeButtons[2] = new JButton("Find Employee");
for (JButton button : employeeButtons) {
button.addActionListener(this);
button.setFont(font);
button.setBorder(border);
button.setFocusable(false);
}
}
public void initBorrowAndReturnMenu() {
borrowAndReturnButtons[0] = new JButton("Borrow Book");
borrowAndReturnButtons[1] = new JButton("Return Book");
borrowAndReturnButtons[2] = new JButton("Find Bill");
for (JButton button : borrowAndReturnButtons) {
button.addActionListener(this);
button.setFont(font);
button.setBorder(border);
button.setFocusable(false);
}
}
public void initInput() {
Font inputFont = new Font("Consolas", Font.PLAIN, 16);
bookRadioButtons[0] = new JRadioButton("Education Book");
bookRadioButtons[1] = new JRadioButton("Reference Book");
bookRadioButtons[2] = new JRadioButton("Dictionary");
bookButtonGroup = new ButtonGroup();
for (int i = 0; i < 3; i++) {
bookRadioButtons[i].setBackground(Color.WHITE);
bookRadioButtons[i].setFont(font);
bookRadioButtons[i].setFocusable(false);
bookRadioButtons[i].addActionListener(this);
bookButtonGroup.add(bookRadioButtons[i]);
}
eduBookLabels[0] = new JLabel("Education book ID");
eduBookLabels[1] = new JLabel("Education book name");
eduBookLabels[2] = new JLabel("Education book remain");
eduBookLabels[3] = new JLabel("Education book price");
eduBookLabels[4] = new JLabel("Education book publish day");
eduBookLabels[5] = new JLabel("Education book publisher");
refBookLabels[0] = new JLabel("Reference book ID");
refBookLabels[1] = new JLabel("Reference book name");
refBookLabels[2] = new JLabel("Reference book remain");
refBookLabels[3] = new JLabel("Reference book price");
refBookLabels[4] = new JLabel("Reference book publish day");
refBookLabels[5] = new JLabel("Reference book author");
dictionaryLabels[0] = new JLabel("Dictionary ID");
dictionaryLabels[1] = new JLabel("Dictionary name");
dictionaryLabels[2] = new JLabel("Dictionary remain");
dictionaryLabels[3] = new JLabel("Dictionary price");
dictionaryLabels[4] = new JLabel("Dictionary publish day");
dictionaryLabels[5] = new JLabel("Dictionary language");
for (int i = 0; i < 6; i++) {
eduBookLabels[i].setFont(font);
refBookLabels[i].setFont(font);
dictionaryLabels[i].setFont(font);
if (i == 4) // Format Dictionary publish day
bookText[i] = new JFormattedTextField(createFormatter("##/##/####"));
else
bookText[i] = new JTextField();
bookText[i].setFont(inputFont);
}
studentLabels[0] = new JLabel("Student ID");
studentLabels[1] = new JLabel("Student name");
studentLabels[2] = new JLabel("Student date of birth");
studentLabels[3] = new JLabel("Student gender");
studentLabels[4] = new JLabel("Student phone");
studentLabels[5] = new JLabel("Student address");
studentLabels[6] = new JLabel("Student email");
studentLabels[7] = new JLabel("Student school");
studentLabels[8] = new JLabel("Student faculty");
studentLabels[9] = new JLabel("Student major");
studentLabels[10] = new JLabel("Student classroom");
for (int i = 0; i < 11; i++) {
studentLabels[i].setFont(font);
studentText[i] = new JTextField();
studentText[i].setFont(inputFont);
}
studentText[2] = new JFormattedTextField(createFormatter("##/##/####"));
studentText[2].setFont(inputFont);
studentText[7].setText("Đại học Sài Gòn");
studentText[7].setEditable(false);
employeeLabels[0] = new JLabel("Employee ID");
employeeLabels[1] = new JLabel("Employee name");
employeeLabels[2] = new JLabel("Employee date of birth");
employeeLabels[3] = new JLabel("Employee gender");
employeeLabels[4] = new JLabel("Employee phone");
employeeLabels[5] = new JLabel("Employee address");
employeeLabels[6] = new JLabel("Employee email");
employeeLabels[7] = new JLabel("Employee roll");
employeeLabels[8] = new JLabel("Employee start date");
employeeLabels[9] = new JLabel("Employee salary");
for (int i = 0; i < 10; i++) {
employeeLabels[i].setFont(font);
employeeText[i] = new JTextField();
employeeText[i].setFont(inputFont);
}
employeeText[2] = new JFormattedTextField(createFormatter("##/##/####"));
employeeText[2].setFont(inputFont);
employeeText[8] = new JFormattedTextField(createFormatter("##/##/####"));
employeeText[8].setFont(inputFont);
billLabels[0] = new JLabel("Status");
billLabels[1] = new JLabel("Person ID");
billLabels[2] = new JLabel("Person name");
billLabels[3] = new JLabel("Books");
billLabels[4] = new JLabel("Borrow day");
billLabels[5] = new JLabel("Return day");
billLabels[6] = new JLabel("Price");
for (int i = 0; i < 7; i++) {
billLabels[i].setFont(font);
billText[i] = new JTextField();
billText[i].setFont(inputFont);
}
billText[4] = new JFormattedTextField(createFormatter("##/##/####"));
billText[4].setFont(inputFont);
billText[5] = new JFormattedTextField(createFormatter("##/##/####"));
billText[5].setFont(inputFont);
addButton = new JButton();
addButton.setBounds(RIGHT_PANEL_WIDTH / 3, RIGHT_PANEL_HEIGHT - 2 * GAP, 200, 30);
addButton.setBorder(border);
addButton.setFocusable(false);
addButton.addActionListener(this);
}
public void initOutput() {
// BOOKS
Book[] books = Library.getBookManagement().getBooks();
booksData = new String[0][8];
for (Book book : books) {
if (book.getStatus().equals("enabled")) {
booksData = Arrays.copyOf(booksData, booksData.length + 1);
booksData[booksData.length - 1] = book.toString().split(", ");
}
}
booksTable = new JTable(new DefaultTableModel(booksData, booksTableHeader)) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
booksTable.getTableHeader().setReorderingAllowed(false);
booksTable.setAutoCreateRowSorter(false);
booksTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
booksTable.getSelectionModel().addListSelectionListener(e -> {
if (booksTable.getSelectedRow() != -1) { // prevent calling the event twice (mouse down and mouse released)
String[] book = booksData[booksTable.getSelectedRow()];
if (book[0].equals("EDU"))
bookRadioButtons[0].setSelected(true);
else if (book[0].equals("REF"))
bookRadioButtons[1].setSelected(true);
else
bookRadioButtons[2].setSelected(true);
for (int i = 0; i < bookText.length; i++)
bookText[i].setText(book[i + 2]);
showInput("Book");
bookText[0].setEditable(false);
rightPanel.remove(booksScrollPane);
rightPanel.remove(addButton);
for (int i = 0; i < 3; i++)
rightPanel.remove(bookRadioButtons[i]);
backButton.setText("BOOK TABLE");
refreshScreen();
}
});
TableColumnModel booksColumnModel = booksTable.getColumnModel();
booksColumnModel.getColumn(0).setPreferredWidth(37);
booksColumnModel.getColumn(2).setPreferredWidth(40);
booksColumnModel.getColumn(3).setPreferredWidth(160);
booksColumnModel.getColumn(4).setPreferredWidth(37);
booksColumnModel.getColumn(5).setPreferredWidth(65);
booksColumnModel.getColumn(6).setPreferredWidth(87);
booksColumnModel.getColumn(7).setPreferredWidth(160);
booksColumnModel.removeColumn(booksColumnModel.getColumn(1)); // remove STAT
booksScrollPane = new JScrollPane(booksTable);
booksScrollPane.setBounds(5, 5, 591, 411);
// STUDENTS
Student[] students = Library.getStudentManagement().getStudents();
studentsData = new String[0][11];
for (Student book : students) {
if (book.getStatus().equals("enabled")) {
studentsData = Arrays.copyOf(studentsData, studentsData.length + 1);
studentsData[studentsData.length - 1] = book.toString().split(", ");
}
}
studentsTable = new JTable(new DefaultTableModel(studentsData, studentsTableHeader)) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
studentsTable.getTableHeader().setReorderingAllowed(false);
studentsTable.setAutoCreateRowSorter(false);
studentsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
studentsTable.getSelectionModel().addListSelectionListener(e -> {
if (studentsTable.getSelectedRow() != -1) { // prevent calling the event twice (mouse down and mouse released)
String[] student = studentsData[studentsTable.getSelectedRow()];
for (int i = 0; i < studentText.length; i++)
studentText[i].setText(student[i + 1]);
showInput("Student");
studentText[0].setEditable(false);
rightPanel.remove(studentsScrollPane);
rightPanel.remove(addButton);
backButton.setText("STUDENT TABLE");
refreshScreen();
}
});
TableColumnModel studentsColumnModel = studentsTable.getColumnModel();
studentsColumnModel.getColumn(1).setPreferredWidth(70);
studentsColumnModel.getColumn(2).setPreferredWidth(160);
studentsColumnModel.getColumn(3).setPreferredWidth(70);
studentsColumnModel.getColumn(4).setPreferredWidth(65);
studentsColumnModel.getColumn(5).setPreferredWidth(77);
studentsColumnModel.getColumn(6).setPreferredWidth(140);
studentsColumnModel.getColumn(11).setPreferredWidth(70);
studentsColumnModel.removeColumn(studentsColumnModel.getColumn(0)); // remove STAT
studentsColumnModel.removeColumn(studentsColumnModel.getColumn(6)); // remove EMAIL
studentsColumnModel.removeColumn(studentsColumnModel.getColumn(6)); // remove SCHOOL
studentsColumnModel.removeColumn(studentsColumnModel.getColumn(6)); // remove FACULTY
studentsColumnModel.removeColumn(studentsColumnModel.getColumn(6)); // remove MAJOR
studentsScrollPane = new JScrollPane(studentsTable);
studentsScrollPane.setBounds(5, 5, 591, 411);
// EMPLOYEES
Employee[] employees = Library.getEmployeeManagement().getEmployees();
employeesData = new String[0][10];
for (Employee employee : employees) {
if (employee.getStatus().equals("enabled")) {
employeesData = Arrays.copyOf(employeesData, employeesData.length + 1);
employeesData[employeesData.length - 1] = employee.toString().split(", ");
}
}
employeesTable = new JTable(new DefaultTableModel(employeesData, employeesTableHeader)) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
employeesTable.getTableHeader().setReorderingAllowed(false);
employeesTable.setAutoCreateRowSorter(false);
employeesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
employeesTable.getSelectionModel().addListSelectionListener(e -> {
if (employeesTable.getSelectedRow() != -1) { // prevent calling the event twice (mouse down and mouse released)
String[] employee = employeesData[employeesTable.getSelectedRow()];
for (int i = 0; i < employeeText.length; i++)
employeeText[i].setText(employee[i + 1]);
showInput("Employee");
employeeText[0].setEditable(false);
rightPanel.remove(employeesScrollPane);
rightPanel.remove(addButton);
backButton.setText("EMPLOYEE TABLE");
refreshScreen();
}
});
TableColumnModel employeesColumnModel = employeesTable.getColumnModel();
employeesColumnModel.getColumn(1).setPreferredWidth(70);
employeesColumnModel.getColumn(2).setPreferredWidth(160);
employeesColumnModel.getColumn(3).setPreferredWidth(70);
employeesColumnModel.getColumn(4).setPreferredWidth(65);
employeesColumnModel.getColumn(5).setPreferredWidth(77);
employeesColumnModel.getColumn(6).setPreferredWidth(140);
employeesColumnModel.getColumn(8).setPreferredWidth(100);
employeesColumnModel.removeColumn(employeesColumnModel.getColumn(0)); // remove STAT
employeesColumnModel.removeColumn(employeesColumnModel.getColumn(6)); // remove EMAIL
employeesColumnModel.removeColumn(employeesColumnModel.getColumn(7)); // remove START DATE
employeesColumnModel.removeColumn(employeesColumnModel.getColumn(7)); // remove SALARY
employeesScrollPane = new JScrollPane(employeesTable);
employeesScrollPane.setBounds(5, 5, 591, 411);
editButton = new JButton();
editButton.setBounds(RIGHT_PANEL_WIDTH / 7, RIGHT_PANEL_HEIGHT - 2 * GAP, 170, 30);
editButton.setBorder(border);
editButton.setFocusable(false);
editButton.addActionListener(this);
removeButton = new JButton();
removeButton.setBounds(RIGHT_PANEL_WIDTH * 4 / 7, RIGHT_PANEL_HEIGHT - 2 * GAP, 170, 30);
removeButton.setBorder(border);
removeButton.setFocusable(false);
removeButton.addActionListener(this);
}
public void initFind() {
findRadioButtons[0] = new JRadioButton("Find by ID");
findRadioButtons[1] = new JRadioButton("Find by name");
findButtonGroup = new ButtonGroup();
for (int i = 0; i < 2; i++) {
findRadioButtons[i].setBackground(Color.WHITE);
findRadioButtons[i].setFont(font);
findRadioButtons[i].setFocusable(false);
findRadioButtons[i].addActionListener(this);
findButtonGroup.add(findRadioButtons[i]);
}
findButton = new JButton();
findButton.setBounds(RIGHT_PANEL_WIDTH / 3, RIGHT_PANEL_HEIGHT - 2 * GAP, 200, 30);
findButton.setBorder(border);
findButton.setFocusable(false);
findButton.addActionListener(this);
findLabel.setBounds(RIGHT_PANEL_WIDTH * 2 / 7, RIGHT_PANEL_HEIGHT / 5, 200, 25);
findLabel.setFont(font);
findText.setBounds(RIGHT_PANEL_WIDTH / 2, RIGHT_PANEL_HEIGHT / 5, 200, 25);
findText.setFont(new Font("Consolas", Font.PLAIN, 16));
}
public void initBorrow() {
nextButton = new JButton("NEXT");
nextButton.setBounds(RIGHT_PANEL_WIDTH - 4 * GAP, RIGHT_PANEL_HEIGHT - 2 * GAP, 50, 30);
nextButton.setBorder(border);
nextButton.addActionListener(this);
prevButton = new JButton("BACK");
prevButton.setBounds(GAP, RIGHT_PANEL_HEIGHT - 2 * GAP, 50, 30);
prevButton.setBorder(border);
prevButton.addActionListener(this);
steps = new JLabel("1/4");
steps.setBounds(RIGHT_PANEL_WIDTH / 2, RIGHT_PANEL_HEIGHT - 2 * GAP, 50, 30);
// BILLS
Bill[] bills = BorrowAndReturn.getBills();
billsData = new String[bills.length][7];
for (int i = 0; i < billsData.length; i++)
billsData[i] = bills[i].toString().split(", ");
billsTable = new JTable(new DefaultTableModel(billsData, billsTableHeader)) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
billsTable.getTableHeader().setReorderingAllowed(false);
billsTable.setAutoCreateRowSorter(false);
billsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
billsTable.getSelectionModel().addListSelectionListener(e -> {
if (billsTable.getSelectedRow() != -1) { // prevent calling the event twice (mouse down and mouse released)
String[] bill = billsData[billsTable.getSelectedRow()];
for (int i = 0; i < billText.length; i++)
billText[i].setText(bill[i]);
showInput("Bill");
billText[0].setEditable(false);
rightPanel.remove(billsScrollPane);
rightPanel.remove(addButton);
rightPanel.remove(editButton);
rightPanel.remove(removeButton);
backButton.setText("BILL TABLE");
refreshScreen();
}
});
for (int i = 0; i < 7; i++) {
billLabels[i].setBounds(RIGHT_PANEL_WIDTH / 10, RIGHT_PANEL_HEIGHT / 15 + i * 33, 200, 25);
billText[i].setBounds(RIGHT_PANEL_WIDTH * 4 / 10, RIGHT_PANEL_HEIGHT / 15 + i * 33, 300, 25);
}
TableColumnModel billColumnModel = billsTable.getColumnModel();
billColumnModel.getColumn(0).setPreferredWidth(70);
billColumnModel.getColumn(1).setPreferredWidth(60);
billColumnModel.getColumn(2).setPreferredWidth(150);
billColumnModel.getColumn(3).setPreferredWidth(150);
billColumnModel.getColumn(4).setPreferredWidth(80);
billColumnModel.getColumn(5).setPreferredWidth(80);
billsScrollPane = new JScrollPane(billsTable);
billsScrollPane.setBounds(5, 5, 591, 411);
// BOOKS
addBookButton = new JButton("ADD");
addBookButton.setBounds(RIGHT_PANEL_WIDTH * 9 / 10, RIGHT_PANEL_HEIGHT / 15 + 3 * 33, 50, 25);
addBookButton.setBorder(border);
addBookButton.addActionListener(this);
returnBookButton = new JButton("\u2713");
returnBookButton.setBounds(RIGHT_PANEL_WIDTH * 9 / 10, RIGHT_PANEL_HEIGHT / 15, 50, 25);
returnBookButton.setBorder(border);
returnBookButton.addActionListener(this);
booksDataTemp = new String[0][8];
booksTableTemp = new JTable(new DefaultTableModel(booksDataTemp, booksTableHeader)) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
booksTableTemp.getTableHeader().setReorderingAllowed(false);
booksTableTemp.setAutoCreateRowSorter(false);
booksTableTemp.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
booksTableTemp.getSelectionModel().addListSelectionListener(e -> {
if (!e.getValueIsAdjusting() && booksTableTemp.getSelectedRow() != -1) { // prevent calling the event twice (mouse down and mouse released)
int row = booksTableTemp.getSelectedRow();
Book book = Library.getBookManagement().findBook(Integer.parseInt(booksDataTemp[row][2]));
book.setRemain(book.getRemain() + 1);
DefaultTableModel model = (DefaultTableModel) booksTableTemp.getModel();
for (int i = 0; i < model.getRowCount(); i++) {
if (model.getValueAt(i, 2).equals(booksDataTemp[row][2])) {
booksDataTemp[i][4] = Integer.toString(book.getRemain());
model.setValueAt(booksDataTemp[i][4], i, 4); // refresh remaining book
}
}
for (int i = row; i < booksTemp.length - 1; i++) {
booksTemp[i] = booksTemp[i + 1];
booksDataTemp[i] = booksDataTemp[i + 1];
}
booksTemp = Arrays.copyOf(booksTemp, booksTemp.length - 1);
booksDataTemp = Arrays.copyOf(booksDataTemp, booksDataTemp.length - 1);
model.removeRow(row);
refreshScreen();
}
});
TableColumnModel booksColumnModel = booksTableTemp.getColumnModel();
booksColumnModel.getColumn(0).setPreferredWidth(37);
booksColumnModel.getColumn(2).setPreferredWidth(40);
booksColumnModel.getColumn(3).setPreferredWidth(160);
booksColumnModel.getColumn(4).setPreferredWidth(37);
booksColumnModel.getColumn(5).setPreferredWidth(65);
booksColumnModel.getColumn(6).setPreferredWidth(87);
booksColumnModel.getColumn(7).setPreferredWidth(160);
booksColumnModel.removeColumn(booksColumnModel.getColumn(1)); // remove STAT
booksScrollPaneTemp = new JScrollPane(booksTableTemp);
booksScrollPaneTemp.setBounds(5, RIGHT_PANEL_HEIGHT / 2, 591, RIGHT_PANEL_HEIGHT / 4);
}
public void refreshScreen() {
leftPanel.revalidate();
leftPanel.repaint();
rightPanel.revalidate();
rightPanel.repaint();
}
public void clearScreen() {
leftPanel.removeAll();
rightPanel.removeAll();
refreshScreen();
}
public void gotoMainMenu() {
canGoBack = false;
leftPanel.setBounds(GAP, GAP + HEADER_HEIGHT + LEFT_PANEL_HEIGHT * 2 / 7, LEFT_PANEL_WIDTH, LEFT_PANEL_HEIGHT * 4 / 7);
leftPanel.setLayout(new GridLayout(4, 1, 0, 10));
for (JButton button : mainButtons) {
leftPanel.add(button);
}
}
public void gotoBookMenu() {
canGoBack = true;
leftPanel.setBounds(GAP, GAP + HEADER_HEIGHT + LEFT_PANEL_HEIGHT * 2 / 7, LEFT_PANEL_WIDTH, LEFT_PANEL_HEIGHT * 4 / 7);
leftPanel.setLayout(new GridLayout(3, 1, 0, 10));
for (JButton button : bookButtons) {
button.setEnabled(true);
leftPanel.add(button);
}
}
public void gotoStudentMenu() {
canGoBack = true;
leftPanel.setBounds(GAP, GAP + HEADER_HEIGHT + LEFT_PANEL_HEIGHT * 2 / 7, LEFT_PANEL_WIDTH, LEFT_PANEL_HEIGHT * 4 / 7);
leftPanel.setLayout(new GridLayout(3, 1, 0, 10));
for (JButton button : studentButtons) {
button.setEnabled(true);
leftPanel.add(button);
}
}
public void gotoEmployeeMenu() {
canGoBack = true;
leftPanel.setBounds(GAP, GAP + HEADER_HEIGHT + LEFT_PANEL_HEIGHT * 2 / 7, LEFT_PANEL_WIDTH, LEFT_PANEL_HEIGHT * 4 / 7);
leftPanel.setLayout(new GridLayout(3, 1, 0, 10));
for (JButton button : employeeButtons) {
button.setEnabled(true);
leftPanel.add(button);
}
}
public void gotoBorrowAndReturnMenu() {
canGoBack = true;
leftPanel.setBounds(GAP, GAP + HEADER_HEIGHT + LEFT_PANEL_HEIGHT * 2 / 7, LEFT_PANEL_WIDTH, LEFT_PANEL_HEIGHT * 4 / 7);
leftPanel.setLayout(new GridLayout(3, 1, 0, 10));
for (JButton button : borrowAndReturnButtons) {
button.setEnabled(true);
leftPanel.add(button);
}
}
public void showInputBook(String type) {
for (int i = 0; i < 6; i++) {
rightPanel.remove(eduBookLabels[i]);
rightPanel.remove(refBookLabels[i]);
rightPanel.remove(dictionaryLabels[i]);
rightPanel.remove(bookText[i]);
}
if (type.equals("Education")) {
for (int i = 0; i < 6; i++) {
eduBookLabels[i].setBounds(RIGHT_PANEL_WIDTH / 10, RIGHT_PANEL_HEIGHT * 2 / 10 + i * 35, 250, 25);
rightPanel.add(eduBookLabels[i]);
}
}
if (type.equals("Reference")) {
for (int i = 0; i < 6; i++) {
refBookLabels[i].setBounds(RIGHT_PANEL_WIDTH / 10, RIGHT_PANEL_HEIGHT * 2 / 10 + i * 35, 250, 25);
rightPanel.add(refBookLabels[i]);
}
}
if (type.equals("Dictionary")) {
for (int i = 0; i < 6; i++) {
dictionaryLabels[i].setBounds(RIGHT_PANEL_WIDTH / 10, RIGHT_PANEL_HEIGHT * 2 / 10 + i * 35, 250, 25);
rightPanel.add(dictionaryLabels[i]);
}
}
for (int i = 0; i < 6; i++) {
bookText[i].setBounds(RIGHT_PANEL_WIDTH / 2, RIGHT_PANEL_HEIGHT / 5 + i * 35, 250, 25);
rightPanel.add(bookText[i]);
}
}
public void showInput(String type) {
backButton.setText("BACK");
if (type.equals("Book")) {
for (int i = 0; i < 3; i++) {
bookRadioButtons[i].setBounds(RIGHT_PANEL_WIDTH / 20 + i * 200, GAP, 150, 25);
rightPanel.add(bookRadioButtons[i]);
}
if (bookRadioButtons[0].isSelected())
showInputBook("Education");
if (bookRadioButtons[1].isSelected())
showInputBook("Reference");
if (bookRadioButtons[2].isSelected())
showInputBook("Dictionary");
addButton.setText("ADD NEW BOOK");
editButton.setText("EDIT BOOK");
removeButton.setText("REMOVE BOOK");
bookText[0].setEditable(true);
}
if (type.equals("Student")) {
for (int i = 0; i < 11; i++) {
studentLabels[i].setBounds(RIGHT_PANEL_WIDTH / 10, RIGHT_PANEL_HEIGHT / 20 + i * 33, 200, 25);
rightPanel.add(studentLabels[i]);
studentText[i].setBounds(RIGHT_PANEL_WIDTH * 4 / 10, RIGHT_PANEL_HEIGHT / 20 + i * 33, 300, 25);
rightPanel.add(studentText[i]);
}
addButton.setText("ADD NEW STUDENT");
editButton.setText("EDIT STUDENT");
removeButton.setText("REMOVE STUDENT");
studentText[0].setEditable(true);
}
if (type.equals("Employee")) {
for (int i = 0; i < 10; i++) {
employeeLabels[i].setBounds(RIGHT_PANEL_WIDTH / 10, RIGHT_PANEL_HEIGHT / 20 + i * 33, 200, 25);
rightPanel.add(employeeLabels[i]);
employeeText[i].setBounds(RIGHT_PANEL_WIDTH * 4 / 10, RIGHT_PANEL_HEIGHT / 20 + i * 33, 300, 25);
rightPanel.add(employeeText[i]);
}
addButton.setText("ADD NEW EMPLOYEE");
editButton.setText("EDIT EMPLOYEE");
removeButton.setText("REMOVE EMPLOYEE");
employeeText[0].setEditable(true);
}
if (type.equals("Bill")) {
for (int i = 0; i < 7; i++) {
billText[i].setEditable(false);
rightPanel.add(billLabels[i]);
rightPanel.add(billText[i]);
}
if (billText[0].getText().equals("Borrowed")) {
rightPanel.add(returnBookButton);
}
}
rightPanel.add(addButton);
rightPanel.add(editButton);
rightPanel.add(removeButton);
}
public void showOutput(String type, boolean clearSelection) {
backButton.setText("BACK");
if (type.equals("Book")) {
if (clearSelection)
booksTable.clearSelection();
rightPanel.add(booksScrollPane);
}
if (type.equals("Student")) {
if (clearSelection)
studentsTable.clearSelection();
rightPanel.add(studentsScrollPane);
}
if (type.equals("Employee")) {
if (clearSelection)
employeesTable.clearSelection();
rightPanel.add(employeesScrollPane);
}
}
public void showFoundItem(Object object) {
if (object instanceof Book foundBook) {
bookText[0].setText(Integer.toString(foundBook.getId()));
bookText[1].setText(foundBook.getName());
bookText[2].setText(Integer.toString(foundBook.getRemain()));
bookText[3].setText(Double.toString(foundBook.getPrice()));
bookText[4].setText(foundBook.getPublishDay().toString());
if (object instanceof EducationBook eduBook) {
bookRadioButtons[0].setSelected(true);
bookText[5].setText(eduBook.getPublisher());
}
if (object instanceof ReferenceBook refBook) {
bookRadioButtons[1].setSelected(true);
bookText[5].setText(refBook.getAuthor());
}
if (object instanceof Dictionary dictionary) {
bookRadioButtons[2].setSelected(true);
bookText[5].setText(dictionary.getLanguage());
}
showInput("Book");
for (int i = 0; i < 3; i++)
rightPanel.remove(bookRadioButtons[i]);
bookText[0].setEditable(false);
}
if (object instanceof Student foundStudent) {
studentText[0].setText(Integer.toString(foundStudent.getId()));
studentText[1].setText(foundStudent.getName());
studentText[2].setText(foundStudent.getDob().toString());
studentText[3].setText(foundStudent.getGender());
studentText[4].setText(foundStudent.getPhone());
studentText[5].setText(foundStudent.getAddress());
studentText[6].setText(foundStudent.getEmail());
studentText[8].setText(foundStudent.getFaculty());
studentText[9].setText(foundStudent.getMajor());
studentText[10].setText(foundStudent.getClassroom());
showInput("Student");
studentText[0].setEditable(false);
}
if (object instanceof Employee foundEmployee) {
employeeText[0].setText(Integer.toString(foundEmployee.getId()));
employeeText[1].setText(foundEmployee.getName());
employeeText[2].setText(foundEmployee.getDob().toString());
employeeText[3].setText(foundEmployee.getGender());
employeeText[4].setText(foundEmployee.getPhone());
employeeText[5].setText(foundEmployee.getAddress());
employeeText[6].setText(foundEmployee.getEmail());
employeeText[7].setText(foundEmployee.getRoll());
employeeText[8].setText(foundEmployee.getStartDate().toString());
employeeText[9].setText(Double.toString(foundEmployee.getSalary()));
showInput("Employee");
employeeText[0].setEditable(false);
}
rightPanel.remove(addButton);
rightPanel.remove(findRadioButtons[0]);
rightPanel.remove(findRadioButtons[1]);
rightPanel.remove(findLabel);
rightPanel.remove(findText);
rightPanel.remove(findButton);
}
public void showFoundItems(Bill[] foundBill) {
DefaultTableModel model = (DefaultTableModel) billsTable.getModel();
while (model.getRowCount() != 0)
model.removeRow(0);
billsData = new String[foundBill.length][7];
for (int i = 0; i < billsData.length; i++)
billsData[i] = foundBill[i].toString().split(", ");
for (Bill bill : foundBill)
model.addRow(bill.toString().split(", "));
rightPanel.add(billsScrollPane);
rightPanel.remove(findRadioButtons[0]);
rightPanel.remove(findRadioButtons[1]);
rightPanel.remove(findLabel);
rightPanel.remove(findText);
rightPanel.remove(findButton);
}
public void showFind(String type) {
backButton.setText("BACK");
for (int i = 0; i < 2; i++) {
findRadioButtons[i].setBounds(RIGHT_PANEL_WIDTH / 5 + i * 200, GAP, 150, 25);
rightPanel.add(findRadioButtons[i]);
}
if (type.equals("Book")) {
if (findRadioButtons[0].isSelected())
findLabel.setText("Book ID");
if (findRadioButtons[1].isSelected())
findLabel.setText("Book name");
findButton.setText("FIND BOOK");
}
if (type.equals("Student")) {
if (findRadioButtons[0].isSelected())
findLabel.setText("Student ID");
if (findRadioButtons[1].isSelected())
findLabel.setText("Student name");
findButton.setText("FIND STUDENT");
}
if (type.equals("Employee")) {
if (findRadioButtons[0].isSelected())
findLabel.setText("Employee ID");
if (findRadioButtons[1].isSelected())
findLabel.setText("Employee name");
findButton.setText("FIND EMPLOYEE");
}
if (type.equals("Bill")) {
if (findRadioButtons[0].isSelected())
findLabel.setText("Person ID");
if (findRadioButtons[1].isSelected())
findLabel.setText("Person name");
findButton.setText("FIND BILL");
}
rightPanel.add(findRadioButtons[0]);
rightPanel.add(findRadioButtons[1]);
rightPanel.add(findLabel);
rightPanel.add(findText);
rightPanel.add(findButton);
}
public void showBorrow(int step) {
backButton.setText("BACK");
rightPanel.add(prevButton);
rightPanel.add(nextButton);
rightPanel.add(steps);
rightPanel.remove(addBookButton);
rightPanel.remove(booksScrollPaneTemp);
for (int i = 1; i < 7; i++) {
rightPanel.add(billLabels[i]);
rightPanel.add(billText[i]);
billText[i].setEditable(true);
}
if (step == 1) {
for (int i = 2; i < 7; i++) {
rightPanel.remove(billLabels[i]);
rightPanel.remove(billText[i]);
}
}
if (step == 2) {
billText[1].setEditable(false);
billText[2].setEditable(false);
billText[3].setText("");
for (int i = 4; i < 7; i++) {
rightPanel.remove(billLabels[i]);
rightPanel.remove(billText[i]);
}
rightPanel.add(addBookButton);
rightPanel.add(booksScrollPaneTemp);
}
if (step == 3) {
String books = booksTemp[0].getName();
for (int i = 1; i < booksTemp.length; i++)
books += " / " + booksTemp[i].getName();
billText[1].setEditable(false);
billText[2].setEditable(false);
billText[3].setEditable(false);
billText[3].setText(books);
rightPanel.remove(billLabels[6]);
rightPanel.remove(billText[6]);
}
if (step == 4) {
for (int i = 1; i < 7; i++)
billText[i].setEditable(false);
}
}
public void showReturn() {
backButton.setText("BACK");
Bill[] bills = BorrowAndReturn.getBills();
DefaultTableModel model = (DefaultTableModel) billsTable.getModel();
while (model.getRowCount() != 0)
model.removeRow(0);
billsData = new String[0][7];
billsTemp = new Bill[0];
for (Bill bill : bills) {
if (bill.getStatus().equals("Borrowed")) {
billsData = Arrays.copyOf(billsData, billsTemp.length + 1);
billsData[billsData.length - 1] = bill.toString().split(", ");
billsTemp = Arrays.copyOf(billsTemp, billsTemp.length + 1);
billsTemp[billsTemp.length - 1] = bill;
model.addRow(bill.toString().split(", "));
}
}
rightPanel.add(billsScrollPane);
}
public void showError(String message) {
JOptionPane.showMessageDialog(frame, message, "Error", JOptionPane.ERROR_MESSAGE);
}
public void showValidity(String message) {
JOptionPane.showMessageDialog(frame, message, "Success", JOptionPane.INFORMATION_MESSAGE);
}
public int showConfirm(String message) {
return JOptionPane.showConfirmDialog(frame, message, "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
}
public Book validateBook() {
int id;
if (bookText[0].getText().isBlank()) {
showError("Please input ID!");
return null;
}
try {
id = Integer.parseInt(bookText[0].getText());
if (bookText[0].getText().length() != 4) {
showError("ID must have 4 digits!");
return null;
} else if (id <= 0) {
showError("ID cannot be a negative number!");
return null;
}
} catch (Exception exception) {
showError("ID must be a number!");