From e0c6b49c80868f1ccb537ee48b60ba838986c7be Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Thu, 8 Feb 2024 20:57:49 +0100 Subject: [PATCH 1/5] Added the missing implementation for the Menu elements aside from Case 5 --- src/main/java/org/example/Administration.java | 37 ++++++++++++++++-- src/main/java/org/example/Main.java | 38 ++++++++++++------- src/main/java/org/example/Student.java | 10 +++++ 3 files changed, 68 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index cae9eb6..5e7f8e1 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -1,9 +1,6 @@ package org.example; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Scanner; +import java.util.*; public class Administration { /** @@ -509,6 +506,38 @@ public class Administration { } + + 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; + } + + public void viewCourses() { + if (courses.isEmpty()) { + System.out.println("No courses available."); + } else { + System.out.println("Available Courses:"); + for (Course course : courses) { + course.printCourseInfo(); + } + } + } + } diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index bbc5851..aa3cdb0 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(); @@ -84,23 +81,38 @@ public class Main { break; case 8: // View Courses + administration.viewCourses(); break; 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 + System.out.print("Enter Student ID to view grades: "); + String GradedstudentId = scanner.next(); + Student student = administration.findStudentById(GradedstudentId); + if (student != null) { + student.printGrades(); + } else { + System.out.println("Student not found."); + } break; + case 11: - // View User - break; - case 12: - // Delete User - break; - case 13: - // View Course List - break; - case 14: // Exit (includes saving data in the files) exit = true; System.out.println("Exiting..."); diff --git a/src/main/java/org/example/Student.java b/src/main/java/org/example/Student.java index fd76b0d..2d510ff 100644 --- a/src/main/java/org/example/Student.java +++ b/src/main/java/org/example/Student.java @@ -107,6 +107,16 @@ public class Student { return students; } + public void assignGrade(Course course, String grade) { + // Assuming courseGrades is a Map where the course is the key and the grade is the value + this.courseGrades.put(course, grade); + } + public void printGrades() { + System.out.println("Grades for Student ID: " + this.studentId); + for (Map.Entry entry : courseGrades.entrySet()) { + System.out.println("Course: " + entry.getKey().getCourseName() + ", Grade: " + entry.getValue()); + } + } } From 2e315e56bf6839814217f84f1d6d044abbf47f79 Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Thu, 8 Feb 2024 21:31:48 +0100 Subject: [PATCH 2/5] refactoring: encapsulates the input handling related to enrolling a student in a course --- src/main/java/org/example/Administration.java | 11 +++++++++++ src/main/java/org/example/Main.java | 6 +----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index dd214cc..07f5a16 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -509,6 +509,17 @@ public class Administration { } } + public void enrollStudentInCourseInteractive() { + System.out.println("Enter Student ID:"); + String studentId = scanner.nextLine(); // Use nextLine for consistent input handling + System.out.println("Enter Course ID:"); + String courseId = scanner.nextLine(); + + boolean enrolled = enrollStudentInCourse(studentId, courseId); +// Feedback for failure is handled within the enrollStudentInCourse method + + } + public void viewCourses() { if (courses.isEmpty()) { System.out.println("No courses available."); diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 0bc5252..c22840f 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -40,11 +40,7 @@ public class Main { administration.registerUser(); break; case 2: // Enroll Student in Course - System.out.println("Enter Student ID:"); - String studentId = scanner.next(); - System.out.println("Enter Course ID:"); - String courseId = scanner.next(); - administration.enrollStudentInCourse(studentId, courseId); + administration.enrollStudentInCourseInteractive(); break; case 3: // Enroll Prof to Course From 20d6af9614e0a59d08200f745c971defc5a1aa5f Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Thu, 8 Feb 2024 21:44:27 +0100 Subject: [PATCH 3/5] Implemented cae 10 To view Grades from Student --- src/main/java/org/example/Administration.java | 9 +++++++++ src/main/java/org/example/Main.java | 3 +++ src/main/java/org/example/Student.java | 9 ++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index 07f5a16..8695a8c 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -552,6 +552,15 @@ public class Administration { return false; } + public void viewGradesFromStudent(String studentId) { + Student student = findStudentById(studentId); + if (student != null) { + student.printGrades(); + } else { + System.out.println("Student with ID " + studentId + " not found."); + } + } + } diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index c22840f..c1b1e87 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -96,6 +96,9 @@ public class Main { break; case 10: // View Grades from Student + System.out.print("Enter Student ID to view grades: "); + String studentId = scanner.next(); + administration.viewGradesFromStudent(studentId); break; case 11: diff --git a/src/main/java/org/example/Student.java b/src/main/java/org/example/Student.java index 8cfe740..a0b7074 100644 --- a/src/main/java/org/example/Student.java +++ b/src/main/java/org/example/Student.java @@ -111,6 +111,13 @@ public class Student { this.courseGrades.put(course, grade); } - + public void printGrades() { + System.out.println("Grades for Student ID: " + this.studentId + ", Name: " + this.studentName); + for (Map.Entry entry : courseGrades.entrySet()) { + Course course = entry.getKey(); + String grade = entry.getValue(); + System.out.println("Course: " + course.getCourseName() + " (" + course.getCourseID() + "), Grade: " + (grade != null ? grade : "Not graded yet")); + } + } } From c80a4dd63634ef3681c3ce2f6ef195ea3fb824a5 Mon Sep 17 00:00:00 2001 From: fdai7600 Date: Thu, 8 Feb 2024 21:49:36 +0100 Subject: [PATCH 4/5] refactoring: encapsulated the input handling related with case 10 --- src/main/java/org/example/Administration.java | 5 ++++- src/main/java/org/example/Main.java | 4 +--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/example/Administration.java b/src/main/java/org/example/Administration.java index 8695a8c..e44611f 100644 --- a/src/main/java/org/example/Administration.java +++ b/src/main/java/org/example/Administration.java @@ -552,8 +552,11 @@ public class Administration { return false; } - public void viewGradesFromStudent(String studentId) { + public void viewGradesFromStudent() { + System.out.print("Enter Student ID to view grades: "); + String studentId = scanner.next(); Student student = findStudentById(studentId); + if (student != null) { student.printGrades(); } else { diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index c1b1e87..8c45a49 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -96,9 +96,7 @@ public class Main { break; case 10: // View Grades from Student - System.out.print("Enter Student ID to view grades: "); - String studentId = scanner.next(); - administration.viewGradesFromStudent(studentId); + administration.viewGradesFromStudent(); break; case 11: From be76e39089db567b11fff63be674464a8d549eaa Mon Sep 17 00:00:00 2001 From: Tobias Herbert Date: Thu, 8 Feb 2024 21:52:55 +0100 Subject: [PATCH 5/5] test: Added testEnrollStudentInteractive Function --- .../java/org/example/AdministrationTest.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/test/java/org/example/AdministrationTest.java b/src/test/java/org/example/AdministrationTest.java index 0397e89..f376603 100644 --- a/src/test/java/org/example/AdministrationTest.java +++ b/src/test/java/org/example/AdministrationTest.java @@ -3,6 +3,7 @@ package org.example; import org.junit.jupiter.api.Test; import java.util.List; +import java.util.Map; import static org.junit.jupiter.api.Assertions.*; @@ -418,4 +419,32 @@ class AdministrationTest { assertEquals("Student",administration.findStudentById("S1010").getStudentRole()); } + + @Test + void testEnrollStudentInCourseInteractive() { + Administration administration = new Administration(); + + Student stud = sampleStudent; + administration.addStudents(stud); + Course course = sampleCourse; + administration.addCourse(course); + String studentId =stud.getStudentId(); // Use nextLine for consistent input handling + String courseId = course.getCourseID(); + + boolean enrolled = administration.enrollStudentInCourse(studentId, courseId); + + boolean correct = false; + Map courses = stud.getCourseGrades(); + + for(Course key : courses.keySet()) + { + if(key == course) + { + correct = true; + break; + } + } + + assertTrue(correct); + } }