66package StudentQuizScores ;
77
88import java .util .ArrayList ;
9- import java .util .HashMap ;
109import java .util .List ;
11- import java .util .Map ;
12- import java .util .Set ;
1310import java .util .Random ;
1411
1512/**
@@ -29,8 +26,11 @@ public void displayMenu() {
2926 int userChoice = 0 , studentIndex = -1 ;
3027 String firstName , lastName ;
3128 boolean has ;
29+ Classes classes = new Classes ();
3230
33- while (userChoice != 6 ) {
31+ classes .readData ();
32+
33+ while (userChoice != 7 ) {
3434
3535 IO .print ("" );
3636 IO .print ("Choose your Quest: " );
@@ -39,130 +39,47 @@ public void displayMenu() {
3939 IO .print ("3: Remove Student" );
4040 IO .print ("4: View List of Student Quiz Scores" );
4141 IO .print ("5: View Average Quiz Scores for One Student" );
42- IO .print ("6: Exit" );
42+ IO .print ("6: Save Data to File" );
43+ IO .print ("7: Exit" );
4344
44- userChoice = IO .readInt ("" , 1 , 6 );
45+ userChoice = IO .readInt ("" , 1 , 7 );
4546
4647 switch (userChoice ) {
47- case 1 :
48+ case 1 : //View Student List
4849 IO .print ("The Students are:" );
4950 IO .print ("=================" );
50- for (Student currentStudent : students ) {
51+ for (currentStudent : students ) {
5152 System .out .println (currentStudent .getFirstName () + " " + currentStudent .getLastName ());
5253 }
5354 break ;
5455
55- case 2 :
56- firstName = IO .readString ("Enter the first name: " );
57- lastName = IO .readString ("Enter the last name: " );
58- has = hasStudent (firstName , lastName );
59- if (has ) {
60- IO .print ("That name is already there, ya dingus!" );
61- } else {
62- Student newStudent = new Student (firstName , lastName );
63- students .add (newStudent );
64- newStudent .quizzes .put (1 , 95.23d );
65- newStudent .quizzes .put (2 , 77.98d );
66- newStudent .quizzes .put (3 , 86.17d );
67- IO .print ("New student successfully added." );
68- }
56+ case 2 : //Add New Student
57+ classes .addNewStudent ();
6958 break ;
7059
71- case 3 :
72- firstName = IO .readString ("Enter the first name: " );
73- lastName = IO .readString ("Enter the last name: " );
74- has = hasStudent (firstName , lastName );
75- if (!has ) {
76- IO .print ("That name isn't there, meathead!" );
77- } else {
78- studentIndex = getStudentIndex (firstName , lastName );
79- students .remove (studentIndex );
80- IO .print ("New student successfully removed." );
81- }
60+ case 3 : //Remove Student
61+ classes .removeStudent ();
8262 break ;
8363
84- case 4 :
85- firstName = IO .readString ("Enter the first name: " );
86- lastName = IO .readString ("Enter the last name: " );
87- has = hasStudent (firstName , lastName );
88- if (!has ) {
89- IO .print ("That name isn't there, goofball!" );
90- } else {
91- studentIndex = getStudentIndex (firstName , lastName );
92- currentStudent = students .get (studentIndex );
93-
94- IO .print ("" );
95- System .out .println ("The quiz scores for " + currentStudent .getFirstName ()
96- + " " + currentStudent .getLastName () + " are:" );
97- Set <Integer > quizKeys = currentStudent .getQuizKeys ();
98-
99- for (int index : quizKeys ) {
100- System .out .println ("Quiz " + index );
101- IO .print (currentStudent .quizzes .get (index ) + " " );
102- }
103-
104- IO .print ("" );
105- }
64+ case 4 : // View List of Student Quiz Scores
65+ classes .viewQuizScores ();
10666 break ;
10767
108- case 5 :
109- firstName = IO .readString ("Enter the first name: " );
110- lastName = IO .readString ("Enter the last name: " );
111- has = hasStudent (firstName , lastName );
112- if (!has ) {
113- IO .print ("That name isn't there, goofball!" );
114- } else {
115- studentIndex = getStudentIndex (firstName , lastName );
116- currentStudent = students .get (studentIndex );
117-
118- System .out .println ("The average quiz score for " + currentStudent .getFirstName () + " " +
119- currentStudent .getLastName () + " is: " );
120- Set <Integer > quizKeys = currentStudent .getQuizKeys ();
121-
122- int numberOfQuizzes = 0 ;
123- double sumOfQuizzes = 0 ;
124-
125- for (int index : quizKeys ) {
126- numberOfQuizzes ++;
127- sumOfQuizzes += currentStudent .quizzes .get (index );
128- }
129-
130- System .out .println (sumOfQuizzes / numberOfQuizzes );
131- }
68+ case 5 : // View Average Quiz Scores for One Student
69+ classes .viewAveQuizScore ();
70+ break ;
71+
72+ case 6 : // Save Data to File
73+ classes .saveData ();
13274 break ;
133-
13475 }
13576 }
13677 IO .print ("Thanks for playing. See you again!" );
78+ classes .saveData ();
79+ IO .print ("Your data have been saved automatically." );
80+ IO .print ("You're welcome." );
13781 }
138-
139-
140- public boolean hasStudent (String fN , String lN ) {
141- String firstName = fN , lastName = lN ;
142-
143- boolean has = false ;
144-
145- for (int i = 0 ; i < students .size (); i ++) {
146- if (students .get (i ).getFirstName ().equals (firstName ) && students .get (i ).getLastName ().equals (lastName )) {
147- has = true ;
148- }
149- }
150-
151- return has ;
152- }
153-
154- public int getStudentIndex (String fN , String lN ) {
155- String firstName = fN , lastName = lN ;
156-
157- int index = 0 ;
158-
159- for (int i = 0 ; i < students .size (); i ++) {
160- if (students .get (i ).getFirstName ().equals (firstName ) && students .get (i ).getLastName ().equals (lastName )) {
161- index = i ;
162- }
163- }
164-
165- return index ;
166- }
167-
16882}
83+
84+
85+
0 commit comments