Starting an Array of a private attribute in another class

Asked

Viewed 1,460 times

0

I’m doing some exercises and I came across a problem, follow 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");
    }
}}

Yeah, on the fourth line of EmpresaTeste I’m starting the array employees of the class Empresa, but the attribute is as private, at the end of the class EmpresaTeste have a if that I was able to gain access to the employee attribute through the get. How do I gain access when I have to start a array?

If I do

empresa.getEmpregados = new Funcionario[10];

he will return me a mistake.

  • Each class must handle its own initialization. If you want to create a company with 10 employees, the idiomatic way to do it is through the constructor: new Empresa(10), or new Empresa(new Funcionario[10]).

  • Yeah, but like I said, I’m following the exercises.

  • You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? Something needs to be improved?

2 answers

5

Probably what you want would be this:

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

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

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

You can even keep both versions.

There yours 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 keep having these methods set, because you may want to change the value of array as a whole. If the code leaves as option the initialization of the array, 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 by a ArrayList. I know you can get away a little bit from the goal but it would 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 take it, create another array with the new size, copy the required data from the old to the new and put in the class again. With a list it is possible to do this internally in the class. And it would allow a guaranteed boot:

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

I put in the Github for future reference.

Thus dispenses the builder and a set. Of course you would still need to have a method to add and remove employees. But this is also required using a array. Still need treatments and validations to access the data of this field, as if it were array, but it is a more suitable 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.

  • Poorly structured as, friend? I still have working class and date... which has some attributes that were used in the Business class..

  • 1

    There are several things but the fact of having almost everything public is one of them. It is worse to choose only one property to be private. It’s bad because there are no builders. Not having the infrastructure to deal with the array. Have method that does nothing useful. Have inconsistent nomenclatures.

  • Okay, I’ve been following what was in Alura’s exercises, except for the part about defining the access modifiers, I’m going to improve on what you said. Thank you!

  • 2

    Maybe the problem is the quality of the course then.

2

Each class must handle its own initialization.

If you want to create a company with 10 employees, the idiomatic way to do it 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 class Empresa should only allow updating/mutation of your status (e.g., adding employees, removing employees, promoting employees, etc).

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

  • Thank you for your reply, friend! getEmployment implementation error has been corrected.

  • Thank you for your reply, friend! getEmployed implementation error has been corrected. I don’t think I could be very clear on the question, but I was able to solve my question like this : company.setEmployees(new employee[10]); (silly mistake).

Browser other questions tagged

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