Programming Assignment Unit 7
Student Management System
Course .java
// a university course with enrollment functionality
import [Link];
public class Course implements Serializable {
// Course attributes with proper encapsulation
private String courseCode;
private String courseName;
private int maxCapacity;
private int currentEnrollment;
// Static variable to track total enrolled students across all courses
private static int totalEnrolledStudents = 0;
// Constructor initializes course with basic parameters
public Course(String courseCode, String courseName, int maxCapacity) {
[Link] = courseCode;
[Link] = courseName;
[Link] = maxCapacity;
[Link] = 0;
}
// Getters provide controlled access to private fields
// (Teaching point: Encapsulation principles)
public String getCourseCode() { return courseCode; }
public String getCourseName() { return courseName; }
public int getMaxCapacity() { return maxCapacity; }
public int getCurrentEnrollment() { return currentEnrollment; }
public static int getTotalEnrolledStudents() { return totalEnrolledStudents;
}
// Method to handle student enrollment with capacity checking
public boolean enrollStudent() {
// Demonstrates conditional logic and state management
if (currentEnrollment < maxCapacity) {
currentEnrollment++;
totalEnrolledStudents++;
return true; // Return boolean to indicate success/failure
}
return false;
}
// String representation for easy display (Teaching point: toString() method)
@Override
public String toString() {
return courseCode + ": " + courseName;
}
}
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
import [Link];
// Main GUI class for the Course Management System
public class CourseGUI extends JFrame {
// School name constant
private static final String SCHOOL_NAME = "University of the People";
// List to store all students
private static List<Student> students = new ArrayList<>();
// List to store all courses
private static List<Course> courses = new ArrayList<>();
// Constructor to set up the GUI
public CourseGUI() {
setTitle(SCHOOL_NAME + " - Course Management System");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Center window
setLayout(new BorderLayout());
// Header panel with logo and title
JPanel headerPanel = new JPanel();
[Link](new BorderLayout());
[Link]([Link](10, 10, 10, 10));
ImageIcon logo = createImageIcon("uopeople_logo.png", "School Logo");
JLabel logoLabel = new JLabel(logo);
[Link](logoLabel, [Link]);
JLabel appTitle = new JLabel("University of the People Course Management
App");
[Link](new Font("Arial", [Link], 14));
[Link]([Link]);
[Link](appTitle, [Link]);
add(headerPanel, [Link]);
// Tabbed pane for different management panels
JTabbedPane tabbedPane = new JTabbedPane();
[Link](new Font("Arial", [Link], 14));
// Course management panel
JPanel coursePanel = new JPanel();
[Link](new BoxLayout(coursePanel, BoxLayout.Y_AXIS));
[Link]([Link](20, 20, 20, 20));
JLabel courseTitle = new JLabel("Course Management");
[Link](new Font("Arial", [Link], 18));
[Link](courseTitle);
// Button to add a new course
JButton addCourseBtn = new JButton("Add Course");
[Link](new Font("Arial", [Link], 14));
[Link](new Color(101, 31, 118));
[Link]([Link]);
[Link](createImageIcon("[Link]", "Add Course", 16, 16));
[Link]("Add a new course to the system");
[Link](10);
[Link](new Insets(2, 2, 2, 2));
[Link](addCourseBtn);
// Button to enroll a student in a course
JButton enrollBtn = new JButton("Enroll Student");
[Link](new Font("Arial", [Link], 14));
[Link](new Color(101, 31, 118));
[Link]([Link]);
[Link](createImageIcon("[Link]", "Enroll Student", 16, 16));
[Link]("Enroll a student in a course");
[Link](10);
[Link](new Insets(2, 2, 2, 2));
[Link](enrollBtn);
[Link]("Course Management", coursePanel);
// Student management panel
JPanel studentPanel = new JPanel();
[Link](new BoxLayout(studentPanel, BoxLayout.Y_AXIS));
[Link]([Link](20, 20, 20, 20));
JLabel studentTitle = new JLabel("Student Management");
[Link](new Font("Arial", [Link], 18));
[Link](studentTitle);
// Button to add a new student
JButton addStudentBtn = new JButton("Add Student");
[Link](new Font("Arial", [Link], 14));
[Link](new Color(101, 31, 118));
[Link]([Link]);
[Link](createImageIcon("[Link]", "Add Student", 16,
16));
[Link]("Add a new student to the system");
[Link](10);
[Link](new Insets(2, 2, 2, 2));
[Link](addStudentBtn);
[Link]("Student Management", studentPanel);
// Grade management panel
JPanel gradePanel = new JPanel();
[Link](new BoxLayout(gradePanel, BoxLayout.Y_AXIS));
[Link]([Link](20, 20, 20, 20));
JLabel gradeTitle = new JLabel("Grade Management");
[Link](new Font("Arial", [Link], 18));
[Link](gradeTitle);
// Button to assign a grade to a student
JButton assignGradeBtn = new JButton("Assign Grade");
[Link](new Font("Arial", [Link], 14));
[Link](new Color(101, 31, 118));
[Link]([Link]);
[Link](createImageIcon("[Link]", "Assign Grade",
16, 16));
[Link]("Assign a grade to a student for a
course");
[Link](10);
[Link](new Insets(2, 2, 2, 2));
[Link](assignGradeBtn);
// Button to calculate a student's overall grade
JButton calcGradeBtn = new JButton("Calculate Grade");
[Link](new Font("Arial", [Link], 14));
[Link](new Color(101, 31, 118));
[Link]([Link]);
[Link](createImageIcon("[Link]", "Calculate Grade",
16, 16));
[Link]("Calculate a student's overall grade");
[Link](10);
[Link](new Insets(2, 2, 2, 2));
[Link](calcGradeBtn);
[Link]("Grade Management", gradePanel);
add(tabbedPane, [Link]);
// Add action listeners for buttons
[Link](e -> showAddCourseDialog());
[Link](e -> showAddStudentDialog());
[Link](e -> showEnrollDialog());
[Link](e -> showAssignGradeDialog());
[Link](e -> showCalculateGradeDialog());
}
// Helper method to create an ImageIcon with optional scaling
protected ImageIcon createImageIcon(String path, String description, int
width, int height) {
[Link] imgURL = getClass().getResource(path);
if (imgURL != null) {
if (width == 0 || height == 0) {
return new ImageIcon(imgURL, description);
} else {
Image image = new
ImageIcon(imgURL).getImage().getScaledInstance(width, height,
Image.SCALE_SMOOTH);
return new ImageIcon(image, description);
}
} else {
[Link]("Couldn't find file: " + path);
return null;
}
}
// Overloaded helper for ImageIcon without scaling
protected ImageIcon createImageIcon(String path, String description) {
return createImageIcon(path, description, 0, 0);
}
// Static method to show the login dialog before launching the main GUI
public static void showLogin() {
JPanel loginPanel = new JPanel();
[Link](new GridBagLayout());
[Link]([Link]);
[Link]([Link](20, 20, 20, 20));
ImageIcon logo = new CourseGUI().createImageIcon("uopeople_logo.png",
"School Logo");
JLabel logoLabel = new JLabel(logo);
[Link]([Link]);
[Link]([Link](20, 0, 20, 0));
JLabel titleLabel = new JLabel("Course Management Software");
[Link]([Link]);
[Link](new Font("Arial", [Link], 24));
[Link](new Color(51, 0, 102));
[Link]([Link](10, 0, 30, 0));
// Username and password fields with placeholders
JTextField username = new JTextFieldWithPlaceholder("Username");
[Link](new Font("Arial", [Link], 14));
[Link]([Link]);
[Link]([Link]);
[Link]([Link](0, 0, 1, 0,
Color.LIGHT_GRAY));
[Link](new Dimension(300, 40));
JPasswordField password = new JPasswordFieldWithPlaceholder("Password");
[Link](new Font("Arial", [Link], 14));
[Link]([Link]);
[Link]([Link]);
[Link]([Link](0, 0, 1, 0,
Color.LIGHT_GRAY));
[Link](new Dimension(300, 40));
// Login button
JButton loginButton = new JButton("LOG IN");
[Link](new Font("Arial", [Link], 14));
[Link](new Color(51, 0, 102));
[Link]([Link]);
[Link](false);
[Link](false);
[Link](new Dimension(200, 40));
[Link]([Link](Cursor.HAND_CURSOR));
// Layout constraints for login panel
GridBagConstraints gbc = new GridBagConstraints();
[Link] = new Insets(10, 10, 10, 10);
[Link] = 0;
[Link] = 0;
[Link] = 1;
[Link] = [Link];
[Link] = [Link];
[Link](logoLabel, gbc);
[Link] = 1;
[Link](titleLabel, gbc);
[Link] = 2;
[Link](username, gbc);
[Link] = 3;
[Link](password, gbc);
[Link] = 4;
[Link] = new Insets(30, 10, 10, 10);
[Link](loginButton, gbc);
// Modal dialog for login
JDialog loginDialog = new JDialog((Frame)null, "Login", true);
[Link]().add(loginPanel);
[Link]();
[Link](450, 600);
[Link](null);
// Login button action: checks credentials and launches main GUI
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("admin".equals([Link]()) && "admin".equals(new
String([Link]()))) {
[Link]();
CourseGUI courseGUI = new CourseGUI();
[Link]();
[Link](true);
} else {
[Link](
loginDialog,
"Invalid credentials. Please try again.",
"Error",
JOptionPane.ERROR_MESSAGE
);
}
}
});
[Link](true);
}
// Dialog to add a new course
private void showAddCourseDialog() {
JTextField code = new JTextField();
JTextField name = new JTextField();
JTextField cap = new JTextField();
Object[] inputs = {
"Course Code:", code,
"Course Name:", name,
"Max Capacity:", cap
};
int result = [Link](this, inputs, "Add Course",
JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
try {
[Link](new Course([Link](), [Link](),
[Link]([Link]())));
[Link](this, "Course added.");
saveDataToFile();
} catch (Exception ex) {
[Link](this, "Invalid input.");
}
}
}
// Dialog to add a new student
private void showAddStudentDialog() {
JTextField name = new JTextField();
JTextField id = new JTextField();
Object[] inputs = {
"Student Name:", name,
"Student ID:", id
};
int result = [Link](this, inputs, "Add Student",
JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
[Link](new Student([Link](), [Link]()));
[Link](this, "Student added.");
saveDataToFile();
}
}
// Dialog to enroll a student in a course
private void showEnrollDialog() {
Student student = selectStudent();
Course course = selectCourse();
if (student != null && course != null) {
if ([Link]() < [Link]()) {
if ([Link](course)) {
[Link](this, "Enrolled
successfully.");
saveDataToFile();
} else {
[Link](this, "Enrollment failed.");
}
} else {
[Link](this, "Course is full.");
}
}
}
// Dialog to assign a grade to a student for a course
private void showAssignGradeDialog() {
Student student = selectStudent();
Course course = selectCourse();
if (student != null && course != null) {
String input = [Link](this, "Enter grade (0-
100):");
try {
double grade = [Link](input);
if (grade < 0 || grade > 100) {
[Link](this, "Grade must be between 0
and 100.");
} else {
if ([Link](course)) { // Ensure student is
enrolled
if ([Link](course, grade)) {
[Link](this, "Grade
assigned.");
saveDataToFile();
} else {
[Link](this, "Grade assignment
failed. Please ensure the student is enrolled in the course.");
}
} else {
[Link](this, "Student is not
enrolled in the course. Please enroll the student first.");
}
}
} catch (NumberFormatException ex) {
[Link](this, "Invalid grade input. Please
enter a numeric value.");
}
}
}
// Dialog to calculate and show a student's overall grade
private void showCalculateGradeDialog() {
// Prompt the user to select a student from the list
Student student = selectStudent();
// Check if a student was selected
if (student != null) {
// Calculate the overall grade for the selected student
double grade = [Link]();
// If the grade is 0.0, it means there are no grades available for
this student
if (grade == 0.0) {
[Link](this, "No grades available for this
student.");
} else {
// Display the calculated overall grade to the user
[Link](this, "Overall Grade: " + grade);
}
}
}
// Helper to select a student from the list
private Student selectStudent() {
if ([Link]()) {
[Link](this, "No students available.");
return null;
}
String[] options =
[Link]().map(Student::toString).toArray(String[]::new);
String selected = (String) [Link](this, "Select
Student:", "Student Selection",
JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if (selected != null) {
String id = [Link](":")[0];
return [Link]().filter(s ->
[Link]().equals(id)).findFirst().orElse(null);
}
return null;
}
// Helper to select a course from the list
private Course selectCourse() {
if ([Link]()) {
[Link](this, "No courses available.");
return null;
}
String[] options =
[Link]().map(Course::toString).toArray(String[]::new);
String selected = (String) [Link](this, "Select
Course:", "Course Selection",
JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
if (selected != null) {
String code = [Link](":")[0];
return [Link]().filter(c ->
[Link]().equals(code)).findFirst().orElse(null);
}
return null;
}
// Save courses and students to a file for persistence
private void saveDataToFile() {
try (ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("course_data.ser"))) {
[Link](courses);
[Link](students);
[Link](this, "Data saved successfully.");
} catch (IOException e) {
[Link](this, "Error saving data: " +
[Link](), "Error", JOptionPane.ERROR_MESSAGE);
}
}
// Load courses and students from a file
private void loadDataFromFile() {
try (ObjectInputStream in = new ObjectInputStream(new
FileInputStream("course_data.ser"))) {
courses = (List<Course>) [Link]();
students = (List<Student>) [Link]();
[Link](null, "Data loaded successfully.");
} catch (IOException | ClassNotFoundException e) {
[Link](null, "No saved data found. Starting
with empty records.");
}
}
// Main method to launch the application
public static void main(String[] args) {
[Link](() -> {
[Link]();
});
}
}
// Custom JTextField with placeholder text
class JTextFieldWithPlaceholder extends JTextField {
private String placeholder;
public JTextFieldWithPlaceholder(String placeholder) {
[Link] = placeholder;
}
@Override
protected void paintComponent(Graphics g) {
[Link](g);
// Draw placeholder if field is empty
if (getText().isEmpty()) {
Graphics2D g2 = (Graphics2D) [Link]();
[Link](getDisabledTextColor());
[Link](placeholder, getInsets().left, getHeight() / 2 +
[Link]().getAscent() / 2);
[Link]();
}
}
}
// Custom JPasswordField with placeholder text
class JPasswordFieldWithPlaceholder extends JPasswordField {
private String placeholder;
public JPasswordFieldWithPlaceholder(String placeholder) {
[Link] = placeholder;
}
@Override
protected void paintComponent(Graphics g) {
[Link](g);
// Draw placeholder if field is empty
if (getText().isEmpty()) {
Graphics2D g2 = (Graphics2D) [Link]();
[Link](getDisabledTextColor());
[Link](placeholder, getInsets().left, getHeight() / 2 +
[Link]().getAscent() / 2);
[Link]();
}
}
}
[Link]
import [Link].*;
import [Link];
import [Link];
public class CourseManagement {
private static List<Course> courses = new ArrayList<>();
private static List<Student> students = new ArrayList<>();
public static void addCourse(String code, String name, int capacity) {
[Link](new Course(code, name, capacity));
}
public static List<Course> getCourses() {
return courses;
}
public static boolean enrollStudent(Student student, Course course) {
return [Link](course);
}
public static boolean assignGrade(Student student, Course course, double
grade) {
return [Link](course, grade);
}
public static double calculateOverallGrade(Student student) {
double total = 0.0;
int count = 0;
for (Course course : courses) {
Double grade = [Link]().get(course);
if (grade != null) {
total += grade;
count++;
}
}
return count > 0 ? total / count : 0.0;
}
public static void saveDataToFile() {
try (ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("course_data.ser"))) {
[Link](courses);
[Link](students);
} catch (IOException e) {
[Link]("Error saving data: " + [Link]());
}
}
public static void loadDataFromFile() {
try (ObjectInputStream in = new ObjectInputStream(new
FileInputStream("course_data.ser"))) {
courses = (List<Course>) [Link]();
students = (List<Student>) [Link]();
} catch (IOException | ClassNotFoundException e) {
[Link]("No saved data found.");
}
}
}
Student .java
import [Link];
import [Link];
import [Link];
public class Student implements Serializable {
private String name;
private String id;
private Map<Course, Double> enrolledCourses;
public Student(String name, String id) {
[Link] = name;
[Link] = id;
[Link] = new HashMap<>();
}
// Getters
public String getName() { return name; }
public String getId() { return id; }
public Map<Course, Double> getEnrolledCourses() { return enrolledCourses; }
// Enrollment method
public boolean enrollInCourse(Course course) {
if ([Link]() < [Link]()) {
if () {
if ([Link]()) {
[Link](course, null);
return true;
}
}
}
return false;
}
// Grade assignment method
public boolean assignGrade(Course course, double grade) {
if ([Link](course) && grade >= 0.0 && grade <=
100.0) {
[Link](course, grade);
return true;
}
return false;
}
// Grade calculation method
public double calculateOverallGrade() {
double total = 0.0;
int count = 0;
for ([Link]<Course, Double> entry : [Link]()) {
Double grade = [Link]();
if (grade != null) {
total += grade;
count++;
}
}
return count > 0 ? total / count : 0.0;
}
@Override
public String toString() {
return id + ": " + name;
}
}
Main .java
import [Link].*;
public class Main {
public static void main(String[] args) {
[Link](() -> {
// Show the login page
[Link]();
});
}
}
Overview
This document provides comprehensive documentation for the Course Management
System, a Java application designed to manage university courses, students, and grades.
The system allows administrators to add courses and students, enroll students in
courses, assign grades, and calculate overall grades.
Key Components
1. [Link]
Purpose: Represents a university course with enrollment functionality.
Attributes:
courseCode: Unique identifier for the course
courseName: Name of the course
maxCapacity: Maximum number of students allowed in the course
currentEnrollment: Current number of students enrolled in the course
totalEnrolledStudents: Static variable tracking total enrolled students across all courses
Methods:
enrollStudent(): Enrolls a student in the course if capacity allows
toString(): Returns a string representation of the course
2. [Link]
Purpose: Main graphical user interface for the Course Management System.
Key Features:
Tabbed interface for course management, student management, and grade management
Login functionality with admin credentials
Data persistence using serialization
Input validation and error handling
Main Panels:
Course Management: Add new courses
Student Management: Add new students
Grade Management: Assign grades to students and calculate overall grades
3. [Link]
Purpose: Provides utility methods for managing courses and students.
Key Methods:
addCourse(): Adds a new course to the system
enrollStudent(): Enrolls a student in a course
assignGrade(): Assigns a grade to a student for a specific course
calculateOverallGrade(): Calculates a student's overall grade
saveDataToFile(): Saves courses and students to a file
loadDataFromFile(): Loads courses and students from a file
4. [Link]
Purpose: Entry point of the application.
Functionality: Initializes and displays the login screen.
5. [Link]
Purpose: Represents a student with enrollment and grade functionality.
Attributes:
name: Student's name
id: Unique student identifier
enrolledCourses: Map of courses the student is enrolled in with corresponding grades
Methods:
enrollInCourse(): Enrolls the student in a course
assignGrade(): Assigns a grade to the student for a specific course
calculateOverallGrade(): Calculates the student's overall grade
.
Login Functionality
The application requires admin credentials to access the main interface. The default
username and password are both "admin".
Error Handling
The application includes input validation and error messages to guide users in case of
invalid operations or inputs.
Output
Username: Admin
Password: admin
Menu page
Add Course
Course Added
Add Student
Add Student grade
Calculate Grade