-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdministratorInterface.java
More file actions
171 lines (152 loc) · 5.79 KB
/
Copy pathAdministratorInterface.java
File metadata and controls
171 lines (152 loc) · 5.79 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
import java.util.Scanner;
/**
* AdministratorInterface provides the command line menu for admins.
*/
public class AdministratorInterface {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Welcome to the Course Enrollment and Grade Management System!");
boolean running = true;
while (running) {
displayMenu();
int choice = getIntInput("Enter your choice: ");
switch (choice) {
case 1:
addNewCourse();
break;
case 2:
enrollStudentInCourse();
break;
case 3:
assignGradeToStudent();
break;
case 4:
calculateStudentOverallGrade();
break;
case 5:
viewTotalEnrollments();
break;
case 6:
System.out.println("Goodbye!");
running = false;
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
scanner.close();
}
/**
* Displays the main menu.
*/
private static void displayMenu() {
System.out.println("\n========== MAIN MENU ==========");
System.out.println("1. Add a new course");
System.out.println("2. Enroll student in course");
System.out.println("3. Assign grade to student");
System.out.println("4. Calculate overall course grade");
System.out.println("5. View total enrolled students");
System.out.println("6. Exit");
System.out.println("===============================");
}
/**
* Handles adding a new course.
*/
private static void addNewCourse() {
System.out.println("\n--- Add New Course ---");
String code = getStringInput("Enter course code: ");
String name = getStringInput("Enter course name: ");
int capacity = getIntInput("Enter maximum capacity: ");
CourseManagement.addCourse(code, name, capacity);
System.out.println("Course added successfully!");
}
/**
* Handles enrolling a student in a course.
*/
private static void enrollStudentInCourse() {
System.out.println("\n--- Enroll Student ---");
String studentName = getStringInput("Enter student name: ");
String studentId = getStringInput("Enter student ID: ");
Student student = new Student(studentName, studentId);
System.out.println("Available courses:");
for (int i = 0; i < CourseManagement.getCourses().size(); i++) {
Course c = CourseManagement.getCourses().get(i);
System.out.println((i + 1) + ". " + c.getCourseCode() + " - " + c.getName());
}
int courseChoice = getIntInput("Select course number: ") - 1;
if (courseChoice >= 0 && courseChoice < CourseManagement.getCourses().size()) {
Course selected = CourseManagement.getCourses().get(courseChoice);
CourseManagement.enrollStudent(student, selected);
} else {
System.out.println("Invalid course selection.");
}
}
/**
* Handles assigning a grade.
*/
private static void assignGradeToStudent() {
System.out.println("\n--- Assign Grade ---");
String studentName = getStringInput("Enter student name: ");
String studentId = getStringInput("Enter student ID: ");
Student student = new Student(studentName, studentId);
String courseCode = getStringInput("Enter course code: ");
double grade = getDoubleInput("Enter grade (0-100): ");
for (Course c : CourseManagement.getCourses()) {
if (c.getCourseCode().equals(courseCode)) {
CourseManagement.assignGrade(student, c, grade);
return;
}
}
System.out.println("Course not found.");
}
/**
* Handles calculating overall grade.
*/
private static void calculateStudentOverallGrade() {
System.out.println("\n--- Calculate Overall Grade ---");
String studentName = getStringInput("Enter student name: ");
String studentId = getStringInput("Enter student ID: ");
Student student = new Student(studentName, studentId);
double overall = CourseManagement.calculateOverallGrade(student);
System.out.printf("Overall grade for " + student.getName() + ": %.2f%n", overall);
}
/**
* Shows total enrollments across all courses.
*/
private static void viewTotalEnrollments() {
System.out.println("\nTotal enrolled students across all courses: " + Course.getTotalEnrolledStudents());
}
/**
* Helper to get string input.
*/
private static String getStringInput(String prompt) {
System.out.print(prompt);
return scanner.nextLine();
}
/**
* Helper to get integer input with error handling.
*/
private static int getIntInput(String prompt) {
while (true) {
System.out.print(prompt);
try {
return Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Please enter a valid number.");
}
}
}
/**
* Helper to get double input with error handling.
*/
private static double getDoubleInput(String prompt) {
while (true) {
System.out.print(prompt);
try {
return Double.parseDouble(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Please enter a valid number.");
}
}
}
}