67 lines
1.7 KiB
Java
67 lines
1.7 KiB
Java
package models;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class Course {
|
|
private static int nextId = 1;
|
|
|
|
private int id;
|
|
private String name;
|
|
private Teacher teacher;
|
|
private List<Student> students;
|
|
|
|
public Course(String name) {
|
|
this.id = nextId++;
|
|
this.name = name;
|
|
this.students = new ArrayList<>();
|
|
}
|
|
|
|
public int getId() { return id; }
|
|
public String getName() { return name; }
|
|
public Teacher getTeacher() { return teacher; }
|
|
|
|
public void setTeacher(Teacher teacher) {
|
|
this.teacher = teacher;
|
|
}
|
|
|
|
public boolean addStudent(Student student) {
|
|
if (students.contains(student)) {
|
|
return false;
|
|
}
|
|
students.add(student);
|
|
return true;
|
|
}
|
|
|
|
public List<Student> getStudents() {
|
|
return students;
|
|
}
|
|
|
|
public void showStudents() {
|
|
if (students.isEmpty()) {
|
|
System.out.println(" No students enrolled yet.");
|
|
} else {
|
|
System.out.println(" Students in " + name + ":");
|
|
for (Student s : students) {
|
|
System.out.println(" - " + s.getUsername());
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if (this == obj) return true;
|
|
if (obj == null || getClass() != obj.getClass()) return false;
|
|
Course course = (Course) obj;
|
|
return id == course.id;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
if (teacher != null) {
|
|
return "[ID:" + id + "] " + name + " (Teacher: " + teacher.getUsername() + ")";
|
|
} else {
|
|
return "[ID:" + id + "] " + name + " (No teacher assigned)";
|
|
}
|
|
}
|
|
} |