-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientOne.java
1630 lines (1507 loc) · 83.6 KB
/
ClientOne.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.*;
import java.io.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.text.*;
/**
* Project 5 - ClientOne class
*
* This class is the client side code. When a user wants to utilize our application
* , they run this class in order to connect to the server. Once the connection is made, that means
* a client will have a socket to the server, and the server will have a socket to the client.
*
* This class will display the GUIs (JFrames and JOptionPanes) for the users to interact with, and their actions
* will cause information to be sent from the client to the server. Depending on what the user is doing, the server
* will receive the information sent from the client, do the necessary actions, and send information back to the
* client so things like real time updates can be displayed.
*
* More information can be found in the documentation that's in the Readme file that's in our repository.
*
* Lab Section L24
*
* @author Yu Hyun Kim
* @version final version 12/12/21
*/
public class ClientOne implements Runnable {
static BufferedReader bfr; //BufferedReader object
static PrintWriter writer; //PrintWriter object
static Socket theSocket; //socket object
static String emailLoggedIn; //email of user that's logged in
/**
* startMenu related GUI variables
*/
static JFrame startMenu = new JFrame("Start Menu"); //start menu JFrame
static JLabel studentLoginGreeting = new JLabel(); //Jlabel for student welcome message
static JLabel teacherLoginGreeting = new JLabel(); //JLabel for teacher welcome message
/**
* GUI related to teacher
*/
static JFrame teacherGradeMenu = new JFrame("Submissions to grade"); //JFrame that shows quizzes to grade
static JTextArea listToGrade = new JTextArea(); //JLabel for list of quizzes to grade
static JFrame teacherMenu = new JFrame("Welcome! You are a teacher!"); //JFrame for teacher Menu
static JFrame coursesMenu = new JFrame("Your Created Courses"); //JFrame for teacher to see courses
static JTextArea listOfCourses = new JTextArea(); //lists teacher's courses
static JFrame quizMenu = new JFrame("Manage quizzes"); //JFrame for teacher to see their quizzes
static JTextArea listOfQuizzesInCourse = new JTextArea(); //JLabel to list the quizzes made
static String courseBeingEdited; //the course that's being edited by the teacher
/**
* GUI related to student
*/
static JFrame studentMenu = new JFrame("Welcome! You are a student!"); //JFrame for student Menu
static JFrame studentCourseMenu = new JFrame("Here are all the courses that exist"); //JFrame to display
//all courses
static JTextArea allCoursesList = new JTextArea(); //lists all courses
static String courseStudentSeeing = ""; //the course the student is seeing
static JFrame studentQuizMenu = new JFrame("Here are all the quizzes"); //JFrame for student to see
//quizzes for specific course
static JTextArea allQuizzesForCourse = new JTextArea(); //JTextArea that lists all Quizzes in course
static JFrame studentSubmissionMenu = new JFrame("Here are all your submissions"); //JFrame for
//student to see submissions
static JTextArea listOfSubmissions = new JTextArea(); //lists submissions
public static void main(String[] args) {
Thread client1 = new Thread(new ClientOne()); //creates a new thread
client1.start(); //starts the thread
try {
Socket socket = new Socket("localhost", 1234); //socket object
theSocket = socket;
bfr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream());
/**
* WE ARE CREATING START MENU
* CREATE ACCOUNT/LOGIN/EXIT
*
* We are making the content panes, JPanels, JButtons, and the ActionListeners
*/
Container startContent = startMenu.getContentPane();
JPanel startJpanel = new JPanel();
JLabel welcomeMessage = new JLabel("Welcome. Press a button to start");
startContent.setLayout(new BorderLayout());
startMenu.setSize(320, 250);
startMenu.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
startMenu.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
writer.write("stop");
writer.println();
writer.flush();
}
});
startJpanel.add(welcomeMessage, BorderLayout.NORTH);
JButton createAccBut = new JButton("Create Account");
startJpanel.add(createAccBut, BorderLayout.SOUTH);
JButton loginBut = new JButton("Login");
startJpanel.add(loginBut, BorderLayout.SOUTH);
startContent.add(startJpanel, BorderLayout.CENTER);
startMenu.setVisible(true);
/**
* What happens when user presses the "Create account button"
*
* Asks user for account info, tells the server that account is being made, and sends the
* account info to the server
*/
ActionListener createAccListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == createAccBut) {
String name = JOptionPane.showInputDialog(null,
"Enter name", "Enter name", JOptionPane.QUESTION_MESSAGE); //name
if (name != null) {
String email = ""; //email
email = JOptionPane.showInputDialog(null,
"Enter email", "Enter email", JOptionPane.QUESTION_MESSAGE);
if (email != null) {
writer.write("createAccount");
writer.println();
writer.flush();
writer.write(name);
writer.println();
writer.flush();
writer.write(email);
writer.println();
writer.flush();
}
}
}
}
};
createAccBut.addActionListener(createAccListener);
/**
* What happens when login button is pressed
*
* User enters their email, and the client sends a String to the server saying that a user wants to login
* and client sends the email that the user entered.
*/
ActionListener loginListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String enteredEmail = JOptionPane.showInputDialog(null,
"Enter your email to login", "Login", JOptionPane.QUESTION_MESSAGE); //email
if (enteredEmail != null) {
writer.write("loginAccount");
writer.println();
writer.flush();
writer.write(enteredEmail);
writer.println();
writer.flush();
emailLoggedIn = enteredEmail;
}
}
};
loginBut.addActionListener(loginListener);
/**
* WE ARE CREATING THE GRADE SUBMISSIONS MENU FOR THE TEACHER
*
* We are making the content panes, JPanels, JButtons, and the ActionListeners
*/
Container gradeContent = teacherGradeMenu.getContentPane();
gradeContent.setLayout(new BorderLayout());
teacherGradeMenu.setSize(400, 400);
teacherGradeMenu.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
teacherGradeMenu.setVisible(false);
listToGrade.setEditable(false);
teacherGradeMenu.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
teacherMenu.setVisible(true);
}
});
JPanel listToGradeJpanel = new JPanel();
listToGradeJpanel.add(listToGrade, BorderLayout.NORTH);
gradeContent.add(listToGradeJpanel, BorderLayout.NORTH);
JPanel gradeMenuButtonJpanel = new JPanel();
gradeContent.add(gradeMenuButtonJpanel, BorderLayout.SOUTH);
JButton teacherGradeButton = new JButton("Grade a quiz");
gradeMenuButtonJpanel.add(teacherGradeButton, BorderLayout.SOUTH);
/**
* What happens when teacher wants to grade quiz
*
* Teacher enters the quiz name they want to grade, and information is sent to the server
*/
ActionListener teacherGradingListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String quizName = JOptionPane.showInputDialog(null, "Enter quiz to grade",
"enter quiz name", JOptionPane.QUESTION_MESSAGE);
if (quizName != null) {
writer.write("gradeQuiz");
writer.println();
writer.flush();
writer.write(emailLoggedIn);
writer.println();
writer.flush();
writer.write(quizName);
writer.println();
writer.flush();
}
}
};
teacherGradeButton.addActionListener(teacherGradingListener);
/**
* CREATING THE QUIZ MENU FOR THE TEACHER
*
* We are making the content panes, JPanels, JButtons, and the ActionListeners
*/
Container quizMenuContent = quizMenu.getContentPane();
quizMenuContent.setLayout(new BorderLayout());
quizMenu.setSize(400, 400);
quizMenu.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
quizMenu.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
coursesMenu.setVisible(true);
}
});
listOfQuizzesInCourse.setEditable(false);
quizMenu.setVisible(false);
JPanel quizJpanel = new JPanel();
JPanel quizJpanelButtons = new JPanel();
JButton createQuiz = new JButton("Create Quiz");
/**
* What happens when teacher wants to create quiz
*
* Teacher enters all quiz info, and then information is sent to the server
*/
ActionListener createQuizListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String quizName = JOptionPane.showInputDialog(null, "Enter quiz name",
"Making a new quiz!", JOptionPane.QUESTION_MESSAGE); //quiz name
if (quizName != null) {
writer.write("makingQuiz");
writer.println();
writer.flush();
writer.write(courseBeingEdited);
writer.println();
writer.flush();
writer.write(emailLoggedIn);
writer.println();
writer.flush();
writer.write(quizName);
writer.println();
writer.flush();
String numberOfQuestions;
int numQuestions = 0;
/**
* asks for number of questions
*/
while (true) {
try {
numberOfQuestions = JOptionPane.showInputDialog(null,
"Enter the number of questions for the quiz",
"Number of questions", JOptionPane.QUESTION_MESSAGE);
if (numberOfQuestions == null) {
JOptionPane.showMessageDialog(null, "You must finish " +
"making the quiz", "Must make quiz", JOptionPane.ERROR_MESSAGE);
continue;
}
numQuestions = Integer.parseInt(numberOfQuestions);
if (numQuestions < 1) {
JOptionPane.showMessageDialog(null,
"Make sure to enter a number equal to or greater than 1 " +
"next time!", "Error with number of questions",
JOptionPane.ERROR_MESSAGE);
} else {
break;
}
} catch (Exception numConvError) {
JOptionPane.showMessageDialog(null,
"Make sure to enter a valid number and make sure " +
"it's 1 or greater next time!", "Error with number of questions",
JOptionPane.ERROR_MESSAGE);
}
}
//WE HAVE NUM QUESTIONS
/**
* asks for number of answers
*/
String numAnswers;
int numAns = 0;
while (true) {
try {
numAnswers = JOptionPane.showInputDialog(null,
"Enter the number of answer choices for each question for the quiz",
"Number of answer choices per question", JOptionPane.QUESTION_MESSAGE);
//number of answer choices for each question
if (numAnswers == null) {
JOptionPane.showMessageDialog(null, "You must finish " +
"making the quiz", "Must make quiz", JOptionPane.ERROR_MESSAGE);
continue;
}
numAns = Integer.parseInt(numAnswers);
if (numAns < 1) {
JOptionPane.showMessageDialog(null, "Make sure to " +
"enter a number equal to or greater than 1 next time!",
"Error with " +
"number of answers", JOptionPane.ERROR_MESSAGE);
} else {
break;
}
} catch (Exception numConvError) {
JOptionPane.showMessageDialog(null,
"Make sure to enter a valid number and make sure it's 1 or " +
"greater next time!", "Error with number of answers",
JOptionPane.ERROR_MESSAGE);
}
}
//we have num answers
String questionString = ""; //String of questions
String answerString = ""; //String of answer choices
/**
* Get the question and the answer choices for each question
*/
for (int i = 1; i <= numQuestions; i++) {
while (true) {
String question = JOptionPane.showInputDialog(null,
"Enter the question for Question " + i + "\n***NOTE: " +
"YOU CANNOT HAVE '*' IN THE QUESTION", "Enter question",
JOptionPane.QUESTION_MESSAGE);
//the question
if (question == null) {
JOptionPane.showMessageDialog(null,
"You must finish making this quiz. Please type your question.",
"Must finish making quiz", JOptionPane.ERROR_MESSAGE);
} else if (question.length() == 0) {
JOptionPane.showMessageDialog(null,
"You didn't enter any characters. Try again",
"Enter a question please", JOptionPane.ERROR_MESSAGE);
} else if (question.indexOf("*") != -1) {
JOptionPane.showMessageDialog(null,
"You can't have '*' anywhere in your question.",
"No '*' in question", JOptionPane.ERROR_MESSAGE);
} else {
questionString += (question + "**"); //question successfully made and break
// from while loop to get answer choices;
break;
}
}
for (int j = 1; j <= numAns; j++) {
while (true) {
String answerChoice = JOptionPane.showInputDialog(null,
"Enter answer choice " + j + "\n***NOTE: YOU CANNOT HAVE '*' " +
"IN ANSWER CHOICE", "Enter answer choice",
JOptionPane.QUESTION_MESSAGE);
//answer choice
if (answerChoice == null) {
JOptionPane.showMessageDialog(null,
"You must finish making this quiz. Please type your" +
" answer choice.", "Must finish making quiz",
JOptionPane.ERROR_MESSAGE);
} else if (answerChoice.length() == 0) {
JOptionPane.showMessageDialog(null,
"You didn't enter any characters. Try again",
"Enter something please", JOptionPane.ERROR_MESSAGE);
} else if (answerChoice.indexOf("*") != -1) {
JOptionPane.showMessageDialog(null,
"You can't have '*' anywhere in your answer choice.",
"No '*' in answer choice", JOptionPane.ERROR_MESSAGE);
} else {
answerString += (answerChoice + "**");
break;
}
}
}
}
writer.write(questionString);
writer.println();
writer.flush();
writer.write(answerString);
writer.println();
writer.flush();
writer.write(numberOfQuestions);
writer.println();
writer.flush();
writer.write(numAnswers);
writer.println();
writer.flush();
/**
* asks the teacher whether they want to randomize the quiz or not
*/
while (true) {
int randomOrNot = JOptionPane.showConfirmDialog(null,
"Do you want the quiz to be randomized",
"Should quiz be randomized?", JOptionPane.YES_NO_OPTION);
if (randomOrNot != JOptionPane.YES_OPTION && randomOrNot != JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null,
"You must select yes or no.", "Select yes or no",
JOptionPane.ERROR_MESSAGE);
} else {
if (randomOrNot == JOptionPane.NO_OPTION) { //if user DOESNT WANT Random
writer.write("noRandomize");
writer.println();
writer.flush();
} else { //if user wants random
writer.write("randomize");
writer.println();
writer.flush();
}
break;
}
}
}
}
};
createQuiz.addActionListener(createQuizListener);
JButton editQuiz = new JButton("Edit Quiz");
/**
* What happens if the teacher wants to edit the quiz.
*
* The process is pretty similar to a teacher making a quiz, which is shown above.
*/
ActionListener editQuizListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String quizName = JOptionPane.showInputDialog(null,
"Enter the name of the quiz that you want to change" +
"\n**Note: If you enter in an invalid quiz name,\nyou will still be prompted to " +
"enter\nthe new information regarding the quiz, but\nafter " +
"you put in all the info, you will receive an\nerror message, and no changes will " +
"be made.", "Editing a quiz!", JOptionPane.QUESTION_MESSAGE);
//quiz name the teacher wants
if (quizName != null) {
writer.write("editingQuiz");
writer.println();
writer.flush();
writer.write(courseBeingEdited);
writer.println();
writer.flush();
writer.write(quizName);
writer.println();
writer.flush();
String numberOfQuestions;
int numQuestions = 0; //number of questions in the quiz
while (true) {
try {
numberOfQuestions = JOptionPane.showInputDialog(null,
"Enter the number of questions for the quiz",
"Number of questions", JOptionPane.QUESTION_MESSAGE);
if (numberOfQuestions == null) {
JOptionPane.showMessageDialog(null,
"You must finish editing the quiz", "Must edit quiz",
JOptionPane.ERROR_MESSAGE);
continue;
}
numQuestions = Integer.parseInt(numberOfQuestions);
if (numQuestions < 1) {
JOptionPane.showMessageDialog(null,
"Make sure to enter a number equal to or greater " +
"than 1 next time!", "Error with number of questions",
JOptionPane.ERROR_MESSAGE);
} else {
break;
}
} catch (Exception numConvError) {
JOptionPane.showMessageDialog(null, "Make sure to enter a " +
"valid number and make sure it's 1 or greater next time!", "Error with " +
"number of questions", JOptionPane.ERROR_MESSAGE);
}
}
//WE HAVE NUM QUESTIONS
String numAnswers;
int numAns = 0; //number of answer choices for each question
while (true) {
try {
numAnswers = JOptionPane.showInputDialog(null,
"Enter the number of answer choices for each question",
"Number of answer choices per question", JOptionPane.QUESTION_MESSAGE);
//number of answer choices being asked
if (numAnswers == null) {
JOptionPane.showMessageDialog(null,
"You must finish editing the quiz", "Must edit quiz",
JOptionPane.ERROR_MESSAGE);
continue;
}
numAns = Integer.parseInt(numAnswers);
if (numAns < 1) {
JOptionPane.showMessageDialog(null,
"Make sure to enter a number equal to or greater than 1 " +
"next time!", "Error with number of answers",
JOptionPane.ERROR_MESSAGE);
} else {
break;
}
} catch (Exception numConvError) {
JOptionPane.showMessageDialog(null, "Make sure to enter " +
"a valid number and make sure it's 1 or greater next time!",
"Error with number of answers", JOptionPane.ERROR_MESSAGE);
}
}
//we have num answers
String questionString = ""; //String that has all the questions
String answerString = ""; //String that stores all the answer choices
/**
* The for loop below allows the teacher to make the quiz questions and the answer choices
* for each of the questions
*/
for (int i = 1; i <= numQuestions; i++) {
while (true) {
String question = JOptionPane.showInputDialog(null,
"Enter the question for Question " + i +
"\n***NOTE: YOU CANNOT HAVE '*' IN THE QUESTION",
"Enter question", JOptionPane.QUESTION_MESSAGE);
if (question == null) {
JOptionPane.showMessageDialog(null,
"You must finish making this quiz. Please type your question.",
"Must finish making quiz", JOptionPane.ERROR_MESSAGE);
} else if (question.length() == 0) {
JOptionPane.showMessageDialog(null,
"You didn't enter any characters. Try again",
"Enter a question please", JOptionPane.ERROR_MESSAGE);
} else if (question.indexOf("*") != -1) {
JOptionPane.showMessageDialog(null,
"You can't have '*' anywhere in your question.",
"No '*' in question", JOptionPane.ERROR_MESSAGE);
} else {
questionString += (question + "**"); //question successfully made and
// break from while loop to get answer choices;
break;
}
}
for (int j = 1; j <= numAns; j++) {
while (true) {
String answerChoice = JOptionPane.showInputDialog(null,
"Enter answer choice " + j + "\n***NOTE: YOU CANNOT HAVE '*' " +
"IN ANSWER CHOICE", "Enter answer choice",
JOptionPane.QUESTION_MESSAGE); //teacher's answer choice
if (answerChoice == null) {
JOptionPane.showMessageDialog(null,
"You must finish making this quiz. " +
"Please type your answer choice.",
"Must finish making quiz", JOptionPane.ERROR_MESSAGE);
} else if (answerChoice.length() == 0) {
JOptionPane.showMessageDialog(null,
"You didn't enter any characters. Try again",
"Enter something please", JOptionPane.ERROR_MESSAGE);
} else if (answerChoice.indexOf("*") != -1) {
JOptionPane.showMessageDialog(null,
"You can't have '*' anywhere in your answer choice.",
"No '*' in answer choice", JOptionPane.ERROR_MESSAGE);
} else {
answerString += (answerChoice + "**");
break;
}
}
}
}
writer.write(questionString);
writer.println();
writer.flush();
writer.write(answerString);
writer.println();
writer.flush();
writer.write(numberOfQuestions);
writer.println();
writer.flush();
writer.write(numAnswers);
writer.println();
writer.flush();
while (true) {
int randomOrNot = JOptionPane.showConfirmDialog(null,
"Do you want the quiz to be randomized",
"Should quiz be randomized?", JOptionPane.YES_NO_OPTION);
//asking whether quiz should be randomized or not
if (randomOrNot != JOptionPane.YES_OPTION && randomOrNot != JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null,
"You must select yes or no.",
"Select yes or no", JOptionPane.ERROR_MESSAGE);
} else {
if (randomOrNot == JOptionPane.NO_OPTION) { //if user DOESNT WANT Random
writer.write("noRandomize");
writer.println();
writer.flush();
} else { //if user wants random
writer.write("randomize");
writer.println();
writer.flush();
}
break;
}
}
}
}
};
editQuiz.addActionListener(editQuizListener);
JButton deleteQuiz = new JButton("Delete Quiz");
/**
* What happens when a teacher wants to delete a quiz
*
* The quiz name is sent to the server, and the server process that info and
* acts accordingly
*/
ActionListener deleteQuizListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String quizToDelete = JOptionPane.showInputDialog(null,
"Enter name of quiz you want to delete",
"name of quiz you want to delete", JOptionPane.QUESTION_MESSAGE);
//name of quiz teacher is trying to delete
if (quizToDelete != null) {
writer.write("deleteQuiz");
writer.println();
writer.flush();
writer.write(courseBeingEdited);
writer.println();
writer.flush();
writer.write(quizToDelete);
writer.println();
writer.flush();
}
}
};
deleteQuiz.addActionListener(deleteQuizListener);
quizJpanel.add(listOfQuizzesInCourse);
quizMenuContent.add(quizJpanel, BorderLayout.NORTH);
quizJpanelButtons.add(createQuiz);
quizJpanelButtons.add(editQuiz);
quizJpanelButtons.add(deleteQuiz);
quizMenuContent.add(quizJpanelButtons, BorderLayout.SOUTH);
/**
* CREATING THE COURSES MENU FOR THE TEACHER
*
* We are making the content panes, JPanels, JButtons, and the ActionListeners
*/
Container coursesContent = coursesMenu.getContentPane();
coursesContent.setLayout(new BorderLayout());
coursesMenu.setSize(600, 400);
coursesMenu.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
coursesMenu.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
teacherMenu.setVisible(true);
}
});
listOfCourses.setEditable(false);
JPanel courseJpanel = new JPanel();
JPanel courseMenuButtons = new JPanel();
JButton createCourse = new JButton("Create Course");
/**
* What happens when teacher wants to create course
*
* Get the course info and send it to the server
*/
ActionListener createCourseListener = new ActionListener() { //CREATING COURSE
@Override
public void actionPerformed(ActionEvent e) {
String courseName = JOptionPane.showInputDialog(null,
"Enter course name", "Making a new course!", JOptionPane.QUESTION_MESSAGE);
//course name trying to be made
if (courseName != null) {
writer.write("makeCourse");
writer.println();
writer.flush();
writer.write(emailLoggedIn);
writer.println();
writer.flush();
writer.write(courseName);
writer.println();
writer.flush();
}
}
};
createCourse.addActionListener(createCourseListener);
JButton editCourse = new JButton("Edit Course");
/**
* What happens when the teacher wants to edit a course
*
* Info is sent to the server regarding the course that's trying to be edited
*/
ActionListener editCourseListener = new ActionListener() { //EDITING COURSE
@Override
public void actionPerformed(ActionEvent e) {
String courseToEdit = JOptionPane.showInputDialog(null,
"Type course you want to edit", "Editing Course",
JOptionPane.QUESTION_MESSAGE); //course name of course trying to be edited
if (courseToEdit != null) {
writer.write("editCourse");
writer.println();
writer.flush();
writer.write(courseToEdit);
writer.println();
writer.flush();
courseBeingEdited = courseToEdit; // courseName
}
}
};
editCourse.addActionListener(editCourseListener);
JButton deleteCourse = new JButton("Delete Course");
/**
* What happens when the teacher wants to delete the course
*
* Info is sent to the server regarding the course that's trying to be deleted
*/
ActionListener delCourseListener = new ActionListener() { //Deleting course
@Override
public void actionPerformed(ActionEvent e) {
String courseToDelete = JOptionPane.showInputDialog(null,
"Enter the name of the course you want to delete",
"Deleting course", JOptionPane.QUESTION_MESSAGE); //course name of course
//trying to be deleted
if (courseToDelete != null) {
writer.write("deleteCourse");
writer.println();
writer.flush();
writer.write(courseToDelete);
writer.println();
writer.flush();
writer.write(emailLoggedIn);
writer.println();
writer.flush();
}
}
};
deleteCourse.addActionListener(delCourseListener);
courseJpanel.add(listOfCourses, BorderLayout.NORTH);
courseMenuButtons.add(createCourse);
courseMenuButtons.add(editCourse);
courseMenuButtons.add(deleteCourse);
coursesContent.add(courseJpanel, BorderLayout.NORTH);
coursesContent.add(courseMenuButtons, BorderLayout.SOUTH);
coursesMenu.setVisible(false);
/**
* Creating JFrame for teacher menu.
* 1) Manage Courses/Quizzes
* 2) Grade Quizzes
* 3) Edit Name
* 4) Delete
* 5) Logout
*
* We are making all the Containers, JPanels, Buttons, and ActionListeners
*/
Container tMenuContent = teacherMenu.getContentPane();
tMenuContent.setLayout(new BorderLayout());
teacherMenu.setSize(700, 200);
teacherMenu.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
teacherMenu.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
startMenu.setVisible(true);
}
});
JPanel greetingTeacherJpanel = new JPanel();
greetingTeacherJpanel.add(teacherLoginGreeting, BorderLayout.NORTH);
JPanel tMainJpanel = new JPanel();
JButton manageButton = new JButton("Manage Course/Quiz");
tMainJpanel.add(manageButton, BorderLayout.CENTER);
/**
* When teacher presses the button that shows them the courses they made
*/
ActionListener manageCourseButton = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
writer.write("manageCourse");
writer.println();
writer.flush();
writer.write(emailLoggedIn);
writer.println();
writer.flush();
}
};
manageButton.addActionListener(manageCourseButton);
JButton gradeButton = new JButton("Grade Quizzes");
tMainJpanel.add(gradeButton, BorderLayout.SOUTH);
/**
* What happens when teacher presses button that shows them the submissions to grade
*/
ActionListener gradeListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
writer.write("teacherWantsToGrade");
writer.println();
writer.flush();
writer.write(emailLoggedIn);
writer.println();
writer.flush();
teacherMenu.setVisible(false);
teacherGradeMenu.setVisible(true);
}
};
gradeButton.addActionListener(gradeListener);
/**
* If teacher wants to edit name
*/
JButton editNameButton = new JButton("Edit Name");
tMainJpanel.add(editNameButton, BorderLayout.SOUTH);
ActionListener changeNameListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String newName = JOptionPane.showInputDialog(null,
"Enter new name", "Name change", JOptionPane.QUESTION_MESSAGE);
//new name of teacher
if (newName != null) {
writer.write("changeName");
writer.println();
writer.flush();
writer.write(emailLoggedIn);
writer.println();
writer.flush();
writer.write(newName);
writer.println();
writer.flush();
}
}
};
editNameButton.addActionListener(changeNameListener);
/**
* if teacher wants to delete account
*/
JButton deleteButton = new JButton("Delete account");
tMainJpanel.add(deleteButton, BorderLayout.SOUTH);
ActionListener deleteAccListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
writer.write("deleteAccount");
writer.println();
writer.flush();
writer.write(emailLoggedIn);
writer.println();
writer.flush();
}
};
deleteButton.addActionListener(deleteAccListener);
JButton logoutButton = new JButton("Logout");
tMainJpanel.add(logoutButton, BorderLayout.SOUTH);
/**
* If teacher wants to logout
*/
ActionListener logoutListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
writer.write("logout");
writer.println();
writer.flush();
teacherMenu.setVisible(false);
studentMenu.setVisible(false);
startMenu.setVisible(true);
}
};
logoutButton.addActionListener(logoutListener);
tMenuContent.add(tMainJpanel, BorderLayout.SOUTH);
tMenuContent.add(greetingTeacherJpanel, BorderLayout.NORTH);
teacherMenu.setVisible(false);
/**
* Creating JFrame for student to see their submissions
*
* Making the Container, JPanels, JButtons, ActionListeners, and TextAreas
*/
Container studentSubContent = studentSubmissionMenu.getContentPane();
studentSubContent.setLayout(new BorderLayout());
studentSubmissionMenu.setSize(400, 400);
studentSubmissionMenu.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
studentSubmissionMenu.setVisible(false);
listOfSubmissions.setEditable(false);
studentSubmissionMenu.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
studentMenu.setVisible(true);
}
});
JPanel stuSubJpanel = new JPanel();
stuSubJpanel.add(listOfSubmissions, BorderLayout.NORTH);
studentSubContent.add(stuSubJpanel, BorderLayout.NORTH);
/**
* Making JFrame that enables student to see quizzes they can take for a course
*
*/
Container studentQuizContent = studentQuizMenu.getContentPane();
studentQuizContent.setLayout(new BorderLayout());
studentQuizMenu.setSize(400, 400);
studentQuizMenu.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
studentQuizMenu.setVisible(false);
allQuizzesForCourse.setEditable(false);
studentQuizMenu.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
studentCourseMenu.setVisible(true);
}
});
JPanel stuQuizJpanel = new JPanel();
stuQuizJpanel.add(allQuizzesForCourse, BorderLayout.NORTH);
JPanel stuQuizButtonJpanel = new JPanel();
JButton takeQuizButton = new JButton("Take Quiz");
stuQuizButtonJpanel.add(takeQuizButton, BorderLayout.SOUTH);
/**
* What happens when student wants to take quiz
*/
ActionListener takeQuizListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String quizTaking = JOptionPane.showInputDialog(null,
"Enter the quiz you want to take", "Enter quiz name",
JOptionPane.QUESTION_MESSAGE); //quiz name student is trying to take
if (quizTaking != null) {
writer.write("takingQuiz");
writer.println();
writer.flush();
writer.write(emailLoggedIn);
writer.println();
writer.flush();