I would like to ask for your help again in this exercise. This time using the abstract class.
Create an abstract class
FuncionarioAbstract
with theString
name attribute and the abstract method:public double getSalario();
There are two classes that inherit fromFuncionarioAbstract
and are concrete:Administrador
andGerente
.Administrador
has a salary of 2000.Gerente
already has an attribute withdouble comissao
and his salary is2500 + comissao
.Create a
Principal
class with a list ofFuncionarioAbstract
(creating objects of typeAdministrador
andGerente
) using interfaceSet
and classHashSet
of packagejava.util
. And create apublic static void imprimir(Set funcionarios)
method by printing the name and salary of each employee.
I have already done the 3 classes and I am finishing the main one, I will post the code here and would like to know if it is correct.
Class FuncionarioAbstract
:
public abstract class FuncionarioAbstract {
private String nome;
public abstract double getSalario();
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
Class Administrador
:
public class Administrador extends FuncionarioAbstract {
public double getSalario() {
return 2000;
}
}
Class Gerente
:
public class Gerente extends FuncionarioAbstract {
public double getSalario() {
return 2500 + comissao;
}
private double comissao;
public double getComissao() {
return comissao;
}
public void setComissao(double comissao) {
this.comissao = comissao;
}
}
Class Principal
:
public static void main (String[] args) {
Set <FuncionarioAbstract> funcionario = new HashSet<FuncionarioAbstract>();
Gerente funcionario1 = new Gerente();
funcionario1.setNome("Ramon Oliveira");
funcionario1.setComissao(1750);
funcionario.add(funcionario1);
Administrador funcionario2 = new Administrador();
funcionario2.setNome("Eliana Franco");
funcionario.add(funcionario2);
imprimir(funcionario);
}
public static void imprimir(Set<FuncionarioAbstract> funcionario) {
for (FuncionarioAbstract f : funcionario) {
System.out.println("Nome do funcionário: " + f.getNome());
System.out.println("Salário do Funcionário: " + f.getSalario());
}
}
}
My question is would be how to use this Set
employees to print the name and salary of employees.