Starting an Array of a Private Attribute in Another Class

0

I am doing some exercises and I came across a problem, it follows part of my code:

class Empresa{
public String nome;
public String cnpj;
private Funcionario[] empregados;
public String rua;
public String bairro;
public int livre = 0;

public Funcionario getFuncionario(int posicao){
    return this.empregados[posicao];
}
public Funcionario getEmpregados(){
    return this.empregados = empregados;
}
//resto da classe
//....
//Segunda classe
//
class EmpresaTeste{
public static void main(String[] args){
    Empresa empresa = new Empresa();
    empresa.empregados = new Funcionario[10];
    Funcionario f1 = new Funcionario();
    f1.nome = "Primeiro funcionario";
    f1.departamento = "Faxina";
    f1.setSalario(1000);
    f1.setRg("37263857634");
    f1.data = new Data();
    f1.data.preencheData(12, 11, 2011);
    empresa.adiciona(f1);
    //...
    if(empresa.contem(empresa.getFuncionario(2))){
        System.out.println("");
        System.out.println("");
        System.out.println("Ok");
    }else{
        System.out.println("");
        System.out.println("");
        System.out.println("Funcionario não encontrado");
    }
}}

Well, on the fourth line of EmpresaTeste I'm starting the array array employees of class Empresa , but the attribute is like private , already at the end of class EmpresaTeste if I was able to access the attribute employed through get . How do I access when I have to start an array ?

If I do

empresa.getEmpregados = new Funcionario[10];

It will return me an error.

    
asked by anonymous 14.09.2015 / 01:24

2 answers

5

Probably what you want is this:

public void setEmpregados(Funcionario[] empregados) {
    this.empregados = empregados;
}

But since you will always need to create an array of this type, you can simplify and send only the size you want.

public void setEmpregados(int tamanho) {
    this.empregados = new Funcionario[tamanho];
}

Give up to keep both versions.

Then your get would look like this:

public Funcionario getEmpregados(){
    return this.empregados;
}

The most correct way would be to initialize the array in a builder . You could continue to have these set methods, because you might want to change the value of the array as a whole. If the code leaves the array as an option, unexpected errors may occur.

public Empresa(int tamanho) {
    this(new Funcionario[tamanho]);
}

public Empresa(Funcionario[] empregados) {
    this.empregados = empregados;
}

I would advise changing the array to ArrayList . I know I might slip away from the goal but allow the code to be improved by not requiring the number of employees to be dynamic. And in practice this flexibility is desirable.

With the array , if you want to change the number of employees, you have to get it, create another array with the new size, to the new one and put in the class again. With a list it is possible to do this internally in the class. And it would allow for a guaranteed boot:

private ArrayList<Funcionario> empregados = new ArrayList<Funcionario>();

This dispenses the constructor and a set . Of course you still need to have a method to add and remove employees. But this is also required using an array . I would still need treatments and validations to access the data in this field, just as it was array , but it is a more appropriate solution.

This class is poorly structured. It would be good to rethink some things in it.

I'm not a fan of the term attribute, I prefer field .

    
14.09.2015 / 01:57
2

Each class should handle its own initialization.

If you want to create a company with 10 employees, the idiomatic way of doing this is through the constructor: new Empresa(10) , or new Empresa(new Funcionario[10]) .

public class Empresa {
  public Empresa(int nFuncionarios) {
    this(new Funcionario[nFuncionarios]);
  }

  public Empresa(Funcionario[] funcs) {
    this.funcionarios = funcs;
  }
}

The methods provided by the Empresa class should only allow the update / mutation of your state (eg add employees, remove employees, promote employees, etc.)

One aside: the implementation of the getEmpregados method should be return this.empregados , instead of return this.empregados = empregados .

    
14.09.2015 / 02:01