TP JAVA: Collection d’objets

Supposons que nous travaillons sur une application de gestion d’étudiants dans une école. Nous allons créer trois classes : Student, Course et School.

  1. Classe Student :
import java.util.ArrayList;

public class Student {
    private String name;
    private int age;
    private ArrayList<String> courses;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
        this.courses = new ArrayList<>();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public ArrayList<String> getCourses() {
        return courses;
    }

    public void addCourse(String courseName) {
        courses.add(courseName);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", courses=" + courses +
                '}';
    }
}
  1. Classe Course :
public class Course {
    private String courseName;
    private ArrayList<Student> students;

    public Course(String courseName) {
        this.courseName = courseName;
        this.students = new ArrayList<>();
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public ArrayList<Student> getStudents() {
        return students;
    }

    public void addStudent(Student student) {
        students.add(student);
    }

    @Override
    public String toString() {
        return "Course{" +
                "courseName='" + courseName + '\'' +
                ", students=" + students +
                '}';
    }
}
  1. Classe School :
public class School {
    private String name;
    private ArrayList<Student> students;
    private ArrayList<Course> courses;

    public School(String name) {
        this.name = name;
        students = new ArrayList<>();
        courses = new ArrayList<>();
    }

    public void addStudent(Student student) {
        students.add(student);
    }

    public void removeStudent(Student student) {
        students.remove(student);
    }

    public Student findStudent(String name) {
        for (Student student : students) {
            if (student.getName().equals(name)) {
                return student;
            }
        }
        return null;
    }

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

    public void removeCourse(Course course) {
        courses.remove(course);
    }

    public Course findCourse(String courseName) {
        for (Course course : courses) {
            if (course.getCourseName().equals(courseName)) {
                return course;
            }
        }
        return null;
    }

    public void displayStudents() {
        System.out.println("Students:");
        for (Student student : students) {
            System.out.println(student);
        }
    }

    public void displayCourses() {
        System.out.println("Courses:");
        for (Course course : courses) {
            System.out.println(course);
        }
    }

    public void displayStudentsInCourse(String courseName) {
        System.out.println("Students in " + courseName + ":");
        for (Course course : courses) {
            if (course.getCourseName().equals(courseName)) {
                for (Student student : course.getStudents()) {
                    System.out.println(student.getName());
                }
            }
        }
    }

    public void enrollStudentInCourse(String studentName, String courseName) {
        Student student = findStudent(studentName);
        Course course = findCourse(courseName);

        if (student != null && course != null) {
            student.addCourse(courseName);
            course.addStudent(student);
            System.out.println(studentName + " enrolled in " + courseName + " successfully.");
        } else {
            System.out.println("Student or course not found.");
        }
    }
}

Maintenant, vous pouvez créer des objets de type Student, Course et School, les ajouter à la liste et afficher les détails des étudiants et des cours. Par exemple :

public class Main {
    public static void main(String[] args) {
        School school = new School("My School");

        // Création d'étudiants
        Student student1 = new Student("Alice", 20);
        Student student2 = new Student("Bob", 22);

        // Création de cours
        Course mathCourse = new Course("Math");
        Course historyCourse = new Course("History");

        // Ajout d'étudiants à l'école
        school.addStudent(student1);
        school.addStudent(student2);

        // Ajout de cours à l'école
        school.addCourse(mathCourse);
        school.addCourse(historyCourse);

        // Inscription des étudiants aux cours
        school.enrollStudentInCourse("Alice", "Math");
        school.enrollStudentInCourse("Bob", "History");

        // Affichage des étudiants et des cours
        school.displayStudents();
        school.displayCourses();

        // Affichage des étudiants dans un cours spécifique
        school.displayStudentsInCourse("Math");
        school.displayStudentsInCourse("History");
    }
}