Files
WS-04-oop-review/src/main/java/models/Teacher.java
T

57 lines
1.5 KiB
Java

package models;
import java.util.ArrayList;
import java.util.List;
public class Teacher extends User {
private List<Course> myCourses;
public Teacher(String username, String password) {
super(username, password);
this.myCourses = new ArrayList<>();
}
public boolean addCourse(Course course) {
if (myCourses.contains(course)) {
return false;
}
myCourses.add(course);
course.setTeacher(this);
return true;
}
public boolean removeCourse(Course course) {
if (myCourses.remove(course)) {
course.setTeacher(null);
return true;
}
return false;
}
public List<Course> getMyCourses() {
return myCourses;
}
public String getMyCoursesDisplay() {
if (myCourses.isEmpty()) {
return " You don't teach any course yet.";
} else {
StringBuilder sb = new StringBuilder("\n My Courses:\n");
for (int i = 0; i < myCourses.size(); i++) {
sb.append(" ").append(i+1).append(". ").append(myCourses.get(i)).append("\n");
}
return sb.toString();
}
}
public boolean checkCourseOwnership(Course course) {
return myCourses.contains(course);
}
public String getStudentsDisplayForCourse(Course course) {
if (!myCourses.contains(course)) {
return "You don't teach this course!";
}
return course.getStudentsDisplay();
}
}