Course Management System

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Unit 5: Objects and Classes

Computer Science Department: University of the People


CS 1102-01 - AY2024-T3 Programming 1
Ursa Sayeed
March 08, 2024
INTRODUCTION
In this program we create a Course Enrollment and Grade Management System.

We Begin by creating the Student class:


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* The Student class represents a student in the university.
* It stores student information, enrolled courses, and grades.
*/
public class Student {
private String name;
private int id;
private List<Course> enrolledCourses;
private Map<Course, Double> grades;

/**
* Constructs a new Student object with the given name and ID.
*
* @param name the name of the student
* @param id the ID of the student
*/
public Student(String name, int id) {
this.name = name;
this.id = id;
this.enrolledCourses = new ArrayList<>();
this.grades = new HashMap<>();
}

// Getters and setters for name and id

/**
* Enrolls the student in the given course.
*
* @param course the course to enroll in
*/
public void enrollCourse(Course course) {
enrolledCourses.add(course);
}

/**
* Assigns a grade to the student for the given course.
*
* @param course the course for which the grade is assigned
* @param grade the grade to assign
*/
public void assignGrade(Course course, double grade) {
grades.put(course, grade);
}
// Other methods if needed
}
Course Class :
/**
* The Course class represents a course offered by the university.
* It stores course information and tracks the total number of enrolled students.
*/
public class Course {
private String code;
private String name;
private int maxCapacity;
private static int totalEnrolledStudents = 0;

/**
* Constructs a new Course object with the given code, name, and maximum capacity.
*
* @param code the code of the course
* @param name the name of the course
* @param maxCapacity the maximum capacity of the course
*/
public Course(String code, String name, int maxCapacity) {
this.code = code;
this.name = name;
this.maxCapacity = maxCapacity;
}

/**
* Returns the code of the course.
*
* @return the code of the course
*/
public String getCode() {
return code;
}

// Other getters for name and maxCapacity

/**
* Returns the total number of students enrolled across all instances of the Course class.
*
* @return the total number of enrolled students
*/
public static int getTotalEnrolledStudents() {
return totalEnrolledStudents;
}

/**
* Increments the total number of enrolled students by 1.
*/
public static void incrementTotalEnrolledStudents() {
totalEnrolledStudents++;
}
// Other methods if needed
}

CoursManagement Class:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* The CourseManagement class manages courses, student enrollments, and grade assignments.
* It provides static methods for course management and overall grade calculation.
*/
public class CourseManagement {
private static List<Course> courses = new ArrayList<>();
private static Map<Student, Map<Course, Double>> overallGrades = new HashMap<>();

/**
* Adds a new course to the list of courses.
*
* @param code the code of the course
* @param name the name of the course
* @param maxCapacity the maximum capacity of the course
*/
public static void addCourse(String code, String name, int maxCapacity) {
Course course = new Course(code, name, maxCapacity);
courses.add(course);
}

/**
* Enrolls a student in a course.
*
* @param student the student to enroll
* @param course the course to enroll in
*/
public static void enrollStudent(Student student, Course course) {
student.enrollCourse(course);
course.incrementTotalEnrolledStudents();

// Initialize the student's grades map in the overallGrades map


Map<Course, Double> studentGrades = overallGrades.get(student);
if (studentGrades == null) {
studentGrades = new HashMap<>();
overallGrades.put(student, studentGrades);
}
}

/**
* Assigns a grade to a student for a course.
*
* @param student the student to assign the grade to
* @param course the course for which the grade is assigned
* @param grade the grade to assign
*/
public static void assignGrade(Student student, Course course, double grade) {
student.assignGrade(course, grade);
Map<Course, Double> studentGrades = overallGrades.get(student);
studentGrades.put(course, grade);
}

/**
* Calculates the overall grade for a student based on their assigned grades.
*
* @param student the student for which to calculate the overall grade
* @return the overall grade for the student
*/
public static double calculateOverallGrade(Student student) {
Map<Course, Double> studentGrades = overallGrades.get(student);
if (studentGrades == null) {
return 0.0; // No grades assigned for the student
}

double totalGradePoints = 0.0;


int totalCredits = 0;

for (Map.Entry<Course, Double> entry : studentGrades.entrySet()) {


Course course = entry.getKey();
double grade = entry.getValue();
// Assuming 3 credits per course for simplicity
totalGradePoints += grade * 3;
totalCredits += 3;
}

return totalGradePoints / totalCredits;


}

/**
* Finds a course by its code in the list of courses.
*
* @param code the code of the course to find
* @return the Course object if found, null otherwise
*/
public static Course findCourseByCode(String code) {
for (Course course : courses) {
if (course.getCode().equals(code)) {
return course;
}
}
return null;
}
}

