A person can have multiple phones. How to do this with vector and not with list?

Asked

Viewed 590 times

1

I’m having trouble connecting Telefone with class Pessoa, a person can have up to three phones. How does this?

package model;

public class Pessoa {

private String nome;
private String endereco;
private String sobrenome;
private Telefone tels[];


public Telefone[] getTels() {
    return tels;
}

public void setTels(Telefone[] tels) {
    this.tels = tels;
}

public String getNome() {
    return nome;
}
public void setNome(String nome) {
    this.nome = nome;
}
public String getEndereco() {
    return endereco;
}
public void setEndereco(String endereco) {
    this.endereco = endereco;
}
public String getSobrenome() {
    return sobrenome;
}
public void setSobrenome(String sobrenome) {
    this.sobrenome = sobrenome;
}

}

Telephone class:

package model;

public class Telefone {

private String telefone;

public String getTelefone() {
    return telefone;
}
public void setTelefone(String telefone) {
    this.telefone = telefone;
}
}

test class:

package model;

public class Teste {

public static void main(String[] args) {

    Pessoa pessoa = new Pessoa();           

    pessoa.setNome("Aline");
    pessoa.setSobrenome("Gonzaga");
    pessoa.setEndereco("Bairro Barra");
    String telefones[] = new String[3];
    telefones[0] = "7627-86476";
    telefones[1] = "5362-56423";
    telefones[2] = "33333-3333";

    System.out.println(pessoa.getNome());
    System.out.println(pessoa.getSobrenome());
    System.out.println(pessoa.getEndereco());
    for(int i = 0; i < 3; i++){
        System.out.println();
    }
}
}
  • 1

    Start the constructor of Pessoa with this.tels = new Telefone[3]. Thus, it will only be possible to add 3 phones. If you try to add more, a IndexOfBoundException which is the attempt to access or add an invalid Input in a pre-defined array.

  • I did this in the Person builder, put the tels = new Phone[3]. But in the main class I created a Telephone type vector with 4 more elements and rotated...

2 answers

4


There are better ways to assemble the classes, but without wanting to mess with the structure and directly answering the question, I would do so:

public static void main(String[] args) {
    Pessoa pessoa = new Pessoa();           
    pessoa.setNome("Aline");
    pessoa.setSobrenome("Gonzaga");
    pessoa.setEndereco("Bairro Barra");
    Telefone telefones[] = new Telefone[3];
    Telefone telefoneTemp = new Telefone();
    telefoneTemp.setTelefone("7627-86476");
    telefones[0] = telefoneTemp;
    telefoneTemp = new Telefone();
    telefoneTemp.setTelefone("5362-56423");
    telefones[1] = telefoneTemp;
    telefoneTemp = new Telefone();
    telefoneTemp.setTelefone("33333-3333");
    telefones[2] = telefoneTemp;
    pessoa.setTels(telefones);
    System.out.println(pessoa.getNome());
    System.out.println(pessoa.getSobrenome());
    System.out.println(pessoa.getEndereco());
    for (Telefone telefone : pessoa.getTels()) System.out.println(telefone.getTelefone());
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If the class Telefone had a builder, it would be easier. If the class Pessoa had a better method to add the phones, it would be even easier. What has already been shown in previous responses, so I will not reproduce what has been taught before, that was much better than this code.

For the code to stay in order there are several things that would need to be added to it.

  • I do not understand why you gave new after inserting in the vector the phone.

  • To create a new instance, each element will have a NEW phone.

  • Wait just one more thing, I’m added a new value in the vector I still don’t understand why I need a new instance to create a new phone... Why does this happen?

  • 2

    If you read what you wrote, it is already answered: "I am added a new value in the array". How will you add a new value without having a new value? A new value is obtained by creating a new instance. You are creating a new phone and placing that phone in the array. I think this class is an exaggeration, but since you created it, you have to keep creating instances for it.

  • Got it! Thank you...I hadn’t thought of it before...

3

Well, I made some adjustments to your code, but I tried to minimize the impact, just to facilitate your understanding of the changes and why you made them.

Follow the adjusted code:

Classe Pessoa

public class Pessoa {

private String nome;
private String endereco;
private String sobrenome;
private Telefone[] tels;

public Telefone[] getTels() {
    return tels;
}

public void setTels(Telefone[] tels) {
    this.tels = tels;
}

public String getNome() {
    return nome;
}

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

public String getEndereco() {
    return endereco;
}

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

public String getSobrenome() {
    return sobrenome;
}

public void setSobrenome(String sobrenome) {
    this.sobrenome = sobrenome;
}

}

Telephone class

public class Telefone {

private String telefone;

public Telefone(String t) {
    this.telefone = t;
}

public String getTelefone() {
    return telefone;
}

public void setTelefone(String telefone) {
    this.telefone = telefone;
}
}

Test Class

public class Teste {

public static void main(String[] args) {

    Pessoa pessoa = new Pessoa();

    pessoa.setNome("Aline");
    pessoa.setSobrenome("Gonzaga");
    pessoa.setEndereco("Bairro Barra");
    Telefone[] telefones = new Telefone[3];

    telefones[0] = new Telefone("7627-86476");
    telefones[1] = new Telefone("5362-56423");
    telefones[2] = new Telefone("33333-3333");
    pessoa.setTels(telefones);

    System.out.println(pessoa.getNome());
    System.out.println(pessoa.getSobrenome());
    System.out.println(pessoa.getEndereco());
    for (int i = 0; i < 3; i++) {
        System.out.println(pessoa.getTels()[i].getTelefone());
    }
}
}

The problem with your original code is that you weren’t doing the set of the phones in your object Pessoa, beyond what you were not creating instances of the class Telefone, which is what is expected by Pessoa.

Now try to understand the adjustments that have been made and compare with the original code.

[]'s

  • Explain this to me please: System.out.println(pessoa.getTels()[i]. getTelefone());

  • You need to indicate which position is retrieving the value to be displayed. Of course, this could be done in more elegant ways, but as stated in the body of the answer, I did not focus on this issue.

Browser other questions tagged

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