Java Aggregation

0

I would like to create a method in my Teacher class that calculates your salary based on your workload , which is an established function in the Discipline class, a teacher can teach a variety of subjects, the hourly loads of each discipline he teaches were accumulated in a variable, so that from there he could multiply by the value of the hour class and get his salary, however, the way I'm trying to do is giving error, help me please :

  

Class Teacher




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;
    }
}

    
asked by anonymous 07.06.2017 / 13:18

2 answers

0

There are 2 errors in your code

The first is that to calculate the time load you are incrementing 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 teacher's workload it is necessary to loop through all disciplines

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

    
07.06.2017 / 14:23
0

You should first change to the list of Teacher Disciplines, as a teacher can teach a variety of 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 value of the class time you can assign in a constant or if you prefer you can create a variable to define it dynamically.

    
07.06.2017 / 14:16