One person may have several phones. How to do this with vector and not with list?

1

I'm not able to associate Telefone with class Pessoa , a person can have up to three phones. How do you do that?

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;
}

}

Phone 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();
    }
}
}
    
asked by anonymous 20.03.2016 / 00:22

2 answers

4

There are better ways to build classes, but without wishing to tweak the structure and answer the question directly, 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());
    }
}

See running on ideone .

If the Telefone class had a constructor, it would be easier. If the Pessoa class had a better method to add the phones, it would be even easier. What has been shown in previous answers, so I'm not going to reproduce what been taught before , which was much better than this code.

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

    
20.03.2016 / 01:35
3

Well, I made some adjustments to your code, but I tried to minimize the impact, just to make it easier for you to understand the changes and why you did them.

Follow the adjusted code:

Person Class

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;
}

}

Phone 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 were not doing set of the phones in your Pessoa object, plus you were not creating instances of the Telefone class, which is expected by Pessoa .

Now try to understand the settings that were made and compare with the original code.

[] 's

    
20.03.2016 / 01:33