1
resposta

[Projeto] Sistema de pontos para alunos

Main :

 public static void main(String[] args) {

        Student student = new Student("Igor");
        student.watchClass();
        student.watchClass();
        student.answerQuiz();
        student.watchClass();
        student.watchClass();
        student.deliverProject();
        System.out.println(student);
    }

Student :

public class Student {
    private String name;
    private Level level;
    private int xp = 0;

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", level=" + level +
                ", xp=" + xp +
                '}';
    }

    public void watchClass(){
        this.xp+=5;
        setLevel();
    }

    public void answerQuiz(){
        this.xp+=10;
        setLevel();
    }

    public void deliverProject(){
        this.xp += 40;
        setLevel();
    }

    public String getName() {
        return name;
    }

    public Level getLevel() {
        return level;
    }

    public int getXp() {
        return xp;
    }

    public void setLevel() {
        if(this.xp<100){
            this.level = Level.LEVEL1;
        }else if(this.xp<200){
            this.level = Level.LEVEL2;
        }else if(this.xp<300){
            this.level = Level.LEVEL3;
        }else if(this.xp<400){
            this.level = Level.LEVEL4;
        }else if(this.xp<500){
            this.level = Level.LEVEL5;
        }else{
            this.level = Level.LEVELMASTER;
        }

    }

Level :

public enum Level {
    LEVEL1,
    LEVEL2,
    LEVEL3,
    LEVEL4,
    LEVEL5,
    LEVELMASTER;
}
1 resposta

Oi, Igor! Como vai?

Do jeito que você explicou, ficou bem claro o cuidado com encapsulamento, mantendo os atributos private e concentrando a regra de mudança de nível dentro da classe Student. A chamada do método setLevel() após cada ação deixa a lógica bem organizada e fácil de entender.

Alura Conte com o apoio da comunidade Alura na sua jornada. Abraços e bons estudos!