Student Management System

Student Management System

Student Management System implementeret i Java

10/23/2024

.csv fil

First Name Last Name Class Group Courses FAG1 FAG2..(osv)
Mathias Andersen L3b Engelsk B (Aktiv) Matematik A (Aktiv)
Freja Nielsen L3c Fysik B (Aktiv) Programmering B (Aktiv) Matematik A (Aktiv)
Oliver Jensen L3d Programmering B (Afsluttet)
Sofie Petersen L3di Matematik A (Aktiv)
Emil Christensen L3e Engelsk B (Aktiv) Matematik A (Aktiv)
Clara Larsen L3i Kemi B (Aktiv)
Victor Ostergaard 3di Programmering B (Aktiv)
Aske Gud 3di Programmering B (Aktiv) Matematik A (Aktiv)

Klassediagram

Loading...

Kode

package com.victor;

import java.io.*;
import java.util.*;

// State Pattern
interface CourseState {
    void changeState(Course course);
    String getStatus();
}

class ActiveState implements CourseState {
    public void changeState(Course course) {
        course.setState(new CompletedState());
    }
    
    public String getStatus() {
        return "Aktiv";
    }
}

class CompletedState implements CourseState {
    public void changeState(Course course) {
        course.setState(new ActiveState());
    }
    
    public String getStatus() {
        return "Afsluttet";
    }
}

class Course {
    private String name;
    private CourseState state;

    public Course(String name, String status) {
        this.name = name;
        this.state = status.equals("Aktiv") ? new ActiveState() : new CompletedState();
    }

    public void changeState() {
        state.changeState(this);
    }

    public void setState(CourseState state) {
        this.state = state;
    }

    public String getName() {
        return name;
    }

    public String getStatus() {
        return state.getStatus();
    }
}

class Student {
    private String firstName;
    private String lastName;
    private String classGroup;
    private List<Course> courses;

    public Student(String firstName, String lastName, String classGroup) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.classGroup = classGroup;
        this.courses = new ArrayList<>();
    }

    public void addCourse(Course course) {
        courses.add(course);
    }

    // Getters
    public String getFirstName() { return firstName; }
    public String getLastName() { return lastName; }
    public String getClassGroup() { return classGroup; }
    public List<Course> getCourses() { return courses; }
}

class StudentManagementSystem {
    private List<Student> students;

    public StudentManagementSystem() {
        students = new ArrayList<>();
        String currentDir = System.getProperty("user.dir");
        String filePath = currentDir + File.separator + "students.csv";
        loadStudentsFromCSV(filePath);
    }

    private void loadStudentsFromCSV(String filename) {
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            String line;
            br.readLine(); // Skip header
            while ((line = br.readLine()) != null) {
                String[] data = line.split(",");
                System.out.println(data.length);
                Student student = new Student(data[0], data[1], data[2]);
                String[] courses = data[3].split(",");
                
                for (String courseInfo : courses) {
                    String[] courseData = courseInfo.split(" \\(");
                    student.addCourse(new Course(courseData[0], courseData[1].replace(")", "")));
                }
                students.add(student);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void saveStudentsToCSV(String filename) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
            bw.write("First Name,Last Name,Class Group,Courses\n");
            for (Student student : students) {
                bw.write(student.getFirstName() + "," + student.getLastName() + "," + student.getClassGroup() + ",");
                for (Course course : student.getCourses()) {
                    bw.write(course.getName() + " (" + course.getStatus() + "), ");
                }
                bw.write("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void displayAllStudents() {
        for (Student student : students) {
            System.out.println(student.getFirstName() + " " + student.getLastName() + " - " + student.getClassGroup());
        }
    }

    public void searchByCourse(String courseName) {
        for (Student student : students) {
            for (Course course : student.getCourses()) {
                if (course.getName().contains(courseName) && course.getStatus().equals("Aktiv")) {
                    System.out.println(student.getFirstName() + " " + student.getLastName());
                    break;
                }
            }
        }
    }

    public void searchByStudent(String name) {
        for (Student student : students) {
            if (student.getFirstName().contains(name) || student.getLastName().contains(name)) {
                System.out.println(student.getFirstName() + " " + student.getLastName() + " - " + student.getClassGroup());
                System.out.println("Active courses:");
                for (Course course : student.getCourses()) {
                    if (course.getStatus().equals("Aktiv")) {
                        System.out.println("- " + course.getName());
                    }
                }
            }
        }
    }

    public void changeCourseStatus(String studentName, String courseName) {
        for (Student student : students) {
            if (student.getFirstName().contains(studentName) || student.getLastName().contains(studentName)) {
                for (Course course : student.getCourses()) {
                    if (course.getName().contains(courseName)) {
                        course.changeState();
                        System.out.println("Changed status of " + course.getName() + " to " + course.getStatus());
                        saveStudentsToCSV("students.csv");
                        return;
                    }
                }
            }
        }
        System.out.println("Student or course not found.");
    }

    public void addStudent(String firstName, String lastName, String classGroup) {
        students.add(new Student(firstName, lastName, classGroup));
        saveStudentsToCSV("students.csv");
        System.out.println("Added new student: " + firstName + " " + lastName);
    }

    public void addCourseToStudent(String studentName, String courseName, String status) {
        for (Student student : students) {
            if (student.getFirstName().contains(studentName) || student.getLastName().contains(studentName)) {
                student.addCourse(new Course(courseName, status));
                System.out.println("Added " + courseName + " to " + student.getFirstName() + " " + student.getLastName());
                saveStudentsToCSV("students.csv");
                return;
            }
        }
        System.out.println("Student not found.");
    }

    public static void main(String[] args) {
        StudentManagementSystem sms = new StudentManagementSystem();
        try (Scanner scanner = new Scanner(System.in)) {
            while (true) {
                System.out.println("\n1. Display all students");
                System.out.println("2. Search by course");
                System.out.println("3. Search by student");
                System.out.println("4. Change course status");
                System.out.println("5. Add new student");
                System.out.println("6. Add course to student");
                System.out.println("7. Exit");
                System.out.print("Enter your choice: ");

                int choice = scanner.nextInt();
                scanner.nextLine(); // Consume newline

                switch (choice) {
                    case 1:
                        sms.displayAllStudents();
                        break;
                    case 2:
                        System.out.print("Enter course name: ");
                        sms.searchByCourse(scanner.nextLine());
                        break;
                    case 3:
                        System.out.print("Enter student name: ");
                        sms.searchByStudent(scanner.nextLine());
                        break;
                    case 4:
                        System.out.print("Enter student name: ");
                        String studentName = scanner.nextLine();
                        System.out.print("Enter course name: ");
                        String courseName = scanner.nextLine();
                        sms.changeCourseStatus(studentName, courseName);
                        break;
                    case 5:
                        System.out.print("Enter first name: ");
                        String firstName = scanner.nextLine();
                        System.out.print("Enter last name: ");
                        String lastName = scanner.nextLine();
                        System.out.print("Enter class group: ");
                        String classGroup = scanner.nextLine();
                        sms.addStudent(firstName, lastName, classGroup);
                        break;
                    case 6:
                        System.out.print("Enter student name: ");
                        String name = scanner.nextLine();
                        System.out.print("Enter course name: ");
                        String course = scanner.nextLine();
                        System.out.print("Enter course status (Aktiv/Afsluttet): ");
                        String status = scanner.nextLine();
                        sms.addCourseToStudent(name, course, status);
                        break;
                    case 7:
                        System.out.println("Exiting...");
                        System.exit(0);
                    default:
                        System.out.println("Invalid choice. Please try again.");
                }

                //wait 2 seconds before next operation
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}