Manipulating class within another class and saving in array

Asked

Viewed 1,402 times

-1

I created a class Pessoa and in it an attribute endereco, only I made a class Endereco to register more than one endereco per person.

In that other class Endereco has id and nome da rua, and I can’t manipulate them from class Pessoa.

I want to save you both together in one array.

public static void main(String[] args) {

        ArrayList<Pessoa> pessoa = new ArrayList<>();

        Pessoa c1 = new Pessoa();
        c1.setId(1);
        c1.setNome("Pedro");
        c1.setEndereco(endereco);//Erro, nao consigo manipular para por o nome da rua e nem o id

        System.out.println(c1.getEndereco());

        pessoa.add(c1);
}

public class Pessoa {

    private int id;
    private String nome;
    private Endereco endereco;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Endereco getEndereco() {
        return endereco;
    }

    public void setEndereco(Endereco endereco) {
        this.endereco = endereco;
    }

}

public class Endereco {

    private int id;
    private String rua;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRua() {
        return rua;
    }

    public void setRua(String rua) {
        this.rua = rua;
    }

}
  • You declare the variable endereco where in the main function? You wouldn’t have to create an address Arraylist and a Person object?

  • Like, I want an array that can register several addresses if needed. Plus the Address that is in another class can be saved in the right sequence of the person array.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

1

First let’s agree that what you call an attribute is actually called a field.

You have to create an object Endereco in your code and then pass that object to the object Pessoa, nor would you need to create a variable that you created but not only didn’t put a value on it, but didn’t state it. Of course, in the form you are doing the variable will be necessary, but just because all the code is conceptually wrong, for example you should use builder (I will not go into the abuse of getter and mainly Setter, but search right here on the site on the subject).

It would look something like this:

var endereco = new Endereco();
endereco.setId(1); // sei lá se isso deveria existir e se deveria ser livre assim
endereco.setRua("rua");
c1.setEndereco(endereco);

I put in the Github for future reference.

Address does not have only street, this is another rather wrong concept. Of course it matters little for exercise but it’s good to know. Actually people are wrong a lot about addresses.

If you want an object to register a person lists, then the variable should be called pessoas, right?

If you want to register more than one address per person then you have to have a field with an address list, which makes the code very different from the one you are using, and then you need to review these setters and probably getters. That’s why I say never use a concept you don’t understand, just because people are using it, maybe they don’t understand why they used it, or they can understand and do it right, while others won’t get the same.

It’s really hard to keep cohesion and low coupling with what you want to do by maintaining OOP precepts. Then the class Pessoa should only deal with this array, indirectly to not expose implementation details and the class Endereco take care of all the rest of it.

  • My idea was to do so Person C1 = new Person(1, "Peter", address); .

  • yeah, it’d be better that way, but that’s not what you did.

Browser other questions tagged

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