Aggregation in Java

Asked

Viewed 1,543 times

0

I would like to create a method in my class Teacher that would calculate your salary based on your workload, which is a function established in the Discipline class, a teacher can teach several disciplines, so I would like the workloads of each discipline he teaches to be accumulated in one variable, so that from then on I could multiply by the value of the hour lesson and get your salary, however, the way I’m trying to do is giving error, please help me:

teacher class




package agregacao;


public class Professor {

    private String nome;
    private double cpf, salario;
    private Disciplina disciplina;

    public Professor(String nome, double cpf, Disciplina disciplina) {
        this.nome = nome;
        this.cpf = cpf;
        this.disciplina = disciplina;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getNome() {
        return this.nome;
    }

    public void setCpf(double cpf) {
        this.cpf = cpf;
    }

    public double getCpf() {
        return this.cpf;
    }

    public void calcSalario() {
        int carga += this.disciplina.getCargaHoraria();//O erro está nessa linha
    }
}

class Discipline



package agregacao;

public class Disciplina {

    private String nomeDisciplina;
    private int cargaHoraria;

    public void setNomeDisciplina(String nomeDisciplina) {
        this.nomeDisciplina = nomeDisciplina;
    }

    public String getNomeDisciplina() {
        return this.nomeDisciplina;
    }

    public void setCargaHoraria(int cargaHoraria) {
        this.cargaHoraria = cargaHoraria;
    }

    public int getCargaHoraria() {
        return this.cargaHoraria;
    }
}

2 answers

1

First you should change to the list of Teacher Disciplines, since a teacher can teach several subjects:

private List<Disciplina> disciplinas = new ArrayList<>();

You can create a method in the Teacher class to include the disciplines, and invoke the method to calculate the salary.

public void addDisciplina(Disciplina disciplina){
    this.getDisciplinas().add(disciplina);
}

public void calcularSalario() {
    int valorHoraAula = 10;
    for(Disciplina disciplina : getDisciplinas()){
        this.salario += disciplina.getCargaHoraria() * valorHoraAula;
    }
}

The lesson time value you can assign in a constant or if you prefer you can create a variable to define it dynamically.

  • but with me access my method of Discipline getCargaHoraria(); thus?

1

There are 2 errors in your code

The first is that to calculate the workload you are increasing the load variable, but it has not been declared, nor given an initial value to it

The second is that as a teacher has several disciplines, you have to keep in the Teacher Class a list of disciplines

Dai to calculate the teacher’s workload it is necessary to traverse a loop of all subjects

public class Professor {

    private String nome;
    private double cpf, salario;
    private ArrayList<Disciplina> disciplinaLista = new ArrayList<Disciplina>();

    public Professor(String nome, double cpf) { //Você pode passar um arraylist pelo construtor se preferir também
        this.nome = nome;
        this.cpf = cpf;
    }

    //Demais métodos...

    public addDisciplina(Disciplina disciplina){
         disciplinaLista.add(disciplina);
    }
    public removeDisciplina(Disciplina disciplina){
         disciplinaLista.remove(disciplina);
    }

    public void calcSalario() {
        int carga = 0;
        for(Disciplina disciplina : disciplinaLista){ // Para cada disciplina na lista de disciplinaLista
             carga += disciplina.getCargaHoraria();
        }

    }
}

Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.