AdministratorInterface Class;
import java.util.Scanner;

/**
* The AdminInterface class provides an interactive command-line interface
* for administrators to manage courses, student enrollments, and grade assignments.
*/
public class AdminInterface {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("===========================|");
System.out.println("SELECT AN OPTION::::: |");
System.out.println("===========================|");
System.out.println("1. Add a new course |");
System.out.println("2. Enroll a student |");
System.out.println("3. Assign a grade |");
System.out.println("4. Calculate overall grade |");
System.out.println("5. Exit |");
System.out.println("===========================|");
System.out.println("===========================|");

int choice = scanner.nextInt();


scanner.nextLine(); // Consume the newline character

switch (choice) {
case 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();
scanner.nextLine(); // Consume the newline character
CourseManagement.addCourse(code, name, maxCapacity);
System.out.println("Course added successfully.");
break;
case 2:
System.out.print("Enter student name: ");
String studentName = scanner.nextLine();
System.out.print("Enter student ID: ");
int studentId = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
Student student = new Student(studentName, studentId);
System.out.print("Enter course code: ");
String courseCode = scanner.nextLine();
Course course = findCourseByCode(courseCode);
if (course != null) {
CourseManagement.enrollStudent(student, course);
System.out.println("Student enrolled successfully.");
} else {
System.out.println("Course not found.");
}
break;
case 3:
System.out.print("Enter student name: ");
String studentName2 = scanner.nextLine();
System.out.print("Enter student ID: ");
int studentId2 = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
Student student2 = new Student(studentName2, studentId2);
System.out.print("Enter course code: ");
String courseCode2 = scanner.nextLine();
Course course2 = findCourseByCode(courseCode2);
if (course2 != null) {
System.out.print("Enter grade: ");
double grade = scanner.nextDouble();
scanner.nextLine(); // Consume the newline character
CourseManagement.assignGrade(student2, course2, grade);
System.out.println("Grade assigned successfully.");
} else {
System.out.println("Course not found.");
}
break;
case 4:
System.out.print("Enter student name: ");
String studentName3 = scanner.nextLine();
System.out.print("Enter student ID: ");
int studentId3 = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
Student student3 = new Student(studentName3, studentId3);
double overallGrade = CourseManagement.calculateOverallGrade(student3);
System.out.println("Overall grade for " + studentName3 + " (ID: " + studentId3 + "): " + overallGrade);
break;
case 5:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Try again.");
}
}
}

/**
* Finds a course by its code in the list of courses.
*
* @param code the code of the course to find
* @return the Course object if found, null otherwise
*/
private static Course findCourseByCode(String code) {
return CourseManagement.findCourseByCode(code);
}
}
In this implementation:
1. The Student class has instance variables to store student information and enrolled
courses. It also has a grades map to store grades for each course.
The enrollCourse and assignGrade methods are implemented as per the requirements.
2. The Course class has instance variables to store course information and a static
variable totalEnrolledStudents to keep track of the total number of enrolled students
across all instances. The getTotalEnrolledStudents method is a static method to
retrieve the total number of enrolled students.
3. The CourseManagement class has static variables to store a list of courses and overall
course grades for each student. The addCourse, enrollStudent, assignGrade,
and calculateOverallGrade methods are implemented as static methods.
4. The AdminInterface class provides an interactive command-line interface for
administrators to interact with the system. It displays a menu and prompts the
administrator for necessary inputs, calling the appropriate methods in
the CourseManagement and Student classes to perform the requested operations.
5. Error handling and input validation can be added to handle cases where invalid inputs
are provided or when enrolling students in courses that have reached their maximum
capacity.
6. Comprehensive documentation can be added to explain the purpose and usage of each
class, method, and variable, as well as instructions for running the program and
interacting with the administrator interface.

OUTPUT:

You might also like