Question about exercise with abstract class in Java

I would like to ask for your help again in this exercise. This time using the abstract class.

Create an abstract class FuncionarioAbstract with the attribute String name and abstract method: public double getSalario(); there are two classes that inherit from FuncionarioAbstract and are concrete: Administrador and Gerente. Administrador has a salary of 2000. Gerente already has one more attribute double comissao and your salary is 2500 + comissao.

Create a class Principal with a list of FuncionarioAbstract (creating objects of type Administrador and Gerente) using the interface Set and the class HashSet of the package java.util. And create a method public static void imprimir(Set funcionarios) by printing the name and salary of each employee.

I have done the 3 classes and I am finishing the main one, I will post the code here and I 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 doubt is would be how to use this Set employees to print the name and salary of employees.

Author: Comunidade, 2016-11-17

4 answers

It's just you iterate the HashSet and print what you want

//Chamada do método
imprimir(fa);

//Declaração do método
public static void imprimir(Set<FuncionarioAbstract> funcionarios)
{
    for (FuncionarioAbstract f : funcionarios) {
        System.out.println(String.format("Nome funcionário %s", f.getNome()));
    }
}
 8
Author: LINQ, 2016-11-18 11:19:39

If you are using Java 8 you can iterate as follows:

public void imprimir(Set<? extends FuncionarioAbstract> funcionarios) {
  funcionarios.forEach(this::imprimir);
}

And implement the method that prints individually:

private void imprimir(Funcionario funcionario) {
  NumberFormat formato = NumberFormat.getCurrencyInstance();

  System.out.println(funcionario.getNome() + " " + formato.format(funcionario.getSalario()));
}
 7
Author: Sorack, 2016-11-17 15:16:12

You can overwrite the toString() method of the FuncionarioAbstract class. See an example in this other answer of mine . It would look like this:

@Override
public String toString() {
    return getNome() + " - R$ " + getSalario();
}

So to make the method to print, it is quite easy:

public static void imprimir(Set<? extends FuncionarioAbstract> funcionarios) {
    for (FuncionarioAbstract func : funcionarios) {
        System.out.println(func);
    }
}

You just need to overwrite the getSalario() method. It is an abstract method in FuncionarioAbstract. Class Gerente implements it one way and class Administrador implements it another.

 5
Author: Victor Stafusa, 2017-04-13 12:59:31

Rewriting the toString of the FuncionarioAbstract class, just pass this lambda expression.

fa.forEach(n-> System.out.println(n));
 2
Author: leonardo, 2016-11-17 15:45:28