-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudentMenu
184 lines (157 loc) · 6 KB
/
StudentMenu
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
import java.util.ArrayList;
import java.util.Scanner;
/**
* This class allows a user to manage a database of students.
*/
public class StudentMenu {
private StudentManager studentManager;
private Scanner scanner;
/**
* This constructor creates a StudentMenu object.
*/
public StudentMenu() {
studentManager = new StudentManager();
scanner = new Scanner(System.in);
}
/**
* This method displays the user menu.
*/
public void displayMenu() {
System.out.println("1. Add Student");
System.out.println("2. Remove Student");
System.out.println("3. Modify Student");
System.out.println("4. Display Students");
System.out.println("5. Display Students final grades");
System.out.println("6. Exit");
}
/**
* This method gets the user's choice.
*
* @return The user's choice.
*/
public int getChoice() {
return scanner.nextInt();
}
/**
* This method allows the user to add a student to the database.
*/
public void addStudent() {
System.out.println("Enter the student's first name:");
String firstName = scanner.next();
System.out.println("Enter the student's last name:");
String lastName = scanner.next();
System.out.println("Enter the student's ID:");
String id = scanner.next();
scanner.nextLine();
// Prompt the user to enter the quiz grades
System.out.println("Enter the student's quiz grades (separated by spaces):");
ArrayList<Double> quizGrades = new ArrayList<>();
String[] quizGradesStr = scanner.nextLine().trim().split("\\s+");
for (String grade : quizGradesStr) {
if (!grade.isEmpty()) {
quizGrades.add(Double.parseDouble(grade));
}
}
// Prompt the user to enter the lab grades
System.out.println("Enter the student's lab grades (separated by spaces):");
ArrayList<Double> labGrades = new ArrayList<>();
String[] labGradesStr = scanner.nextLine().trim().split("\\s+");
for (String grade : labGradesStr) {
if (!grade.isEmpty()) {
labGrades.add(Double.parseDouble(grade));
}
}
// Prompt the user to enter the project grade
System.out.println("Enter the student's project grade:");
double projectGrade = scanner.nextDouble();
// Prompt the user to enter the midterm grade
System.out.println("Enter the student's midterm grade:");
double midtermGrade = scanner.nextDouble();
// Prompt the user to enter the final grade
System.out.println("Enter the student's final grade:");
double finalGrade = scanner.nextDouble();
// Prompt the user to enter the attendance grade
System.out.println("Enter the student's attendance grade:");
double attendanceGrade = scanner.nextDouble();
// Create the student and add them to the database
StudentGrades grades = new StudentGrades(quizGrades, labGrades, projectGrade, midtermGrade, finalGrade, attendanceGrade);
Student student = new Student(firstName, lastName, id, grades);
studentManager.addStudent(student);
System.out.println("Student added.");
}
/**
* This method removes a student from the database.
*/
public void removeStudent() {
System.out.println("Enter the student's ID:");
String id = scanner.next();
Student student = studentManager.getStudent(id);
studentManager.removeStudent(student);
System.out.println("Student removed.");
}
/**
* This method modifies a student's information.
*/
public void modifyStudent() {
System.out.println("Enter the student's ID:");
String id = scanner.next();
Student student = studentManager.getStudent(id);
System.out.println("Enter the student's new first name:");
String firstName = scanner.next();
System.out.println("Enter the student's new last name:");
String lastName = scanner.next();
studentManager.modifyStudent(student, firstName, lastName, id);
}
/**
* This method displays all students in the database.
*/
public void displayStudents() {
ArrayList<Student> students = studentManager.getStudents();
for (Student student : students) {
System.out.println(student);
}
}
/**
* This method searches for a student in the database.
* and displays their final grade.
*/
public void searchStudent() {
scanner.nextLine();
System.out.println("Enter the student's first name or ID:");
String searchInput = scanner.nextLine();
Student student = studentManager.getStudent(searchInput);
System.out.println(student + "\nStudent: " + student.getFirstName() + "\n has a total grade average of " + student.getGrades().getTotalGradeAverage() + ",\n a letter grade of " + student.getGrades().getLetterGrade() + ",\n and a GPA of " + student.getGrades().getGPA() + ".");
}
/**
* This method runs the program.
*/
public void run() {
int choice = 0;
while (choice != 6) {
displayMenu();
choice = getChoice();
switch (choice) {
case 1:
addStudent();
break;
case 2:
removeStudent();
break;
case 3:
modifyStudent();
break;
case 4:
displayStudents();
break;
case 5:
searchStudent();
break;
case 6:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice.");
}
}
}
}