Skip to content
Snippets Groups Projects
CoursesOfStudy.java 3.13 KiB
Newer Older
package de.thdeg.grademanager.model;
Kevin Thaller's avatar
Kevin Thaller committed

Kevin Thaller's avatar
Kevin Thaller committed
import jakarta.persistence.*;
Kevin Thaller's avatar
Kevin Thaller committed
@Entity
@Table(name = "coursesOfStudy")
Kevin Thaller's avatar
Kevin Thaller committed
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
Kevin Thaller's avatar
Kevin Thaller committed
    private int id;

Kevin Thaller's avatar
Kevin Thaller committed
    private String name;

Kevin Thaller's avatar
Kevin Thaller committed
    private String degree;

    @Column(name = "duration")
Kevin Thaller's avatar
Kevin Thaller committed
    private int duration;

Kevin Thaller's avatar
Kevin Thaller committed
    private int fees;

    @Column(name = "field_of_study")
Kevin Thaller's avatar
Kevin Thaller committed
    private String fieldOfStudy;

    @OneToMany(
            mappedBy = "coursesOfStudy",
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    private List<Course> courses = new ArrayList<>();
Kevin Thaller's avatar
Kevin Thaller committed
    @ManyToOne
    @JoinColumn(name = "courses_of_study_id")
    private CoursesOfStudy coursesOfStudy;
Kevin Thaller's avatar
Kevin Thaller committed
    public List<Course> getCourses() {
        return courses;
    }

    /*
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "courses_of_study_id")
    private CoursesOfStudy coursesOfStudy;
    */
Kevin Thaller's avatar
Kevin Thaller committed

    }

    public CoursesOfStudy(String name, String degree, int duration, int fees, String fieldOfStudy) {
        this.name = name;
Kevin Thaller's avatar
Kevin Thaller committed
        this.degree = degree;
        this.duration = duration;
        this.fees = fees;
        this.fieldOfStudy = fieldOfStudy;
Kevin Thaller's avatar
Kevin Thaller committed
    }
Kevin Thaller's avatar
Kevin Thaller committed
    @Override
Kevin Thaller's avatar
Kevin Thaller committed
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CoursesOfStudy that = (CoursesOfStudy) o;
        return id == that.id;
Kevin Thaller's avatar
Kevin Thaller committed
    @Override
Kevin Thaller's avatar
Kevin Thaller committed
        return Objects.hash(id);
    }

Kevin Thaller's avatar
Kevin Thaller committed
    @Override
    public String toString() {
        return name;
    }

Kevin Thaller's avatar
Kevin Thaller committed
        return id;
    }
Kevin Thaller's avatar
Kevin Thaller committed
        this.id = id;
    }

Kevin Thaller's avatar
Kevin Thaller committed
        this.name = name;
    }

Kevin Thaller's avatar
Kevin Thaller committed
        return degree;
    }

    public void setDegree(String degree) {
Kevin Thaller's avatar
Kevin Thaller committed
        this.degree = degree;
    public void setDuration(int duration) {
Kevin Thaller's avatar
Kevin Thaller committed
        this.duration = duration;
Kevin Thaller's avatar
Kevin Thaller committed
        return fees;
Kevin Thaller's avatar
Kevin Thaller committed
        this.fees = fees;
    }
Kevin Thaller's avatar
Kevin Thaller committed
        return fieldOfStudy;
    }
    public void setFieldOfStudy(String fieldOfStudy) {
Kevin Thaller's avatar
Kevin Thaller committed
        this.fieldOfStudy = fieldOfStudy;
    public void addCourse(Course course) {
Kevin Thaller's avatar
Kevin Thaller committed
        courses.add(course);
        course.setCoursesOfStudy(this);
Kevin Thaller's avatar
Kevin Thaller committed

    public void removeCourse(Course course) {
Kevin Thaller's avatar
Kevin Thaller committed
        courses.remove(course);
        course.setCoursesOfStudy(null);
Kevin Thaller's avatar
Kevin Thaller committed
    }

    public void updateCourse(Course course){
        int index = findCourseID(course);
        courses.set(index, course);
    }

    private int findCourseID(Course course){
        for (int i = 0; i < courses.size(); i++) {
            if (courses.get(i).getId() == course.getId()){
                return i;
            }
        }
        return -1;
    }