-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuizApplication.java
More file actions
972 lines (878 loc) · 37.3 KB
/
Copy pathQuizApplication.java
File metadata and controls
972 lines (878 loc) · 37.3 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
import java.util.*;
import java.io.*;
public class QuizApplication {
static Scanner input = new Scanner(System.in);
static boolean isUserLogin = false;
static boolean isAdminLogin = false;
static int age;
static String currentUser ;
public static boolean isStrong(String password){
if (password.length() < 8){
return false;
}
boolean hasDigit = false;
boolean hasUpper = false;
boolean hasLetter = false;
boolean hasSpecial = false;
//loop for checking the password
for(int i = 0 ; i < password.length() ; i++ ){
//storing character of password to check
char character = password.charAt(i);
if(Character.isDigit(character)){
hasDigit = true;
}
if(Character.isLetter(character)){
hasLetter = true;
}
if(Character.isUpperCase(character)){
hasUpper = true;
}
if (!(Character.isDigit(character) && Character.isLetter(character))) {
hasSpecial = true;
}
}
return hasDigit && hasLetter && hasSpecial && hasUpper;
}
public static boolean duplicateUsers(String userName , String password){
//opening the registration file
File file = new File("registrations.txt");
try{
Scanner reader = new Scanner(file);
while (reader.hasNext()){
String line = reader.nextLine();
String[] registrationDetails = line.trim().split("\\|");
if(userName.equals(registrationDetails[0]) &&
password.equals(registrationDetails[1])){
return true;
}
}
reader.close();
}
catch (FileNotFoundException e){
System.out.println("File Does Not Exists!");
}
return false;
}
public static boolean duplicateUsers(String userName ){
//opening the registration file
File file = new File("registrations.txt");
try{
Scanner reader = new Scanner(file);
while (reader.hasNext()){
String line = reader.nextLine();
String[] registrationDetails = line.trim().split("\\|");
if(userName.equals(registrationDetails[0])){
return true;
}
}
reader.close();
}
catch (FileNotFoundException e){
System.out.println("File Does Not Exists!");
}
return false;
}
public static void registerUser() throws InputMismatchException{
String userName;
String password;
while (true) {
//prompting user to enter user-name & password
System.out.print("Enter Your Name: ");
userName = input.nextLine();
//checking if user-name already taken
if(duplicateUsers(userName)){
System.out.println("User Name Already Taken");
System.out.println("Kindly Use Another User-Name");
continue;
}
while (true) {
try {
System.out.print("Enter Your Age: ");
age = input.nextInt();
break;
} catch (InputMismatchException e) {
System.out.println("Only Integers are Accepted....Try Again");
input.nextLine();
}
}
//validating age
if( age < 15){
return;
}
//clearing buffer
input.nextLine();
System.out.print("Enter a strong Password: ");
password = input.nextLine();
//checking if user already exists
if(duplicateUsers(userName,password)){
System.out.println("User Already Exists Kindly Login");
return;
}
if (!isStrong(password)) {
System.out.println("Weak Password! .... Kindly Enter a Strong Password");
}
else {
//if password is strong then exit the loop
System.out.println("***** Registered Successfully *******");
break;
}
}
try {
// opening file for storing registration records
FileWriter writer = new FileWriter("registrations.txt", true);
writer.write(userName + "|" + password + "|" + age + "\n");
//closing writer
writer.close();
}catch (FileNotFoundException e){
System.out.println("File Does Not Exist");
}
catch (IOException e){
System.out.println("Error While Writing to the file");
}
}
public static void loginUser() {
int attempts = 3;
while (true) {
System.out.print("Enter Name: ");
String userName = input.nextLine();
while (true) {
try {
System.out.print("Enter Your Age: ");
age = input.nextInt();
break;
} catch (InputMismatchException e) {
System.out.println("Only Integers are Accepted....Try Again");
input.nextLine();
}
}
//clearing buffer
input.nextLine();
System.out.print("Enter Your Password: ");
String password = input.nextLine();
File file = new File("registrations.txt");
try {
Scanner reader = new Scanner(file);
while (reader.hasNext()) {
String line = reader.nextLine();
String[] details = line.trim().split("\\|");
//storing age in registration file in a variable so to verify
int registeredAge = Integer.parseInt(details[2]);
if (userName.equals(details[0]) && password.equals(details[1]) &&
age == registeredAge) {
currentUser = userName;
isUserLogin = true;
break;
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
attempts --;
if(attempts == 0){
System.out.println("-------------------------------------");
System.out.println("| Better Luck Next Time!! |");
System.out.println("| You Have Wasted All Your Attempts |");
System.out.println("-------------------------------------");
break;
}
if (isUserLogin) {
System.out.println("******** User Login Successful ********");
break;
} else {
System.out.println("Invalid Credentials. Try Again!");
System.out.println("Attempts left: " + attempts);
}
}
}
public static void loginAdmin() {
int attempts = 3;
do{
System.out.print("Enter Name: ");
String userName = input.nextLine();
System.out.print("Enter Your Password: ");
String password = input.nextLine();
//opening the registration file to read data so to verify login
File file = new File("adminlogin.txt");
try {
Scanner reader = new Scanner(file);
while (reader.hasNext()) {
//storing line from the file in a variable
String line = reader.nextLine();
String[] details = line.trim().split("\\|");
//checking if the login details matches
if (userName.equals(details[0]) && password.equals(details[1])) {
isAdminLogin = true;
}
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("Can not Find the File");
}
if (isAdminLogin) {
System.out.println("******** Admin Login Successfully ********");
//getting out of loop if login successfully
break;
}
else {
System.out.println("Invalid Credentials ....... Try Again!");
System.out.println("You Have " +(attempts - 1) + ((attempts > 2)?" attempts":" attempt") + " left");
}
attempts --;
}while (attempts != 0 );
}
public static void showUserQuizMenu() {
System.out.println("=======================================");
System.out.println("| Welcome to User Quiz Section |");
System.out.println("=======================================");
System.out.println("| 1: Take Quiz |");
System.out.println("| 2: View Results |");
System.out.println("| 3: Exit |");
System.out.println("========================================");
//variable that will store user selected option
int userMenuOption;
while (true) {
try {
System.out.print("Kindly Select an Option from the User-Menu: ");
userMenuOption = input.nextInt();
if (userMenuOption < 1 || userMenuOption > 3){
System.out.println("Invalid Option....Try Again");
continue;
}
//getting out of loop when valid input received
break;
} catch (InputMismatchException e) {
System.out.println("Only Integers are Allowed! .... Try Again");
input.nextLine();
}
}
//checking what user selected from the MENU
switch (userMenuOption) {
case 1 -> startQuiz();
case 2 -> viewResults();
case 3 -> {
System.out.println("Logging Out......");
}
}
}
public static void showAdminQuizMenu() {
System.out.println("=======================================");
System.out.println("| Welcome to Admin Quiz Section |");
System.out.println("=======================================");
System.out.println("| 1: Add Quiz |");
System.out.println("| 2: View All Results |");
System.out.println("| 3: Edit Quiz |");
System.out.println("| 4: Delete Quiz |");
System.out.println("| 5: Exit |");
System.out.println("=======================================");
int adminChoice = 0;
while (true) {
try {
System.out.print("Kindly Select an option from the MENU: ");
adminChoice = input.nextInt();
if(adminChoice < 1 || adminChoice > 5){
System.out.println("Invalid Choice.... Try Again");
continue;
}
//clearing buffer
input.nextLine();
//getting out of loop if correct input is provided
break;
} catch (InputMismatchException e) {
System.out.println("Only Integers are Accepted.....Try Again");
input.nextLine();
}
}
switch (adminChoice) {
case 1 -> addQuiz();
case 2 -> viewAllResults();
case 3 -> editQuiz();
case 4 -> deleteQuiz();
case 5 -> {
System.out.println("Logging Out....");
isAdminLogin = false;
}
}
}
public static void subjects(){
File file = new File("Subjects.txt");
try{
Scanner reader = new Scanner(file);
while (reader.hasNext()){
String line = reader.nextLine();
System.out.println(line);
}
}catch (FileNotFoundException e){
System.out.println("File Does Not Exists");
}
}
public static void startQuiz() {
char userWants;
//checking what difficulty level is selected by the user
String difficulty = difficultyLevel().toUpperCase();
//setting timer for the quiz based on the difficulty
int totalTimeInSeconds = switch (difficulty.toUpperCase()){
//setting quiz time 2.5 minutes if user selected easy
case "EASY" -> 150;
//setting quiz time 5 minutes if user selected medium
case "MEDIUM" -> 5 * 60;
//setting quiz time 10 minutes if user selected hard
case "HARD" -> 10 * 60;
//setting timer randomly between 1 - 10
case "SURPRISE" -> (int)(1 + Math.random() * 10 ) * 60;
default -> 300;
};
if(difficulty.equals("SURPRISE")){
takeQuiz("SurpriseQuiz.txt","SurpriseKey.txt",currentUser,totalTimeInSeconds);
System.out.print("Want to Go Back to Menu (Y/N)?");
userWants = input.next().toUpperCase().charAt(0);
if(userWants == 'Y'){
showUserQuizMenu();
return;
}
else {
System.out.println("Logging Out.....");
return;
}
}
// if user selects surprise then quiz menu will not be shown to the user
System.out.println("=========== QUIZ MENU =============");
subjects();
System.out.println("=========================================");
//variable that will store what user selected from the QUIZ-MENU
String subjectName;
input.nextLine();
System.out.print("Enter the Subject Name to Attempt Quiz: ");
subjectName = input.nextLine();
String questionFile = subjectName + difficulty + "Quiz.txt";
String keyFile = subjectName + difficulty + "Key.txt";
takeQuiz(questionFile,keyFile,currentUser,totalTimeInSeconds);
System.out.println("Do You Want to Go to back to MENU then press Y otherwise " +
"press any other key to exit");
userWants = input.next().toUpperCase().charAt(0);
if (userWants != 'Y') {
System.out.println("Logging Out........");
}
else {
showUserQuizMenu();
}
}
//method to get total question that will help in compiling the result
public static int lineCounter(String File){
int counter = 0;
File file = new File(File);
try{
Scanner reader = new Scanner(file);
while (reader.hasNext()){
String line = reader.nextLine();
counter ++;
}
reader.close();
}catch (FileNotFoundException e){
System.out.println("File Does Not Exists");
}
return counter;
}
public static void takeQuiz(String questionFile , String key,String userName, int totalTimeInSeconds) {
int score = 0;
//getting how many total questions are there in the file
int questions = lineCounter(questionFile);
File file = new File(questionFile);
File file2 = new File(key);
try {
//for writing results in user file
FileWriter writer = new FileWriter(userName + ".txt", true);
//for writing result in a file accessible by admin
FileWriter resultWriter = new FileWriter("AllResults.txt",true);
// for reading questions
Scanner reader = new Scanner(file);
// for reading key
Scanner keyReader = new Scanner(file2);
// Start timer
long startTime = System.currentTimeMillis();
long lastMinuteUpdate = 0;
//giving user message that how much time user have to attempt the quiz
System.out.println("======================= NOTE! =======================");
System.out.println("| You Have " + (totalTimeInSeconds/60) + " minutes and "
+ (totalTimeInSeconds % 60) + " seconds to complete the Quiz |");
System.out.println("==========================================================");
while (reader.hasNext() && keyReader.hasNext()) {
//checking how much time has passed
long elapsedTime = (System.currentTimeMillis() - startTime) / 1000;
//checking how much time is left
long timeLeft = totalTimeInSeconds - elapsedTime;
if (timeLeft <= 0) {
System.out.println("\n Time's up! Quiz ended automatically.");
break;
}
// Showing user how much time is left for completing the quiz
if (elapsedTime - lastMinuteUpdate >= 30) {
System.out.println(" Time Left: " + (timeLeft / 60) + " minutes " + (timeLeft % 60) + " seconds");
lastMinuteUpdate = elapsedTime;
}
String line = reader.nextLine();
String[] mcq = line.trim().split("\\$");
char correctAnswer = keyReader.next().charAt(0);
for (String question : mcq) {
System.out.println(question);
}
char userAnswer;
while (true) {
System.out.print("Enter Your Answer: ");
userAnswer = input.next().toUpperCase().charAt(0);
if(userAnswer != 'A' && userAnswer != 'B'
&& userAnswer != 'C'&& userAnswer != 'D'){
System.out.println("Select option(A,B,C or D)");
}
else {
//clearing buffer
input.nextLine();
break;
}
}
if (userAnswer == correctAnswer) {
score++;
}
}
System.out.println("You Scored " + score + " out of " + questions);
//writing data in user results file
writer.write(userName + " Scored " + score + " out of " + questions + " in "
+ questionFile.substring(0, questionFile.indexOf(".")) + "\n");
//writing data in allResults file
resultWriter.write(userName + " scored " + score + "/"
+ lineCounter(questionFile) + " in " + questionFile.substring(0,questionFile.indexOf("Q")) + "\n" );
writer.close();
reader.close();
keyReader.close();
resultWriter.close();
} catch (FileNotFoundException e) {
System.out.println("File Not Found Exception");
} catch (IOException e) {
System.out.println("Error while reading/writing the file");
}
}
public static void viewResults() {
char userWants;
//opening the file in which result is stores
File file = new File(currentUser + ".txt");
if(!(file.exists())){
System.out.println("There is no Previous Record of " + currentUser);
System.out.println("*THANKS!*");
System.out.print("Do You Want to go back to USER-MENU(Y/N)? : ");
userWants = input.next().toUpperCase().charAt(0);
if(userWants != 'Y'){
System.out.println("Logging Out..........");
isUserLogin = false;
}
else {
showUserQuizMenu();
}
return;
}
try{
//creating scanner object to read from the file
Scanner reader = new Scanner(file);
while (reader.hasNext()){
String line = reader.nextLine();
System.out.println(line);
}
reader.close();
}
catch (FileNotFoundException e){
System.out.println("File Does Not Exist!");
}
System.out.println("Do You Want to Go to back to MENU then press Y otherwise " +
"press any other key to exit");
userWants = input.next().toUpperCase().charAt(0);
if (userWants != 'Y') {
System.out.println("Logging Out........");
isUserLogin = false;
}
else {
showUserQuizMenu();
}
}
public static void viewAllResults(){
File file = new File("AllResults.txt");
try{
Scanner reader = new Scanner(file);
while (reader.hasNext()){
String line = reader.nextLine();
System.out.println(line);
}
}catch (IOException e){
System.out.println("Error While Reading data from the file");
}
System.out.println("Do You Want to Go to back to MENU then press Y otherwise " +
"press any other key to exit");
char adminWants = input.next().toUpperCase().charAt(0);
if (adminWants != 'Y') {
System.out.println("Logging Out........");
isAdminLogin = false;
}
}
public static void addQuiz() {
System.out.print("Enter the Subject Name to Add: ");
String subjectName = input.nextLine();
//Now allowing admin to select the difficulty
System.out.println("=================================");
System.out.println("| 1: EASY |");
System.out.println("| 2: MEDIUM |");
System.out.println("| 3: HARD |");
System.out.println("==================================");
int difficultyOption;
while (true){
try{
System.out.printf("Select a Difficulty Level for %s",subjectName);
difficultyOption = input.nextInt();
if(difficultyOption <1 || difficultyOption>3){
System.out.println("Kindly Select a Valid Option");
}
else {
break;
}
}catch (InputMismatchException e){
System.out.println("Only Integers Are Accepted.....Try Again");
}
}
String difficulty = "";
switch (difficultyOption){
case 1 -> difficulty = "EASY";
case 2 -> difficulty = "MEDIUM";
case 3 -> difficulty = "HARD";
}
int lineNumber = lineCounter("Subjects.txt");
//creating files
File file1 = new File(subjectName + difficulty + "Quiz.txt");
File file2 = new File(subjectName + difficulty + "Key.txt");
try {
//creating file writer Object to append data
FileWriter writer = new FileWriter("Subjects.txt",true);
//creating Print Writer so we can format data in a specific way
PrintWriter writer1 = new PrintWriter(writer);
//counting line number in subject file
lineNumber ++;
writer1.printf("| %d: %-34s|\n",lineNumber,subjectName);
System.out.println(subjectName + " Added to the Subjects Successfully!");
if(file1.createNewFile()){
System.out.println( "Quiz File " + file1.getName() + " Created Successfully");
}
else {
System.out.println("Quiz File " + file1.getName() + " Already Exists");
}
if(file2.createNewFile()){
System.out.println("Key File " + file2.getName() + " Created Successfully");
}
else {
System.out.println("Key File " + file1.getName() + " Already Exists");
}
writer.close();
writer1.close();
}catch (FileNotFoundException e){
System.out.println("File Does Not exists");
}
catch (IOException e){
System.out.println("Error Occurred Writing or creating the files");
}
char adminWants;
System.out.println("Do You Want to Go to back to MENU then press Y otherwise " +
"press any other key to exit");
adminWants = input.next().toUpperCase().charAt(0);
if (adminWants != 'Y') {
System.out.println("Logging Out........");
isAdminLogin = false;
}
}
public static void deleteQuiz(){
System.out.print("Enter the Subject Name to Delete: ");
String subjectName = input.nextLine();
//Now allowing admin to select the difficulty
System.out.println("=================================");
System.out.println("| 1: EASY |");
System.out.println("| 2: MEDIUM |");
System.out.println("| 3: HARD |");
System.out.println("==================================");
int difficultyOption;
while (true){
try{
System.out.printf("Select a Difficulty Level for: %s\n",subjectName);
difficultyOption = input.nextInt();
if(difficultyOption <1 || difficultyOption>3){
System.out.print("Kindly Select a Valid Option: ");
}
else {
break;
}
}catch (InputMismatchException e){
System.out.println("Only Integers Are Accepted.....Try Again");
}
}
String difficulty = "";
switch (difficultyOption){
case 1 -> difficulty = "EASY";
case 2 -> difficulty = "MEDIUM";
case 3 -> difficulty = "HARD";
}
String quizFile = subjectName + difficulty + "Quiz.txt";
String keyFile = subjectName + difficulty + "Key.txt";
File file1 = new File(quizFile);
File file2 = new File(keyFile);
if(file1.delete() && file2.delete()){
System.out.println("All the files of " + subjectName + " deleted Successfully!");
}
//now logic for updating the subjects file
File subjects = new File("Subjects.txt");
File tempFile = new File("temp.txt");
try{
tempFile.createNewFile();
Scanner reader = new Scanner(subjects);
PrintWriter writer = new PrintWriter(tempFile);
while (reader.hasNext()) {
String line = reader.nextLine();
if (!line.contains(subjectName)) {
writer.println(line);
}
}
reader.close();
writer.close();
}catch (IOException e){
System.out.println("Error while reading or writing to the file");
}
//now deleting the subject file & renaming temp file with subject file
if(subjects.delete() && tempFile.renameTo(subjects)){
System.out.println(subjectName + " Subject removed from the Subject File Successfully");
}
else {
System.out.println("Error while updating the subjects file");
}
char adminWants;
System.out.println("Do You Want to Go to back to MENU then press Y otherwise " +
"press any other key to exit");
adminWants = input.next().toUpperCase().charAt(0);
if (adminWants != 'Y') {
System.out.println("Logging Out........");
isAdminLogin = false;
}
}
public static void editQuiz() {
char wantToAddQuestion;
System.out.println("=========== EDIT QUIZ ==============");
System.out.println("----------------------------------------");
System.out.println("| SUBJECTS |");
System.out.println("----------------------------------------");
subjects();
System.out.println("=========================================");
System.out.print("Enter Subject Name to Edit Quiz: ");
String quizSelection = input.nextLine();
// Prompt admin to select a subject to edit
System.out.println("=================================");
System.out.println("| 1: Easy |");
System.out.println("| 2: Medium |");
System.out.println("| 3: Hard |");
System.out.println("==================================");
int difficultyOption;
// Prompt admin to select difficulty level for editing quiz
while (true){
try{
System.out.print("Select a difficulty: ");
difficultyOption = input.nextInt();
if(difficultyOption <1 || difficultyOption>3){
System.out.println("Kindly Select a Valid Option");
}
else {
break;
}
}catch (InputMismatchException e){
System.out.println("Only Integers Are Accepted.....Try Again");
}
}
String difficulty = "";
switch (difficultyOption){
case 1 -> difficulty = "Easy";
case 2 -> difficulty = "Medium";
case 3 -> difficulty = "Hard";
}
String subjectName = quizSelection + difficulty;
try{
//creating file writer object to edit the question file
FileWriter questionWriter = new FileWriter(subjectName + "Quiz.txt", true);
FileWriter keyWriter = new FileWriter(subjectName + "Key.txt",true);
do {
//prompting admin to enter the question to add
System.out.println("Enter the Questions that you want to add along with the options: ");
input.nextLine(); // consume leftover newline
String question = input.nextLine();
//storing questions in the file
questionWriter.write(question + "\n");
//prompting admin to enter the key for the asked question
System.out.print("Now Enter the Key: ");
char key = input.next().toUpperCase().charAt(0);
//clearing buffer
input.nextLine();
//storing answer in the key file
keyWriter.write(key + "\n");
//asking admin if want to add more question , otherwise exit from loop
System.out.println("Do You want to Add More Questions?(Y/N): ");
wantToAddQuestion = input.next().toUpperCase().charAt(0);
} while (wantToAddQuestion == 'Y');
//closing resources
System.out.println("Questions Added Successfully!");
questionWriter.close();
keyWriter.close();
}
catch (FileNotFoundException e){
System.out.println("File Does Not exits");
}
catch (IOException e){
System.out.println("Error while Writing to the file");
}
char adminWants;
System.out.println("Do You Want to Go to back to MENU then press Y otherwise " +
"press any other key to exit");
adminWants = input.next().toUpperCase().charAt(0);
if (adminWants != 'Y') {
System.out.println("Logging Out........");
isAdminLogin = false;
}
}
public static String difficultyLevel() {
System.out.println("================================");
System.out.println("| 1: Easy |");
System.out.println("| 2: Medium |");
System.out.println("| 3: Hard |");
System.out.println("| 4: Surprise Me |");
System.out.println("=================================");
int optionSelected = 0;
while (true) {
try {
System.out.println("Kindly Select a Difficulty Level: ");
optionSelected = input.nextInt();
// getting out of loop if input is valid
if (optionSelected >= 1 && optionSelected <= 4) {
break;
} else {
System.out.println("Kindly Select Option 1, 2, 3, or 4");
}
} catch (InputMismatchException e) {
System.out.println("Only Integers are Accepted....Try Again");
input.nextLine(); // clear invalid input
}
}
String difficultySelected = "";
switch (optionSelected) {
case 1 -> difficultySelected = "Easy";
case 2 -> difficultySelected = "Medium";
case 3 -> difficultySelected = "Hard";
case 4 -> difficultySelected = "Surprise";
}
return difficultySelected;
}
public static void userLoginMenu(){
System.out.println("=======================================");
System.out.println("| Welcome to User Login Section |");
System.out.println("=======================================");
System.out.println("| 1: Register |");
System.out.println("| 2: Login |");
System.out.println("=======================================");
//variable that will store user's choice
int userOption;
//loop that will run till valid input is not given
while (true){
try {
System.out.print("Kindly Select an Option: ");
userOption = input.nextInt();
//clearing buffer
input.nextLine();
//getting out of loop if got the desired input
break;
}
catch (InputMismatchException e){
System.out.println("Only Integers are Accepted...... Try Again");
input.nextLine();
}
}
//checking which option user selected from the USER-MENU
switch (userOption) {
case 1 -> {
registerUser();
if(age < 15){
input.nextLine();
System.out.println("You Can't Take This Quiz");
System.out.println("Good Bye!");
System.out.print("Press Enter to Exit: ");
input.nextLine();
return;
}
else {
System.out.println("***** Kindly Enter Details to Now Login ******");
//after registration prompting user to login
loginUser();
}
}
case 2 -> {
loginUser();
}
}
//if user login successfully then show userQuizMenu
if (isUserLogin) {
showUserQuizMenu();
}
}
public static void adminLoginMenu(){
System.out.println("=======================================");
System.out.println("| Welcome to Admin Login Section |");
System.out.println("=======================================");
loginAdmin();
while (isAdminLogin) {
showAdminQuizMenu();
}
}
public static void main(String[] args) {
//declaring & initializing variables
int userChoice;
//displaying welcome message & prompting user to register or login accordingly
System.out.println("\n/==================================================\\");
System.out.println("| |");
System.out.println("| ! << Q U I Z A P P >> ! |");
System.out.println("| ! ========================= ! |");
System.out.println("| ! | W E L C O M E | ! |");
System.out.println("| ! ========================= ! |");
System.out.println("| ! ! |");
System.out.println("|--------------------------------------------------|");
System.out.println("| |");
System.out.println("| ! +----------------------+ ! |");
System.out.println("| ! | 1. USER | ! |");
System.out.println("| ! | 2. ADMIN | ! |");
System.out.println("| ! +----------------------+ ! |");
System.out.println("| ! ! |");
System.out.println("\\==================================================/");
while (true) {
try{
System.out.print(">> Select an Option[1-2]: ");
userChoice = input.nextInt();
//clearing buffer
input.nextLine();
//verifying whether user selected valid option
if(userChoice != 1 && userChoice != 2){
System.out.println("> Invalid Option.... Try Again");
continue;
}
//if input is valid then getting out of the loop
break;
}
catch (InputMismatchException e){
System.out.println("> Only Integers are Accepted....... Try Again");
//clearing buffer
input.nextLine();
}
}
// checking what user selected
if (userChoice == 1) {
userLoginMenu();
}
else {
adminLoginMenu();
}
//closing scanner input
input.close();
}
}