Repository navigation
Expand file tree
/
Copy pathManager.java
More file actions
1414 lines (1171 loc) · 54.7 KB
/
Copy pathManager.java
File metadata and controls
1414 lines (1171 loc) · 54.7 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
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
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.IOException;
public class Manager extends User{
String[] manages;
public Manager(String[] data, DBConnect x){
db = x;
id = Integer.parseInt(data[0]);
username = data[1];
ManagedBy = Integer.parseInt(data[4]); //With the exception of login(), skip data[2], userType can be determined by class type.
email = data[5];
password = data[2];
firstName = data[6];
lastName = data[7];
gender = data[8];
dob = data[9];
userType = data[3];
manages = db.read("SELECT User_Id FROM User WHERE Managed_By_Id = " + id + ";");
}
public void viewWorkflow(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Users not yet registered");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(1125,400);
String[] users = db.read("SELECT User_Id FROM User WHERE Registered = '0';");
String[][] rowData = new String[users.length][9];
for (int i = 0; i < users.length; i++) {
String[] result = db.read("SELECT Username, User_Type, Email, First_Name, Surname, Gender, DOB, Qualification FROM User WHERE User_Id = " + users[i] + ";");
rowData[i][0] = users[i];
rowData[i][1] = result[0];
rowData[i][2] = result[1];
rowData[i][3] = result[2];
rowData[i][4] = result[3];
rowData[i][5] = result[4];
rowData[i][6] = result[5];
rowData[i][7] = result[6];
rowData[i][8] = result[7];
}
String[] columnNames = {"User Id", "Username", "User Type", "Email", "First Name", "Surname", "Gender", "Date of Birth", "Qualification"};
DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
public void approveUser(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Approve User");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400,400);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setVisible(true);
frame.setVisible(true);
JButton submit = new JButton();
submit.setText("Approve");
submit.setBounds(140,200,120,50);
JLabel userId = new JLabel("User Id");
userId.setBounds(50,10,300,30);
JTextField userIdf = new JTextField();
userIdf.setBounds(50,40,300,30);
panel.add(submit);
panel.add(userId);
panel.add(userIdf);
frame.add(panel);
submit.addActionListener(e -> {
String userIdText = userIdf.getText();
try {
int user_Id = Integer.parseInt(userIdText);
approveUser2(user_Id);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter a valid user ID.", "Error", JOptionPane.ERROR_MESSAGE);
}
});
}
public void approveUser2(Integer x){
String[] registered = db.read("SELECT * FROM User WHERE User_Id = " + x + " AND Registered = 1;" );
String[] exists = db.read("SELECT * FROM User WHERE User_Id = " + x + ";");
if (exists.length == 0){
JOptionPane.showMessageDialog(null, "That user Id does not exist", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (registered.length == 0){
db.write("UPDATE User SET Registered = " + 1 + " WHERE User_Id = " + x + ";");
db.write("UPDATE User SET Active = " + 1 + " WHERE User_Id = " + x + ";");
db.write("UPDATE User SET Managed_By_Id = "+ id +" WHERE User_Id = "+ x + ";");
JOptionPane.showMessageDialog(null, "This user has been registered, and activated. You have been assigned as their manager.", "User Registered", JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null, "That user Id has already been registered.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public void removeFromWorkflow(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Remove user from workflow");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400,400);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setVisible(true);
frame.setVisible(true);
JButton submit = new JButton();
submit.setText("Remove");
submit.setBounds(140,200,120,50);
JLabel userId = new JLabel("User Id");
userId.setBounds(50,10,300,30);
JTextField userIdf = new JTextField();
userIdf.setBounds(50,40,300,30);
panel.add(submit);
panel.add(userId);
panel.add(userIdf);
frame.add(panel);
submit.addActionListener(e -> {
String userIdText = userIdf.getText();
try {
int user_Id = Integer.parseInt(userIdText);
removeFromWorkflow2(user_Id);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter a valid user ID.", "Error", JOptionPane.ERROR_MESSAGE);
}
});
}
public void removeFromWorkflow2(Integer x){
String[] registered = db.read("SELECT * FROM User WHERE User_Id = " + x + " AND Registered = 1;" );
String[] exists = db.read("SELECT * FROM User WHERE User_Id = " + x + ";");
if (exists.length == 0){
JOptionPane.showMessageDialog(null, "That user Id does not exist", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (registered.length == 0){
db.write("DELETE FROM User WHERE User_Id = " + x + ";");
JOptionPane.showMessageDialog(null, "This user has been removed from the signup workflow", "User Registered", JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null, "That user Id has already been registered, if you no longer want their account active you can deactivate it", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public void viewUsers(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("My Users");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(1250,400);
manages = db.read("SELECT User_Id FROM User WHERE Managed_By_Id = " + id + ";");
String[][] rowData = new String[manages.length][10];
for (int i = 0; i < manages.length; i++) {
String[] result = db.read("SELECT Username, User_Type, Email, First_Name, Surname, Gender, DOB, Qualification, Active FROM User WHERE User_Id = " + manages[i] + ";");
rowData[i][0] = manages[i];
rowData[i][1] = result[0];
rowData[i][2] = result[1];
rowData[i][3] = result[2];
rowData[i][4] = result[3];
rowData[i][5] = result[4];
rowData[i][6] = result[5];
rowData[i][7] = result[6];
rowData[i][8] = result[7];
rowData[i][9] = result[8];
}
String[] columnNames = {"User Id", "Username", "User Type", "Email", "First Name", "Surname", "Gender", "Date of Birth", "Qualification", "Active"};
DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
public void activateUser(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Activate User");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400,400);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setVisible(true);
frame.setVisible(true);
JButton submit = new JButton();
submit.setText("Activate");
submit.setBounds(140,200,120,50);
JLabel userId = new JLabel("User Id");
userId.setBounds(50,10,300,30);
JTextField userIdf = new JTextField();
userIdf.setBounds(50,40,300,30);
panel.add(submit);
panel.add(userId);
panel.add(userIdf);
frame.add(panel);
submit.addActionListener(e -> {
String userIdText = userIdf.getText();
try {
int user_Id = Integer.parseInt(userIdText);
activateUser2(user_Id);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter a valid user ID.", "Error", JOptionPane.ERROR_MESSAGE);
}
});
}
public void activateUser2(Integer x){
String[] active = db.read("SELECT * FROM User WHERE User_Id = " + x + " AND Active = 1;" );
String[] exists = db.read("SELECT * FROM User WHERE User_Id = " + x + ";");
if (exists.length == 0){
JOptionPane.showMessageDialog(null, "That user Id does not exist", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (active.length == 0) {
db.write("UPDATE User SET Active = " + 1 + " WHERE User_Id = " + x + ";");
JOptionPane.showMessageDialog(null, "User Id: '" + x + "' has been activated.", "Account Activated", JOptionPane.PLAIN_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "That user Id is already activated", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public void deactivateUser(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Deactivate User");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400,400);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setVisible(true);
frame.setVisible(true);
JButton submit = new JButton();
submit.setText("Deactivate");
submit.setBounds(140,200,120,50);
JLabel userId = new JLabel("User Id");
userId.setBounds(50,10,300,30);
JTextField userIdf = new JTextField();
userIdf.setBounds(50,40,300,30);
panel.add(submit);
panel.add(userId);
panel.add(userIdf);
frame.add(panel);
submit.addActionListener(e -> {
String userIdText = userIdf.getText();
try {
int user_Id = Integer.parseInt(userIdText);
deactivateUser2(user_Id);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter a valid user ID.", "Error", JOptionPane.ERROR_MESSAGE);
}
});
}
public void deactivateUser2(Integer x){
String[] active = db.read("SELECT * FROM User WHERE User_Id = " + x + " AND Active = 0;" );
String[] exists = db.read("SELECT * FROM User WHERE User_Id = " + x + ";");
if (exists.length == 0){
JOptionPane.showMessageDialog(null, "That user Id does not exist", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (active.length == 0) {
db.write("UPDATE User SET Active = " + 0 + " WHERE User_Id = " + x + ";");
JOptionPane.showMessageDialog(null, "User Id: '"+x+"' has been deactivated.", "Account Deactivated", JOptionPane.PLAIN_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "That user Id is already deactivated", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public void passReset(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Reset Password");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400,400);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setVisible(true);
frame.setVisible(true);
JButton submit = new JButton();
submit.setText("Reset");
submit.setBounds(140,200,120,50);
JLabel userId = new JLabel("User Id");
userId.setBounds(50,10,300,30);
JTextField userIdf = new JTextField();
userIdf.setBounds(50,40,300,30);
panel.add(submit);
panel.add(userId);
panel.add(userIdf);
frame.add(panel);
submit.addActionListener(e -> {
String userIdText = userIdf.getText();
try {
int user_Id = Integer.parseInt(userIdText);
passReset2(user_Id);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter a valid user ID.", "Error", JOptionPane.ERROR_MESSAGE);
}
});
}
public void passReset2(Integer x){
String[] exists = db.read("SELECT * FROM User WHERE User_Id = " + x + ";");
if (exists.length == 0){
JOptionPane.showMessageDialog(null, "That user Id does not exist", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
db.write("UPDATE User SET Password = 'temporaryPassword23' WHERE User_Id = " + x + ";");
JOptionPane.showMessageDialog(null, "The user's password has been reset to \"temporaryPassword23\".", "Password changed", JOptionPane.PLAIN_MESSAGE);
}
public void viewModule(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Modules");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(750,200);
String[] modules = db.read("SELECT Module_Id FROM Module;");
String[][] rowData = new String[modules.length][7];
for (int i = 0; i < modules.length; i++) {
String[] result = db.read("SELECT * FROM Module WHERE Module_Id = " + modules[i] + ";");
rowData[i][0] = result[0];
rowData[i][1] = result[1];
rowData[i][2] = result[2];
rowData[i][3] = result[3];
rowData[i][4] = result[4];
rowData[i][5] = result[5];
rowData[i][6] = result[6];
}
String[] columnNames = {"Module Id", "Module Name", "Course Id", "Module Description", "Semester", "Credits", "Lecturer Id"};
DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
public void viewCourse(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Courses");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(625,200);
String[] courses = db.read("SELECT Course_Id FROM Course;");
String[][] rowData = new String[courses.length][5];
for (int i = 0; i < courses.length; i++) {
String[] result = db.read("SELECT * FROM Course WHERE Course_Id = " + courses[i] + ";");
rowData[i][0] = result[0];
rowData[i][1] = result[1];
rowData[i][2] = result[2];
rowData[i][3] = result[3];
rowData[i][4] = result[4];
}
String[] columnNames = {"Course Id", "Course Name", "Course Type", "Department Id", "Course Description"};
DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
public void assignModule(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Assign Module");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400,400);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setVisible(true);
frame.setVisible(true);
JButton submit = new JButton();
submit.setText("Assign");
submit.setBounds(140,200,120,50);
JLabel lid = new JLabel("Lecturer Id");
lid.setBounds(50,10,300,30);
JLabel mid = new JLabel("Module id");
mid.setBounds(50,90,300,30);
JTextField lidf = new JTextField();
lidf.setBounds(50,40,300,30);
JTextField midf = new JTextField();
midf.setBounds(50,120,300,30);
panel.add(lid);
panel.add(lidf);
panel.add(mid);
panel.add(midf);
panel.add(submit);
frame.add(panel);
submit.addActionListener(e -> {
Integer lecturerId = Integer.parseInt(lidf.getText());
Integer moduleId = Integer.parseInt(midf.getText());
try {
assignModule2(lecturerId, moduleId);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter valid numeric values for both ids", "Invalid Input", JOptionPane.ERROR_MESSAGE);
}
});
}
public void assignModule2(Integer lecturer, Integer module){
String[] lecturers = db.read("SELECT Lecturer_Id FROM Module WHERE Module_Id = '"+module+"';");
String[] lexists = db.read("SELECT * FROM User WHERE User_Id = " + lecturer + ";");
if (lexists.length == 0){
JOptionPane.showMessageDialog(null, "That lecturer Id does not exist", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
lexists = db.read("SELECT Lecturer_Id FROM Module;");
if (lexists.length != 0){
JOptionPane.showMessageDialog(null, "That lecturer already has a module", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
String[] mexists = db.read("SELECT * FROM Module WHERE Module_Id = " + module + ";");
if (mexists.length == 0){
JOptionPane.showMessageDialog(null, "That module Id does not exist", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if(lecturers.length == 0){
db.write("UPDATE Module SET Lecturer_Id = " + lecturer + " WHERE Module_Id = " + module + ";");
JOptionPane.showMessageDialog(null, "Lecturer Id: '"+lecturer+"' has been assigned to Module: '"+module+"'.", "Lecturer Assigned", JOptionPane.PLAIN_MESSAGE);
}else{
JOptionPane.showMessageDialog(null, "That module already has a lecturer.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public void addModule(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Add Module");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400,500);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setVisible(true);
frame.setVisible(true);
JButton submit = new JButton();
submit.setText("Add Module");
submit.setBounds(140,400,120,50);
JLabel name = new JLabel("Module Name");
name.setBounds(50,10,300,30);
JLabel description = new JLabel("Module description");
description.setBounds(50,90,300,30);
JLabel credits = new JLabel("Credits");
credits.setBounds(50,170,300,30);
JLabel id = new JLabel("Course Id");
id.setBounds(50,250,300,30);
JTextField namef = new JTextField();
namef.setBounds(50,40,300,30);
JTextField descriptionf = new JTextField();
descriptionf.setBounds(50,120,300,30);
JRadioButton credits10 = new JRadioButton();
credits10.setText("10 Credits");
credits10.setBounds(50,200,100,30);
JRadioButton credits20 = new JRadioButton();
credits20.setText("20 Credits");
credits20.setBounds(150,200,100,30);
JTextField idf = new JTextField();
idf.setBounds(50,280,300,30);
panel.add(submit);
panel.add(name);
panel.add(namef);
panel.add(description);
panel.add(descriptionf);
panel.add(credits10);
panel.add(credits20);
panel.add(id);
panel.add(idf);
frame.add(panel);
ButtonGroup BG1 = new ButtonGroup();
BG1.add(credits10);
BG1.add(credits20);
submit.addActionListener(e -> {
String courseIdText = idf.getText();
try {
int courseId = Integer.parseInt(courseIdText);
addModule2(namef.getText(), descriptionf.getText(), credits10, credits20, courseId);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter a valid course ID.", "Error", JOptionPane.ERROR_MESSAGE);
}
});
}
public void addModule2(String name, String desc, JRadioButton c10, JRadioButton c20, int id){
Driver d = new Driver();
String[] exists = db.read("SELECT * FROM Course WHERE Course_Id = " + id + ";");
if (exists.length == 0){
JOptionPane.showMessageDialog(null, "That course Id does not exist", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
exists = db.read("SELECT Module_Name FROM Module WHERE Module_Name = '" + name + "';");
if (exists.length != 0){
JOptionPane.showMessageDialog(null, "That module name already exists", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
boolean error = d.valid_length(name,"module name",30);
if (error) {
return;
}
error = d.valid_length(desc,"module description",200);
if (error) {
return;
}
if (c10.isSelected()) {
db.write("INSERT INTO Module (Module_Name, Module_Description, Credits) VALUES ('" + name + "', '" + desc + "', 10);");
JOptionPane.showMessageDialog(null, "Module '"+name+"' has been added.", "Module Added", JOptionPane.PLAIN_MESSAGE);
String[] module = db.read("SELECT Module_Id FROM Module WHERE Module_Name = '" + name + "';");
assignCourse(Integer.parseInt(module[0]), id);
} else if (c20.isSelected()) {
db.write("INSERT INTO Module (Module_Name, Module_Description, Credits) VALUES ('" + name + "', '" + desc + "', 20);");
JOptionPane.showMessageDialog(null, "Module '"+name+"' has been added.", "Module Added", JOptionPane.PLAIN_MESSAGE);
String[] module = db.read("SELECT Module_Id FROM Module WHERE Module_Name = '" + name + "';");
assignCourse(Integer.parseInt(module[0]), id);
} else {
JOptionPane.showMessageDialog(null, "Please select a value for credits", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public void assignCourse(Integer module, Integer course){
db.write("UPDATE Module SET Course_Id = " + course + " WHERE Module_Id = " + module + ";");
String[] rules = db.read("SELECT Rule_Id FROM Course_Rules WHERE Course_Id = '"+course+"';");
for(int i = 0; i < rules.length; i++){
db.write("INSERT INTO Module_Rules (Module_Id, Rule_Id) VALUES ('"+module+"', '"+rules[i]+"');");
}
JOptionPane.showMessageDialog(null, "Module '"+module+"' has been assigned to Course '"+course+"'.", "Course Added", JOptionPane.PLAIN_MESSAGE);
}
public void addCourse(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Add Course");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400,500);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setVisible(true);
frame.setVisible(true);
JButton submit = new JButton();
submit.setText("Add Course");
submit.setBounds(140,400,120,50);
JLabel name = new JLabel("Course Name");
name.setBounds(50,10,300,30);
JLabel description = new JLabel("Course description");
description.setBounds(50,90,300,30);
JLabel id = new JLabel("Department Id");
id.setBounds(50,170,300,30);
JLabel type = new JLabel("Course type");
type.setBounds(50,250,300,30);
JTextField namef = new JTextField();
namef.setBounds(50,40,300,30);
JTextField descriptionf = new JTextField();
descriptionf.setBounds(50,120,300,30);
JTextField idf = new JTextField();
idf.setBounds(50,200,300,30);
JRadioButton under = new JRadioButton();
under.setText("Undergraduate");
under.setBounds(50,280,125,30);
JRadioButton post = new JRadioButton();
post.setText("Postgraduate");
post.setBounds(200,280,125,30);
panel.add(submit);
panel.add(name);
panel.add(namef);
panel.add(description);
panel.add(descriptionf);
panel.add(id);
panel.add(idf);
panel.add(type);
panel.add(under);
panel.add(post);
frame.add(panel);
ButtonGroup BG1 = new ButtonGroup();
BG1.add(under);
BG1.add(post);
submit.addActionListener(e -> {
String deptIdText = idf.getText();
try {
int deptId = Integer.parseInt(deptIdText);
addCourse2(namef.getText(), descriptionf.getText(), deptId, under, post);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter a valid department ID.", "Error", JOptionPane.ERROR_MESSAGE);
}
});
}
public void addCourse2(String name, String desc, int id, JRadioButton under, JRadioButton post){
Driver d = new Driver();
String[] exists = db.read("SELECT * FROM Department WHERE Department_Id = " + id + ";");
if (exists.length == 0){
JOptionPane.showMessageDialog(null, "That department Id does not exist", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
boolean error = d.valid_length(name,"course name",30);
if (error) {
return;
}
error = d.valid_length(desc,"course description",200);
if (error) {
return;
}
if (under.isSelected()) {
db.write("INSERT INTO Course (Course_Name, Course_Description, Department_Id, Course_Type) VALUES ('" + name + "', '" + desc + "', " + id + ", 'Undergraduate');");
JOptionPane.showMessageDialog(null, "Course '"+name+"' has been added.", "Course Added", JOptionPane.PLAIN_MESSAGE);
} else if (post.isSelected()) {
db.write("INSERT INTO Course (Course_Name, Course_Description, Department_Id, Course_Type) VALUES ('" + name + "', '" + desc + "', " + id + ", 'Postgraduate');");
JOptionPane.showMessageDialog(null, "Course '"+name+"' has been added.", "Course Added", JOptionPane.PLAIN_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Please select an option for the course type", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public void updateCourse(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Update Course");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400,400);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setVisible(true);
frame.setVisible(true);
JButton submit = new JButton();
submit.setText("Update Course");
submit.setBounds(140,300,120,50);
JLabel id = new JLabel("Course Id");
id.setBounds(50,10,300,30);
JLabel description = new JLabel("Course description");
description.setBounds(50,90,300,30);
JTextField idf = new JTextField();
idf.setBounds(50,40,300,30);
JTextField descriptionf = new JTextField();
descriptionf.setBounds(50,120,300,30);
panel.add(submit);
panel.add(id);
panel.add(idf);
panel.add(description);
panel.add(descriptionf);
frame.add(panel);
submit.addActionListener(e -> {
String courseIdText = idf.getText();
try {
int courseId = Integer.parseInt(courseIdText);
updateCourse2(descriptionf.getText(), courseId);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter a valid department ID.", "Error", JOptionPane.ERROR_MESSAGE);
}
});
}
public void updateCourse2(String desc, Integer x){
Driver d = new Driver();
String[] exists = db.read("SELECT * FROM Course WHERE Course_Id = " + x + ";");
if (exists.length == 0){
JOptionPane.showMessageDialog(null, "That course Id does not exist", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
boolean error = d.valid_length(desc,"course description",200);
if (error) {
return;
}
db.write("UPDATE Course SET Course_Description = '" + desc + "' WHERE Course_Id = " + x + ";");
JOptionPane.showMessageDialog(null, "Course '"+x+"' has been updated'.", "Description Updated", JOptionPane.PLAIN_MESSAGE);
}
public void enrollStudent(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Enroll Student");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400,400);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setVisible(true);
frame.setVisible(true);
JButton submit = new JButton();
submit.setText("Enroll");
submit.setBounds(140,200,120,50);
JLabel sid = new JLabel("Student Id");
sid.setBounds(50,10,300,30);
JLabel cid = new JLabel("Course id");
cid.setBounds(50,90,300,30);
JTextField sidf = new JTextField();
sidf.setBounds(50,40,300,30);
JTextField cidf = new JTextField();
cidf.setBounds(50,120,300,30);
panel.add(sid);
panel.add(sidf);
panel.add(cid);
panel.add(cidf);
panel.add(submit);
frame.add(panel);
submit.addActionListener(e -> {
Integer studentId = Integer.parseInt(sidf.getText());
Integer courseId = Integer.parseInt(cidf.getText());
try {
enrollStudent2(studentId, courseId);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Please enter valid numeric values for both ids", "Invalid Input", JOptionPane.ERROR_MESSAGE);
}
});
}
public void enrollStudent2(Integer student, Integer course){
String[] exists = db.read("SELECT * FROM User WHERE User_Id = " + student + " AND User_type = 'Student';");
if(exists.length == 0) {
JOptionPane.showMessageDialog(null, "Please enter a valid Student Id", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
exists = db.read("SELECT * FROM Course WHERE Course_Id = " + course + ";");
if (exists.length == 0){
JOptionPane.showMessageDialog(null, "That course Id does not exist", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
//loop for each module in a course
String[] modules = db.read("SELECT Module_Id FROM Module WHERE Course_Id = "+ course+ ";");
for(int i = 0; i < modules.length; i++){
//find existing attempts for this module, if any
Integer atts = 0;
String[] attempts = db.read("SELECT Student_Attempt_No FROM Student_Module WHERE Student_Id = "+student+" AND Module_Id = "+modules[i]+";");
//what rules apply to this module?
String[] rules = db.read("SELECT Rule_Id FROM Module_Rules WHERE Module_Id = '"+modules[i]+"';");
Boolean attemptsRule = false;
Boolean attendanceRule = false;
for(int j = 0; j < rules.length; j++){
if(Integer.parseInt(rules[j]) == 1){
attemptsRule = true;
}else if(Integer.parseInt(rules[j]) == 2){
attendanceRule = true;
}
}
//check if module is full (when rule is applied)
if(attendanceRule){
String[] students = db.read("SELECT Student_Id FROM Student_Module WHERE Module_Id = '"+modules[i]+"';");
if(students.length > 29){
JOptionPane.showMessageDialog(null, "Module "+modules[i]+" is full. This student cannot be enrolled.", "Error", JOptionPane.ERROR_MESSAGE);
//N.B. if a student fails this check, and is not enrolled, they have been enrolled into any modules before, but not any after.
return;
}
}
//Update attempts, where it is allowed
if(attempts.length == 0){
atts = 1;
}else if(Integer.parseInt(attempts[0]) == 2 && attemptsRule){
JOptionPane.showMessageDialog(null, "This student has exceeded the maximum allowed attempts for module "+modules[i]+".", "Error", JOptionPane.ERROR_MESSAGE);
//N.B. if a student fails this check, and is not enrolled, they have been enrolled into any modules before, but not any after.
return;
}else{
atts = Integer.parseInt(attempts[0]) + 1;
}
//add student to this module
exists = db.read("SELECT * FROM Student_Module WHERE Module_Id = " + modules[i] + " AND Student_Id = " + student + ";");
if (exists.length == 0){
db.write("INSERT INTO Student_Module (Module_Id, Student_Id, Student_Attempt_No) VALUES (" + modules[i] + ", " + student + ", " + atts + ");");
} else {
db.write("UPDATE Student_Module SET Student_Attempt_No = " + atts + ",Exam_Mark = NULL , Lab_Mark = NULL, Decision = NULL WHERE Module_Id = " + modules[i] + " AND Student_Id = " + student + ";");
}
}
//Success message, only when student is enrolled into EVERY module for a course.
String[] courseName = db.read("SELECT Course_Name FROM Course WHERE Course_Id = '"+course+"';");
JOptionPane.showMessageDialog(null, "Success!\n Student "+student+"has been added to modules in course"+courseName[0]+".", "Success", JOptionPane.PLAIN_MESSAGE);
}
public void getResults(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Student Results");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(750,400);
String[] students = db.read("SELECT DISTINCT sm.Student_Id FROM Student_Module sm INNER JOIN User u ON sm.Student_Id = u.User_Id WHERE u.Managed_By_Id = " + id + ";");
int counter = 0;
int resultlength = 0;
for (int i = 0; i < students.length; i++) {
String[] modules = db.read("SELECT Module_Id FROM Student_Module WHERE Student_Id = " + students[i] + ";");
for (int j = 0; j < modules.length; j++) {
resultlength++;
}
}
String[][] rowData = new String[resultlength][6];
for (int i = 0; i < students.length; i++) {
String[] modules = db.read("SELECT Module_Id FROM Student_Module WHERE Student_Id = " + students[i] + ";");
for (int j = 0; j < modules.length; j++) {
String[] result = db.read("SELECT * FROM Student_Module WHERE Student_Id = " + students[i] + " AND Module_Id = " + modules[j] + ";");
rowData[counter][0] = result[0];
rowData[counter][1] = result[1];
rowData[counter][2] = result[2];
rowData[counter][3] = result[3];
rowData[counter][4] = result[4];
rowData[counter][5] = result[5];
counter++;
}
}
String[] columnNames = {"Module Id", "Student Id", "Student Attempt No", "Exam Mark", "Lab Mark", "Decision"};
DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
public void setDecision(){
ImageIcon image = new ImageIcon("logo.jpg");
JFrame frame = new JFrame();
frame.setTitle("Student Decision");
frame.setIconImage(image.getImage());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(400,400);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setVisible(true);
frame.setVisible(true);
JButton submit = new JButton();
submit.setText("Set Decision");
submit.setBounds(140,300,120,50);
JLabel sid = new JLabel("Student Id");
sid.setBounds(50,10,300,30);
JLabel mid = new JLabel("Module Id");
mid.setBounds(50,90,300,30);
JLabel dec = new JLabel("Decision");
dec.setBounds(50,170,300,30);
JTextField sidf = new JTextField();
sidf.setBounds(50,40,300,30);
JTextField midf = new JTextField();
midf.setBounds(50,120,300,30);