Stuck in Java Hello World *Hell*

Question:
I have been attempting a project for a class on a student management system. After I spent hours debugging I ran the code and the only thing it does is “Hello World”. I have completely deleted my code, working line by line to ensure I didn’t miss anything . I even reached out to my instructor days ago in hopes he’d give me wisdom as I am newbie . Could anyone please let me know what I am doing wrong?

Repl link:
https://replit.com/@jessicataylorja/Program-5-Assignment#AdministratorInterface.java

code snippet
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

class Student {
    private String name;
    private String id;
    private Map<String, Double> enrolledCourses;

    public Student(String name, String id) {
        this.name = name;
        this.id = id;
        this.enrolledCourses = new HashMap<>();
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }

    public Map<String, Double> getEnrolledCourses() {
        return enrolledCourses;
    }

    public void enrollInCourse(Course course) {
        if (enrolledCourses.size() < 5) {
            enrolledCourses.put(course.getCode(), null);
            System.out.println(name + " enrolled in " + course.getName() + ".");
        } else {
            System.out.println("Student cannot enroll in more than 5 courses.");
        }
    }

    public void assignGrade(Course course, double grade) {
        if (enrolledCourses.containsKey(course.getCode())) {
            enrolledCourses.put(course.getCode(), grade);
            System.out.println("Grade assigned to " + name + " for " + course.getName() + ": " + grade);
        } else {
            System.out.println(name + " is not enrolled in " + course.getName() + ".");
        }
    }
}

class Course {
    private String code;
    private String name;
    private int maxCapacity;
    private List<Student> enrolledStudents = new ArrayList<>();
    private static int totalEnrolledStudents = 0;

    public Course(String code, String name, int maxCapacity) {
        this.code = code;
        this.name = name;
        this.maxCapacity = maxCapacity;
    }

    public String getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public int getMaxCapacity() {
        return maxCapacity;
    }

    public List<Student> getEnrolledStudents() {
        return enrolledStudents;
    }

    public static int getTotalEnrolledStudents() {
        return totalEnrolledStudents;
    }

    public void enrollStudent(Student student) {
        if (enrolledStudents.size() < maxCapacity) {
            enrolledStudents.add(student);
            totalEnrolledStudents++;
            System.out.println(student.getName() + " enrolled in " + name + ".");
        } else {
            System.out.println(name + " has reached its maximum capacity.");
        }
    }
}

class CourseManagement {
    public static List<Course> courses = new ArrayList<>();
    public static Map<Student, Double> overallGrades = new HashMap<>();

    public static void addCourse(String code, String name, int maxCapacity) {
        Course newCourse = new Course(code, name, maxCapacity);
        courses.add(newCourse);
        System.out.println("Course " + name + " added successfully.");
    }

    public static void enrollStudent(Student student, Course course) {
        course.enrollStudent(student);
    }

    public static void assignGrade(Student student, Course course, double grade) {
        student.assignGrade(course, grade);
    }

    public static void calculateOverallGrade(Student student) {
        Map<String, Double> grades = student.getEnrolledCourses();
        double overallGrade = grades.values().stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
        overallGrades.put(student, overallGrade);
        System.out.println("Overall grade for " + student.getName() + ": " + overallGrade);
    }
}

public class AdministratorInterface {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("\nAdministrator Menu:");
            System.out.println("1. Add a new course");
            System.out.println("2. Enroll students");
            System.out.println("3. Assign grades");
            System.out.println("4. Calculate overall grades");
            System.out.println("5. Exit");

            System.out.print("Enter your choice: ");
            String choice = scanner.nextLine();

            if (choice.equals("1")) {
                System.out.print("Enter course code: ");
                String code = scanner.nextLine();
                System.out.print("Enter course name: ");
                String name = scanner.nextLine();
                System.out.print("Enter maximum capacity: ");
                int maxCapacity = scanner.nextInt();
                CourseManagement.addCourse(code, name, maxCapacity);
                scanner.nextLine(); // Consume the newline character

            } else if (choice.equals("2")) {
                System.out.print("Enter student name: ");
                String studentName = scanner.nextLine();
                System.out.print("Enter student ID: ");
                String studentId = scanner.nextLine();
                Student student = new Student(studentName, studentId);

                System.out.print("Enter course code to enroll in: ");
                String courseCode = scanner.nextLine();
                Course course = findCourse(courseCode);

                if (course != null) {
                    CourseManagement.enrollStudent(student, course);
                } else {
                    System.out.println("Course with code " + courseCode + " not found.");
                }

            } else if (choice.equals("3")) {
                System.out.print("Enter student ID: ");
                String studentId = scanner.nextLine();
                Student student = findStudent(studentId);

                if (student != null) {
                    System.out.print("Enter course code: ");
                    String courseCode = scanner.nextLine();
                    Course course = findCourse(courseCode);

                    if (course != null) {
                        System.out.print("Enter the grade: ");
                        double grade = scanner.nextDouble();
                        CourseManagement.assignGrade(student, course, grade);
                        scanner.nextLine(); // Consume the newline character
                    } else {
                        System.out.println("Course with code " + courseCode + " not found.");
                    }
                } else {
                    System.out.println("Student with ID " + studentId + " not found.");
                }

            } else if (choice.equals("4")) {
                System.out.print("Enter student ID: ");
                String studentId = scanner.nextLine();
                Student student = findStudent(studentId);

                if (student != null) {
                    CourseManagement.calculateOverallGrade(student);
                } else {
                    System.out.println("Student with ID " + studentId + " not found.");
                }

            } else if (choice.equals("5")) {
                break;

            } else {
                System.out.println("Invalid choice. Please try again.");
            }
        }

        scanner.close();
    }

    private static Course findCourse(String code) {
        return CourseManagement.courses.stream().filter(c -> c.getCode().equals(code)).findFirst().orElse(null);
    }
    private static Student find;   
    private static Student findStudent(String id) {
    return CourseManagement.overallGrades.keySet()
                          .stream()
                          .filter(student -> student.getId().equals(id))
                          .findFirst()
                          .orElse(null);
              }
          }
5 Likes

thank you! I really appreciate it! My code works

Mark the solution that’s helped you, so others know this is solved.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.