-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLMS.java
More file actions
3645 lines (3026 loc) · 127 KB
/
Copy pathLMS.java
File metadata and controls
3645 lines (3026 loc) · 127 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 required libraries
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.io.*;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class LMS {
static Scanner input=new Scanner(System.in); // Create universal input object
static String[][] books=new String[10000][6]; // Create books array to store books data
//0 index for title
//1 index for author
//2 index for genre
//3 index for Book Number
//4 index for borrowed_by
//5 index for due_date
static String[][] members=new String[10000][6]; // Create members array to store members data
//0 index for Member ID
//1 index for Name
//2 index for CNIC
//3 index for Phone
//4 index for encryptedPassword
//5 index for late fees
static String[][] admin=new String[20][5]; // Create admin array to store admins data
//0 index for Admin ID
//1 index for Name
//2 index for CNIC
//3 index for Phone
//4 index for encryptedPassword
static String[][] systemAdmin=new String[2][5]; // Create systemAdmin array to store System Admin data
//0 index for Admin ID
//1 index for Name
//2 index for CNIC
//3 index for Phone
//4 index for encryptedPassword
// global variables to store values for current logged in Member, Admin and System Admin
static String loggedInMemberID;
static String loggedInMemberPassword;
static int memberExistanceIndex;
static String loggedInAdminID;
static String loggedInAdminPassword;
static int adminExistanceIndex;
static String loggedInSysAdminID;
static String loggedInSysAdminPassword;
static int sysAdminExistanceIndex = 0;
// Create a default System Admin and save it's details in arrays and file
static{
systemAdmin[0][0]="1";
systemAdmin[0][1]="Jeff";
systemAdmin[0][2]="61101-8574331-0";
systemAdmin[0][3]="0335-1234576";
systemAdmin[0][4]=EncryptPassword("sysAdmin","library101","1");
saveSysAdmins();
}
// load everything at startup
static {
MemberLateFeesInitialization();
try {
loadMembers();
loadAdmins();
loadSysAdmins();
loadBooks();
} catch (Exception e) {
System.err.println("ERROR loading data files: " + e.getMessage());
}
}
// Load members by creating a new file (members.txt)
public static void loadMembers() {
File file = new File("members.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("ERROR creating members.txt file.");
return;
}
}
// Read its data and store in arrays
try (Scanner reader = new Scanner(file)) {
int i = 0;
while (reader.hasNextLine() && i < members.length) {
String line = reader.nextLine().trim();
if (line.isEmpty()) continue; // Skip line if it's empty
String[] data = line.split(" ");
for (int j = 0; j < data.length && j < members[0].length; j++) {
members[i][j] = data[j];
if(j == 1){
// Change space between each name of user with "_" for better readability
members[i][j] = members[i][j].replace("_", " ") ;
}
}
i++;
}
} catch (IOException e) {
System.out.println("ERROR reading members.txt file.");
}
saveMembers();
}
// Save the updated array's data in members.txt
public static void saveMembers() {
try (PrintWriter pw = new PrintWriter(new FileOutputStream("members.txt", false))) {
int n = NotEmpty(members);
for (int i = 0; i < n; i++) {
members[i][1] = members[i][1].replace(" ", "_") ;
pw.printf("%s %s %s %s %s %s%n",
members[i][0], members[i][1], members[i][2],
members[i][3], members[i][4], members[i][5]
);
}
} catch (IOException e) {
System.err.println("ERROR saving members: " + e.getMessage());
}
}
public static void loadAdmins() {
File file = new File("admins.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("ERROR creating admins.txt file.");
return;
}
}
try (Scanner reader = new Scanner(file)) {
int i = 0;
while (reader.hasNextLine() && i < admin.length) {
String line = reader.nextLine().trim();
if (line.isEmpty()) continue;
String[] data = line.split(" ");
for (int j = 0; j < data.length && j < admin[0].length; j++) {
admin[i][j] = data[j];
if(j == 1){
admin[i][j] = admin[i][j].replace("_", " ") ;
}
}
i++;
}
} catch (FileNotFoundException e) {
System.out.println("ERROR loading admin data: " + e.getMessage());
}
saveAdmins();
}
public static void saveAdmins(){
try (PrintWriter pw = new PrintWriter(new FileOutputStream("admins.txt", false))) {
int n = NotEmpty(admin);
for (int i = 0; i < n; i++) {
admin[i][1] = admin[i][1].replace(" ", "_") ;
pw.printf("%s %s %s %s %s%n",
admin[i][0], admin[i][1], admin[i][2],
admin[i][3], admin[i][4]
);
}
} catch (IOException e) {
System.err.println("ERROR saving admins: " + e.getMessage());
}
}
public static void loadSysAdmins() {
File file = new File("sysAdmins.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("ERROR creating sysAdmins.txt file.");
return;
}
}
try (Scanner reader = new Scanner(file)) {
int i = 0;
while (reader.hasNextLine() && i < systemAdmin.length) {
String line = reader.nextLine().trim();
if (line.isEmpty()) continue;
String[] data = line.split(" ");
for (int j = 0; j < data.length && j < systemAdmin[0].length; j++) {
systemAdmin[i][j] = data[j];
if(j == 1){
systemAdmin[i][j] = systemAdmin[i][j].replace("_", " ") ;
}
}
i++;
}
} catch (FileNotFoundException e) {
System.out.println("ERROR loading system system admin data: " + e.getMessage());
}
}
public static void saveSysAdmins() {
try (PrintWriter pw = new PrintWriter(new FileOutputStream("sysAdmins.txt", false))) {
int n = NotEmpty(systemAdmin);
for (int i = 0; i < n; i++) {
systemAdmin[i][1] = systemAdmin[i][1].replace(" ", "_") ;
pw.printf("%s %s %s %s %s%n",
systemAdmin[i][0], systemAdmin[i][1], systemAdmin[i][2],
systemAdmin[i][3], systemAdmin[i][4]
);
}
} catch (IOException e) {
System.err.println("ERROR saving sysAdmins: " + e.getMessage());
}
}
public static void loadBooks() {
File file = new File("books.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("ERROR creating books.txt file.");
return;
}
}
try (Scanner reader = new Scanner(file)) {
int i = 0;
while (reader.hasNextLine() && i < books.length) {
String line = reader.nextLine().trim();
if (line.isEmpty()) continue;
String[] data = line.split(" ");
for (int j = 0; j < data.length && j < books[0].length; j++) {
books[i][j] = data[j];
if(j == 0){
books[i][j] = books[i][j].replace("_", " ") ;
}
if (j == 1){
books[i][j] = books[i][j].replace("_", " ") ;
}
}
i++;
}
} catch (IOException e) {
System.out.println("ERROR reading books.txt file.");
}
}
public static void saveBooks() {
try (PrintWriter pw = new PrintWriter(new FileOutputStream("books.txt", false))) {
int n = NotEmpty(books);
for (int i = 0; i < n; i++) {
books[i][0] = books[i][0].replace(" ", "_");
books[i][1] = books[i][1].replace(" ", "_");
pw.printf("%s %s %s %s %s %s%n",
books[i][0], books[i][1], books[i][2],
books[i][3], books[i][4], books[i][5]
);
}
} catch (IOException e) {
System.err.println("ERROR saving books: " + e.getMessage());
}
}
// main() function to run everything (main console/first menu)
public static void main(String[] args) {
// Run the management system until user wants to terminate the program
while(true) {
System.out.println("\n-------------------------------------------------------------");
System.out.println(" Library Management System ");
System.out.println("-------------------------------------------------------------");
System.out.println("1. Member Login");
System.out.println("2. Admin Login");
System.out.println("3. System Administrator Login");
System.out.println("4. Exit Program");
// Run the loop until user enters correct "choice" integer
while (true) {
System.out.print("\nEnter Your Choice(1-4): ");
// Handle exception due to Miss Match Input by the user
try {
int choice = input.nextInt();
input.nextLine();
switch (choice) {
case 1:
if (!(Confirmation())) {
break;
}
if (MemberLogin()) {
System.out.println("\nLogged In Successfully");
System.out.println("Redirecting to the console......");
delay(1500);
StudentConsole();
} else {
System.out.println("Invalid Credentials!");
delay(1500);
}
break;
case 2:
if (!(Confirmation())) {
break;
}
if (AdminLogin()) {
System.out.println("\nLogged In Successfully");
System.out.println("Redirecting to the console......");
delay(1500);
AdminConsole();
} else {
System.out.println("Invalid Credentials!");
delay(1500);
}
break;
case 3:
if (!(Confirmation())) {
break;
}
if (SysAdminLogin()) {
System.out.println("\nLogged In Successfully");
System.out.println("Redirecting to the console......");
delay(1500);
SysAdminConsole();
} else {
System.out.println("Invalid Credentials!");
delay(1500);
}
break;
case 4:
System.out.println("Terminating the Program!");
delay(1500);
return;
default:
System.out.println("ERROR! Enter correct choice");
delay(1500);
break;
}
break;
} catch (InputMismatchException ex) { // Catch the exception
System.out.println("\nERROR! Enter correct choice");
input.nextLine(); // Refresh the buffer
}
}
}
}
// A method to add a new System Admin
public static void AddSystemAdmin() {
loadSysAdmins(); //Load current System Admin data
line();
// If System Admin array limit reached, then increase the limit
if (NotEmpty(systemAdmin) == systemAdmin.length) {
System.out.println("System Admin Limit Reached: Increase Limit In Source Code!");
return;
}
// NotEmpty() will return length of "systemAdmin" array and store it as an index
int index = NotEmpty(systemAdmin);
// Run Loop until user enters correct information for new System Admin
while(true) {
// Handle exception
try {
// Enter new Admin details
System.out.print("Enter First Name: ");
String firstName = input.nextLine();
System.out.print("Enter Last Name: ");
String lastName = input.nextLine();
System.out.print("Enter Mobile Phone Number (Format: 03xx-xxxxxxx): ");
String num = input.nextLine();
System.out.print("Enter CNIC (Format: xxxxx-xxxxxxx-x): ");
String cnic = input.nextLine();
// Trim any whitespaces (if exist)
firstName = firstName.trim();
lastName = lastName.trim();
num = num.trim();
cnic = cnic.trim();
// If any data is empty, return to the main menu
if (firstName.isEmpty() || lastName.isEmpty() || num.isEmpty() || cnic.isEmpty()) {
System.out.println("All fields (Name, Mobile Phone Number, CNIC) are Required!");
return;
}
if (!ValidateCNIC(cnic)) {
System.out.println("ERROR! Invalid CNIC!");
return;
}
if (!ValidatePhone(num)) {
System.out.println("ERROR! Invalid Mobile Number!");
return;
}
// The index is -1 from id
String id = Integer.toString(index + 1);
// Default Password
String defaultPassword = "Sys123admin123";
// Encrypt Password
String encryptedPassword = EncryptPassword("sysAdmin", defaultPassword, id);
// Add new System Admin data in "systemAdmin" array
systemAdmin[index][0] = id;
systemAdmin[index][1] = lastName + "," + firstName;
systemAdmin[index][2] = cnic;
systemAdmin[index][3] = num;
systemAdmin[index][4] = encryptedPassword;
// Print new System Admin data
System.out.println("\nSystem Admin ID: " + id);
System.out.println("Password: " + defaultPassword);
System.out.println("\nSystem Admin Has Been Added!");
// Update sysAdmins.txt file with new System Admins data
UpdateFile("sysAdmin", "yes", id);
line();
break; // Break the loop if all the data is successfully saved
} catch (InputMismatchException ex) { // Handle exception
System.out.println("\nError! Enter correct information");
input.nextLine(); // Refresh the buffer
}
}
}
// Create a function to add a new Admin
public static void AddAdmin(){
loadAdmins(); //Load current Admin data
line();
// If Admin array limit reached, then increase the limit
if(NotEmpty(admin) == admin.length){
System.out.println("Admin Limit Reached: Increase Limit In Source Code!");
return;
}
// NotEmpty() will return length of "admin" array and store it as an index
int index = NotEmpty(admin);
// Run Loop until user enters correct information for new Admin
while(true) {
try { // Handle exception
// Enter new Admin details
System.out.print("Enter First Name: ");
String firstName = input.nextLine();
System.out.print("Enter Last Name: ");
String lastName = input.nextLine();
System.out.print("Enter Mobile Phone Number (Format: 03xx-xxxxxxx): ");
String num = input.nextLine();
System.out.print("Enter CNIC (Format: xxxxx-xxxxxxx-x): ");
// Trim any whitespaces (if exist)
String cnic = input.nextLine();
firstName = firstName.trim();
lastName = lastName.trim();
num = num.trim();
cnic = cnic.trim();
// If any data is empty, return to the main menu
if (firstName.isEmpty() || lastName.isEmpty() || num.isEmpty() || cnic.isEmpty()) {
System.out.println("All fields (Name,Mobile Phone Number,CNIC) are Required!");
return;
}
if (!ValidateCNIC(cnic)) {
System.out.println("ERROR! Invalid CNIC!");
return;
}
if (!ValidatePhone(num)) {
System.out.println("ERROR! Invalid Mobile Number!");
return;
}
// Create a unique random id for a new admin
String id;
while (true) {
int check = 0;
id = Integer.toString((int) (1 + Math.random() * 20));
for (int i = 0; i < index; i++) {
if (admin[i][0].equalsIgnoreCase(id)) {
check = 1;
break;
}
}
if (check == 0) {
break;
}
}
//Create a default password for a new admin
String defaultPassword = "admin123";
String encryptedPassword = EncryptPassword("admin", defaultPassword, id);
// Add new Admin data in "Admin" array
admin[index][0] = id;
admin[index][1] = lastName + "," + firstName;
admin[index][2] = cnic;
admin[index][3] = num;
admin[index][4] = encryptedPassword;
// Print new Admin data
System.out.println("\nAdmin ID: " + id);
System.out.println("Password: " + defaultPassword);
System.out.println("\nAdmin Has Been Added!");
// Update admins.txt file with new admin data
UpdateFile("admin", "yes", id);
line();
break; // Break the loop if all the data is successfully saved
} catch (InputMismatchException ex) { // Catch the exception
System.out.println("\nERROR! Enter correct information");
input.nextLine(); // Refresh the buffer
}
}
}
// Create a function to remove an Admin
public static void RemoveAdmin() {
line();
// Check Admin array if its empty
if(NotEmpty(admin) == 0){
System.out.println("The Database is Already Empty");
return;
}
// Run Loop until user enters correct information for an Admin
while(true) {
try { // Handle the exception
System.out.print("Enter Admin ID: ");
String id = input.nextLine();
id = id.trim(); // Remove any whitespaces
try {
int idNum = Integer.parseInt(id);
if (idNum <= 0 || idNum > admin.length) {
System.out.println("ERROR! Invalid ID!");
}
} catch (NumberFormatException e) {
System.out.println("ERROR! Invalid ID format!");
}
int check = 0; // Check if id exists
int index = 0; // Find the index of ID
for (int i = 0; i < NotEmpty(admin); i++) {
if (admin[i][0].equals(id)) {
check = 1;
index = i;
break;
}
}
// Print message if id does not exist
if (check == 0) {
System.out.println("\nAdmin ID " + id + " Does Not Exist In The Database");
return;
}
// Print removed admin data
System.out.printf("\n%-15s %-25s %-20s %-20s", "ADMIN ID", "NAME", "CNIC", "PHONE");
line(); // Call a function to print some lines for formatting
System.out.printf("%-15s %-25s %-20s %-20s\n", admin[index][0], admin[index][1], admin[index][2], admin[index][3]);
//Ask for confirmation
if (!Confirmation()) {
System.out.println("\nAdmin Removal Operation Cancelled!");
return;
}
NullifyRow(index, admin); // Put null values in removed admin indexes
Normalize(index, admin); // Normalize the data for removed admin indexes
System.out.println("\nAdmin Removal Operation Successful!");
// Update the admins.txt file by removing admins data
UpdateFile("admin", null, id);
line();
break; // Break the loop if all the data is successfully saved
} catch (InputMismatchException ex) { //Catch the exception
System.out.println("\nERROR! Enter correct details");
input.nextLine(); // Refresh the buffer
}
}
}
// Create a function to edit Admin Information
public static void EditAdminInfo(){
loadAdmins(); // Load Data
line();
// Check if admin array is empty or not
if(NotEmpty(admin)==0){
System.out.println("The Database is Empty.");
return;
}
// Run Loop until user enters correct information for Admin
while(true) {
try {
System.out.print("Enter Admin ID: ");
String id = input.nextLine();
id = id.trim();
// Check if ID exist
boolean found = false;
for (int i = 0; i < NotEmpty(admin); i++) {
if (admin[i][0].equals(id)) {
found = true;
break;
}
}
if (!found) {
System.out.println("ERRRO! ID not found!");
return;
}
// Print data
System.out.printf("\n%-15s %-25s %-20s %-20s", "ADMIN ID", "NAME", "CNIC", "PHONE");
line(); // For formatting
int adminIndex = 0;
int check = 0;
for (int i = 0; i < NotEmpty(admin); i++) {
if ((admin[i][0]).equalsIgnoreCase(id)) {
check = 1;
adminIndex = i;
System.out.printf("%-15s %-25s %-20s %-20s\n", admin[i][0], admin[i][1], admin[i][2], admin[i][3]);
}
}
if (check == 0) {
System.out.println("Admin ID \"" + id + "\" Does Not Exist In The Database");
return;
}
if (!(Confirmation())) {
System.out.println("Operation Cancelled!");
return;
}
System.out.println("\nWrite Updated Info For Concerned Admin.");
System.out.println("\nPress Enter To Leave Info For That Field As Is, Anything Entered will be Stored In that Field If Appropriate.\n");
System.out.print("Enter Updated Admin First Name (Enter Current First Name If Last Name Is To Be Updated): ");
String firstName = input.nextLine();
firstName = firstName.trim();
System.out.print("Enter Updated Admin Last Name (Enter Current Last Name If First Name Is Being Updated): ");
String lastName = input.nextLine();
lastName = lastName.trim();
if (!(firstName.isEmpty()) || !(lastName.isEmpty())) {
admin[adminIndex][1] = lastName + "," + firstName;
System.out.println("Admin Name Updated!");
} else {
System.out.println("Admin Name Retained!");
}
System.out.print("Enter Updated CNIC (Format: xxxxx-xxxxxxx-x): ");
String cnic = input.nextLine();
cnic = cnic.trim();
if (!(cnic.isEmpty())) {
if (!(ValidateCNIC(cnic))) {
System.out.println("ERROR! Invalid CNIC!");
return;
}
admin[adminIndex][2] = cnic;
System.out.println("Admin CNIC Updated!");
} else {
System.out.println("Admin CNIC Retained!");
}
System.out.print("Enter Updated Mobile Phone Number (Format: 03xx-xxxxxxx): ");
String num = input.nextLine();
num = num.trim();
if (!(num.isEmpty())) {
if (!(ValidatePhone(num))) {
System.out.println("ERROR! Invalid Phone Number!");
return;
}
admin[adminIndex][3] = num;
System.out.println("Admin Phone Number Updated!");
} else {
System.out.println("Admin Phone Number Retained!");
}
System.out.println("\nInformation Updated!\n");
// Update the admins.txt file by removing admins data
UpdateFile("admin", "yes", id);
line();
break; // Break the loop if all the data is successfully saved
} catch (InputMismatchException ex) { // Catch exception
System.out.println("\nERROR! Enter correct details");
input.nextLine(); // Refresh Buffer
}
}
}
// Create method to edit System Admin Info
public static void EditSysAdminInfo(){
loadSysAdmins(); // Load Data
line();
if(NotEmpty(systemAdmin)==0){
System.out.println("The Database is Empty.");
return;
}
while (true) {
try {
System.out.print("Enter System Administrator ID: ");
String id = input.nextLine();
id = id.trim();
boolean found = false;
for (int i = 0; i < NotEmpty(systemAdmin); i++) {
if (systemAdmin[i][0].equals(id)) {
found = true;
break;
}
}
if (!found) {
System.out.println("ERROR! ID not found!");
return;
}
System.out.print("Enter Password: ");
String pass = input.nextLine();
pass = pass.trim();
int sysAdminIndex = 0;
int check = 0;
// Check if ID exists
for (int i = 0; i < NotEmpty(systemAdmin); i++) {
if (systemAdmin[i][0].equals(id)) {
if (!(pass.equals(DecryptPassword("systemAdmin", systemAdmin[i][4], id)))) {
System.out.println("Invalid Credentials!");
return;
}
sysAdminIndex = i;
check = 1;
}
}
if (check == 0) {
System.out.println("System Administrator ID \"" + id + "\" Does Not Exist In The Database");
return;
}
System.out.printf("\n%-20s %-25s %-20s %-20s", "SYSTEM ADMIN ID", "NAME", "CNIC", "PHONE");
line(); // For Formatting
//Print System Admin data
System.out.printf("%-20s %-25s %-20s %-20s\n", systemAdmin[sysAdminIndex][0], systemAdmin[sysAdminIndex][1], systemAdmin[sysAdminIndex][2], systemAdmin[sysAdminIndex][3]);
if (!(Confirmation())) {
System.out.println("Operation Cancelled!");
return;
}
System.out.println("\nWrite Updated Info For Concerned System Administrator.");
System.out.println("\nPress Enter To Leave Info For That Field As Is, Anything Entered will be Stored In that Field If Appropriate.\n");
// Get required inputs
System.out.print("Enter Updated System Admin First Name (Enter Current First Name If Last Name Is To Be Updated): ");
String firstName = input.nextLine();
firstName = firstName.trim();
System.out.print("Enter Updated System Admin Last Name (Enter Current Last Name If First Name Is Being Updated): ");
String lastName = input.nextLine();
lastName = lastName.trim();
if (!(firstName.isEmpty()) || !(lastName.isEmpty())) {
systemAdmin[sysAdminIndex][1] = lastName + "," + firstName;
System.out.println("System Admin Name Updated!");
} else {
System.out.println("System Admin Name Retained!");
}
System.out.print("Enter Updated CNIC (Format: xxxxx-xxxxxxx-x): ");
String cnic = input.nextLine();
cnic = cnic.trim();
if (!(cnic.isEmpty())) {
if (!(ValidateCNIC(cnic))) {
System.out.println("Invalid CNIC!");
return;
}
systemAdmin[sysAdminIndex][2] = cnic;
System.out.println("System Administrator CNIC Updated!");
} else {
System.out.println("System Administrator CNIC Retained!");
}
System.out.print("Enter Updated Mobile Phone Number (Format: 03xx-xxxxxxx): ");
String num = input.nextLine();
num = num.trim();
if (!(num.isEmpty())) {
if (!(ValidatePhone(num))) {
System.out.println("ERROR! Invalid Phone Number!");
return;
}
systemAdmin[sysAdminIndex][3] = num;
System.out.println("System Administrator Phone Number Updated!");
} else {
System.out.println("System Administrator Phone Number Retained!");
}
System.out.println("\nInformation Updated!\n");
// Update the sysAdmins.txt file by changing system admins data
UpdateFile("sysAdmin", "yes", id);
line();
break; // Break the loop if all the data is successfully saved
} catch (InputMismatchException ex) {
System.out.println("\nERROR! Enter correct information");
input.nextLine(); // Refresh buffer
}
}
}
// Create a function to edit member Info
public static void EditMemberInfo(){
loadMembers(); // Load data
line();
if(NotEmpty(members)==0){
System.out.println("The Database is Empty.");
return;
}
// Run Loop until use enters correct data
while (true) {
try {
System.out.print("Enter Member ID: ");
String id = input.nextLine();
id = id.trim();
boolean found = false;
for (int i = 0; i < NotEmpty(members); i++) {
if (members[i][0].equals(id)) {
found = true;
break;
}
}
if (!found) {
System.out.println("ERROR! ID not found!");
return;
}
System.out.printf("\n%-15s %-25s %-20s %-20s %-20s", "MemberID", "NAME", "CNIC", "PHONE", "LATEFEES");
line();
int memberIndex = 0;
int check = 0;
for (int i = 0; i < NotEmpty(members); i++) {
if ((members[i][0]).equalsIgnoreCase(id)) {
check = 1;
memberIndex = i;
System.out.printf("%-15s %-25s %-20s %-20s %-20s\n", members[i][0], members[i][1], members[i][2], members[i][3], members[i][5]);
}
}
if (check == 0) {
System.out.println("Member ID \"" + id + "\" Does Not Exist In The Database");
return;
}
if (!(Confirmation())) {
System.out.println("Operation Cancelled!");
return;
}
System.out.println("\nWrite Updated Info For Concerned Member.");
System.out.println("\nPress Enter To Leave Info For That Field As Is, Anything Entered will be Stored In that Field If Appropriate.\n");
System.out.print("Enter Updated Member First Name (Enter Current First Name If Last Name Is To Be Updated): ");
String firstName = input.nextLine();
firstName = firstName.trim();
System.out.print("Enter Updated Member Last Name (Enter Current Last Name If First Name Is Being Updated): ");
String lastName = input.nextLine();
lastName = lastName.trim();
if (!(firstName.isEmpty()) || !(lastName.isEmpty())) {
members[memberIndex][1] = lastName + "," + firstName;
System.out.println("Member Name Updated!");
} else {
System.out.println("Member Name Retained!");
}
System.out.print("Enter Updated CNIC (Format: xxxxx-xxxxxxx-x): ");
String cnic = input.nextLine();
cnic = cnic.trim();
if (!(cnic.isEmpty())) {
if (!(ValidateCNIC(cnic))) {
System.out.println("ERROR! Invalid CNIC!");
return;
}
members[memberIndex][2] = cnic;
System.out.println("Member CNIC Updated!");
} else {
System.out.println("Member CNIC Retained!");
}
System.out.print("Enter Updated Mobile Phone Number (Format: 03xx-xxxxxxx): ");
String num = input.nextLine();
num = num.trim();
if (!(num.isEmpty())) {
if (!(ValidatePhone(num))) {
System.out.println("ERROR! Invalid Phone Number!");
return;
}
members[memberIndex][3] = num;
System.out.println("Member Phone Number Updated!");
} else {
System.out.println("Member Phone Number Retained!");
}
System.out.println("\nInformation Updated!\n");
// Update the members.txt file by removing members data
UpdateFile("member", "yes", id);
line();
break; // Break the loop if all the data is successfully saved
} catch (InputMismatchException ex) {
System.out.println("\nERROR! Enter correct information");
input.nextLine(); // Refresh Buffer
}
}
}
// Create a method to show Member login console
public static boolean MemberLogin(){
loadMembers();
line();
//Run Loop until use enters correct data
while (true) {
try {
System.out.print("\nEnter Membership ID: ");