From b6948da1ebb9d8d25dfe385567343e4c4d197925 Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Thu, 8 Feb 2024 20:01:26 +0100 Subject: [PATCH 1/4] Adjusted the implementation of Cases 6-9 from the new menu to the older ones --- src/main/java/org/example/Administration.java | 64 +++++++++++++++++++ src/main/java/org/example/Main.java | 17 ++++- src/main/java/org/example/Student.java | 4 ++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index bbd7f83..470ca9f 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -407,6 +407,70 @@ public class Administration { } + + public void createCourse(String courseName, String courseID, int credits) { + // Check if a course with the given ID already exists + if (findCourseByID(courseID) != null) { + System.out.println("A course with ID " + courseID + " already exists."); + } else { + Course newCourse = new Course(courseName, courseID, credits); + courses.add(newCourse); + System.out.println("Course " + courseName + " (" + courseID + ") created successfully."); + } + } + + public boolean enrollStudentInCourse(String studentId, String courseId) { + Student student = findStudentById(studentId); + Course course = findCourseByID(courseId); + if (student != null && course != null) { + // If the course is not already enrolled + if (!student.getCourseGrades().containsKey(course)) { + student.getCourseGrades().put(course, "Not Graded"); + System.out.println("Student " + studentId + " enrolled in course " + courseId + "."); + return true; + } else { + System.out.println("Student " + studentId + " is already enrolled in course " + courseId + "."); + return false; + } + } else { + if (student == null) System.out.println("Student ID not found."); + if (course == null) System.out.println("Course ID not found."); + return false; + } + } + + public void viewCourses() { + if (courses.isEmpty()) { + System.out.println("No courses available."); + } else { + System.out.println("Available Courses:"); + for (Course course : courses) { + course.printCourseInfo(); + } + } + } + + + public boolean assignGradeToStudent(String studentId, String courseId, String grade) { + 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. + if (student.getCourseGrades().containsKey(course)) { + // Student is enrolled in the course, proceed to assign the grade. + student.getCourseGrades().put(course, grade); + return true; + } else { + // Student is not enrolled in the specified course. + System.out.println("Student is not enrolled in the specified course."); + return false; + } + } + return false; + } + } diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index f41f76d..1566a84 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -58,10 +58,23 @@ public class Main { // Delete Course break; case 8: - // View Courses + 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."); + }} break; case 10: // View Grades from Student diff --git a/src/main/java/org/example/Student.java b/src/main/java/org/example/Student.java index fd76b0d..8cfe740 100644 --- a/src/main/java/org/example/Student.java +++ b/src/main/java/org/example/Student.java @@ -107,6 +107,10 @@ public class Student { return students; } + public void assignGrade(Course course, String grade) { + this.courseGrades.put(course, grade); + } + } From 38977b1f093b1c9b558029aee77b4b5fce56cc8f Mon Sep 17 00:00:00 2001 From: fdai7887 Date: Thu, 8 Feb 2024 20:37:07 +0100 Subject: [PATCH 2/4] Added test for dropFromCourse Method in AdministrationTest class --- .../java/org/example/AdministrationTest.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/test/java/org/example/AdministrationTest.java b/src/test/java/org/example/AdministrationTest.java index 647c024..0397e89 100644 --- a/src/test/java/org/example/AdministrationTest.java +++ b/src/test/java/org/example/AdministrationTest.java @@ -324,6 +324,33 @@ class AdministrationTest { } + + @Test + void dropProfFromCourse(){ + Professor prof = sampleProf; + Course course = sampleCourse; + List courseList = prof.getCoursesTaught(); + courseList.add(course); + prof.setCoursesTaught(courseList); + + //The "User input" to determine the professor + String profID = prof.getProfessorID(); + + //Grabs the coursesTaught List from the Professor the user wants to enroll + List courseList2 = prof.getCoursesTaught(); + //Assigning the course to the Prof by adding it the to the courseList + courseList2.remove(course); + //The Professor gets the updated courseList + prof.setCoursesTaught(courseList); + + + //Checks if the course got removed + assertEquals(0,prof.getCoursesTaught().size() ); + + } + + + @Test void registerUser() { Administration administration = new Administration(); From bf2260f2b7919bb7b66e8b00b0ef81fd1afaa609 Mon Sep 17 00:00:00 2001 From: fdai7887 Date: Thu, 8 Feb 2024 20:51:52 +0100 Subject: [PATCH 3/4] refactoring: removed redundancies from Menu in Main class --- src/main/java/org/example/Main.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index a5ebe9f..3c59a3c 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -72,15 +72,9 @@ public class Main { // View Grades from Student break; case 11: - // View User - break; - case 12: // Delete User break; - case 13: - // View Course List - break; - case 14: + case 12: // Exit (includes saving data in the files) exit = true; System.out.println("Exiting..."); From 3744102661584db7487818a901973261f644d488 Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Thu, 8 Feb 2024 21:10:44 +0100 Subject: [PATCH 4/4] refactoring: follow up on the redundancy on the Menu (element 11-13) --- src/main/java/org/example/Administration.java | 32 ------------------- src/main/java/org/example/Main.java | 9 ++---- 2 files changed, 2 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index e613922..dd214cc 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -541,38 +541,6 @@ public class Administration { return false; } - - public void createCourse(String courseName, String courseID, int credits) { - // Check if a course with the given ID already exists - if (findCourseByID(courseID) != null) { - System.out.println("A course with ID " + courseID + " already exists."); - } else { - Course newCourse = new Course(courseName, courseID, credits); - courses.add(newCourse); - System.out.println("Course " + courseName + " (" + courseID + ") created successfully."); - } - } - - public boolean enrollStudentInCourse(String studentId, String courseId) { - Student student = findStudentById(studentId); - Course course = findCourseByID(courseId); - if (student != null && course != null) { - // If the course is not already enrolled - if (!student.getCourseGrades().containsKey(course)) { - student.getCourseGrades().put(course, "Not Graded"); - System.out.println("Student " + studentId + " enrolled in course " + courseId + "."); - return true; - } else { - System.out.println("Student " + studentId + " is already enrolled in course " + courseId + "."); - return false; - } - } else { - if (student == null) System.out.println("Student ID not found."); - if (course == null) System.out.println("Course ID not found."); - return false; - } - } - } diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 2544299..0bc5252 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -30,10 +30,7 @@ public class Main { System.out.println("8. View Courses"); System.out.println("9. Assign Grade to Student"); System.out.println("10. View Grades from Student"); - System.out.println("11. View User"); - System.out.println("12. Delete User"); - System.out.println("13. View Course List"); - System.out.println("14. Exit (includes saving data in the files)"); + System.out.println("11. Exit (includes saving data in the files)"); System.out.print("Select an option: "); int choice = scanner.nextInt(); @@ -104,10 +101,8 @@ public class Main { case 10: // View Grades from Student break; + case 11: - // Delete User - break; - case 12: // Exit (includes saving data in the files) exit = true; System.out.println("Exiting...");