6
6
package StudentQuizScores ;
7
7
8
8
import java .util .ArrayList ;
9
- import java .util .HashMap ;
10
9
import java .util .List ;
11
- import java .util .Map ;
12
- import java .util .Set ;
13
10
import java .util .Random ;
14
11
15
12
/**
@@ -29,8 +26,11 @@ public void displayMenu() {
29
26
int userChoice = 0 , studentIndex = -1 ;
30
27
String firstName , lastName ;
31
28
boolean has ;
29
+ Classes classes = new Classes ();
32
30
33
- while (userChoice != 6 ) {
31
+ classes .readData ();
32
+
33
+ while (userChoice != 7 ) {
34
34
35
35
IO .print ("" );
36
36
IO .print ("Choose your Quest: " );
@@ -39,130 +39,47 @@ public void displayMenu() {
39
39
IO .print ("3: Remove Student" );
40
40
IO .print ("4: View List of Student Quiz Scores" );
41
41
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" );
43
44
44
- userChoice = IO .readInt ("" , 1 , 6 );
45
+ userChoice = IO .readInt ("" , 1 , 7 );
45
46
46
47
switch (userChoice ) {
47
- case 1 :
48
+ case 1 : //View Student List
48
49
IO .print ("The Students are:" );
49
50
IO .print ("=================" );
50
- for (Student currentStudent : students ) {
51
+ for (currentStudent : students ) {
51
52
System .out .println (currentStudent .getFirstName () + " " + currentStudent .getLastName ());
52
53
}
53
54
break ;
54
55
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 ();
69
58
break ;
70
59
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 ();
82
62
break ;
83
63
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 ();
106
66
break ;
107
67
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 ();
132
74
break ;
133
-
134
75
}
135
76
}
136
77
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." );
137
81
}
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
-
168
82
}
83
+
84
+
85
+
0 commit comments