From 6bb64f6a06888e1eece7a5190210bc0319e808b5 Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Thu, 8 Feb 2024 22:51:27 +0100 Subject: [PATCH] refactoring: encapsulated the input handling related with case 9 --- src/main/java/org/example/Administration.java | 23 +++++++++++-------- src/main/java/org/example/Main.java | 16 ++----------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index 8d34482..d6851e3 100644 --- a/src/main/java/org/example/Administration.java +++ b/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); - // First, find the course by ID to ensure it exists in the system. Course course = findCourseByID(courseId); - 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)) { - // Student is enrolled in the course, proceed to assign the grade. student.getCourseGrades().put(course, grade); - return true; + System.out.println("Grade assigned successfully."); } 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){ diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 4e96cb5..ef9fa77 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -86,21 +86,9 @@ public class Main { case 8: administration.viewCourses(); break; - case 9:{ + case 9: // 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; case 10: // View Grades from Student