Browse Source

refactoring: encapsulated the input handling related with case 9

remotes/origin/fdai7887
fdai7600 11 months ago
parent
commit
6bb64f6a06
  1. 23
      src/main/java/org/example/Administration.java
  2. 16
      src/main/java/org/example/Main.java

23
src/main/java/org/example/Administration.java

@ -529,24 +529,27 @@ public class Administration {
} }
public boolean assignGradeToStudent(String studentId, String courseId, String grade) {
public void assignGradeToStudent(Scanner scanner) {
System.out.print("Enter Student ID: ");
String studentId = scanner.next();
System.out.print("Enter Course ID: ");
String courseId = scanner.next();
System.out.print("Enter Grade: ");
String grade = scanner.next();
Student student = findStudentById(studentId); Student student = findStudentById(studentId);
// First, find the course by ID to ensure it exists in the system.
Course course = findCourseByID(courseId); Course course = findCourseByID(courseId);
if (student != null && course != null) { if (student != null && course != null) {
// Check if the student is enrolled in the specified course.
// Assuming Student class has a method to add or update grade for a course
if (student.getCourseGrades().containsKey(course)) { if (student.getCourseGrades().containsKey(course)) {
// Student is enrolled in the course, proceed to assign the grade.
student.getCourseGrades().put(course, grade); student.getCourseGrades().put(course, grade);
return true;
System.out.println("Grade assigned successfully.");
} else { } else {
// Student is not enrolled in the specified course.
System.out.println("Student is not enrolled in the specified course.");
return false;
System.out.println("Student " + studentId + " is not enrolled in course " + courseId + ".");
} }
} else {
System.out.println("Failed to assign grade. Ensure the student and course IDs are correct.");
} }
return false;
} }
public void dropStudentFromCourse(String studentID, String courseID){ public void dropStudentFromCourse(String studentID, String courseID){

16
src/main/java/org/example/Main.java

@ -86,21 +86,9 @@ public class Main {
case 8: case 8:
administration.viewCourses(); administration.viewCourses();
break; break;
case 9:{
case 9:
// Assign Grade to Student // Assign Grade to Student
System.out.println("Assign Grade to Student selected");
System.out.print("Enter Student ID: ");
String toBeGradedStudentId = scanner.next();
System.out.print("Enter Course ID: ");
String toBeGradedCourseId = scanner.next();
System.out.print("Enter Grade: ");
String grade = scanner.next();
boolean success = administration.assignGradeToStudent(toBeGradedStudentId, toBeGradedCourseId, grade);
if (success) {
System.out.println("Grade assigned successfully.");
} else {
System.out.println("Failed to assign grade. Ensure the student and course IDs are correct.");
}}
administration.assignGradeToStudent(scanner);
break; break;
case 10: case 10:
// View Grades from Student // View Grades from Student

Loading…
Cancel
Save