Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.6 kB
0
Indexable
Never
package com.example.enum_test;

import jakarta.persistence.*;

import java.util.ArrayList;
import java.util.List;

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;
    @ElementCollection
    @Enumerated(EnumType.STRING)
    @CollectionTable(name = "person_grades", joinColumns = @JoinColumn(name = "person_id"))
    @Column(name = "grade")
    private List<Grade> grades = new ArrayList<>();

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public List<Grade> getGrades() {
        return grades;
    }

    public void setGrades(List<Grade> grades) {
        this.grades = grades;
    }

    public void addGrade(Grade grade) {
        this.grades.add(grade);
    }

    public Person(String name) {
        this.name = name;
    }

    public Person() {
    }

    public double avg() {
        return grades.stream()
                .mapToInt(Grade::getValue)
                .average()
                .orElse(0.0);
    }
}

package com.example.enum_test;

public enum Grade {
    TEN(10),
    NINE(8),
    SEVEN(7),
    SIX(6),
    FAIL(9000);

    private final int value;

    private Grade(int value) {
        this.value = value;
    }

    int getValue() {
        return this.value;
    }

}

Leave a Comment