Java ArrayList - Overriding values

5

I need to perform the following activity:

Consider an application to store the following person data in an address book: name, address, and telephone. Specify a TAD to store the people data and the operations required to enter, query, and delete people's data. "

Well, I need to create a class that registers a user and performs operations like check register and deletion, well, I'm a layman in java but I was able to write the data to an ArrayList, however whenever I register a new user, the previous one is overwritten, storing only the last one registered. Below is the code, divided into Main, Person and Operations class. I want to know why you are overwriting and if the code is generally in agreement with the statement of the question. Thank you all!

Main class:

public class exercicio {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    Pessoa usuario = new Pessoa();
    Operacoes acao = new Operacoes();

    int op;

    do {
        System.out.println("[1] Inserir");
        System.out.println("[2] Consultar");
        System.out.println("[3] Remover");
        System.out.println("[4] Sair");
        System.out.print("Opção desejada: ");
        op = input.nextInt();
        switch (op) {
            case 1:
                input.nextLine();
                System.out.print("Nome: ");
                usuario.setNome(input.nextLine());
                System.out.print("Endereço: ");
                usuario.setEndereco(input.nextLine());
                System.out.print("Telefone: ");
                usuario.setTelefone(input.nextLine());
                acao.inserePessoa(usuario);
                System.out.println(usuario);
                break;
            case 2:
                acao.consultaPessoa();
                break;
            case 3:
                break;
        }
    } while (op != 4);
}}

Class Person:

public class Pessoa {

String nome;
String endereco;
String telefone;

public Pessoa() {
}

public Pessoa(String nome, String endereco, String telefone) {
    this.nome = nome;
    this.endereco = endereco;
    this.telefone = telefone;
}

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 getTelefone() {
    return telefone;
}

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

@Override
public String toString() {
    return "nome=" + nome + ", endereco=" + endereco + ", telefone=" + telefone;
}}

Class Operations:

public class Operacoes extends Pessoa {

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

public void inserePessoa(Object usuario) {
    listaPessoa.add((Pessoa) usuario);
}

public String consultaPessoa() {
    for (Pessoa c: listaPessoa) {
        System.out.println(listaPessoa.get(0));
    }
    return "oi";
}}
    
asked by anonymous 28.02.2015 / 21:02

1 answer

3

You need to create new instances of this Person you created

public class exercicio {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    Operacoes acao = new Operacoes();

    int op;

    do {
        System.out.println("[1] Inserir");
        System.out.println("[2] Consultar");
        System.out.println("[3] Remover");
        System.out.println("[4] Sair");
        System.out.print("Opção desejada: ");
        op = input.nextInt();
        switch (op) {
            case 1:
                Pessoa usuario = new Pessoa();
                input.nextLine();
                System.out.print("Nome: ");
                usuario.setNome(input.nextLine());
                System.out.print("Endereço: ");
                usuario.setEndereco(input.nextLine());
                System.out.print("Telefone: ");
                usuario.setTelefone(input.nextLine());
                acao.inserePessoa(usuario);
                System.out.println(usuario);
                break;
            case 2:
                acao.consultaPessoa();
                break;
            case 3:
                break;
        }
    } while (op != 4);
}}

Access the list with this.

public class Operacoes extends Pessoa {

public ArrayList<Pessoa> listaPessoa = new ArrayList<>();

public void inserePessoa(Pessoa usuario) {
    listaPessoa.add(usuario);
}

public String consultaPessoa() {
    for (Pessoa c: this.listaPessoa) {
        System.out.println(listaPessoa.get(0));
    }
    return "oi";
}}

If you want to remove a Person by name

public String deletaPessoa(String alvo) {
    for (Pessoa c: this.listaPessoa) {
       if (c.getnome().equals(alvo)) c.remove();
    }

}

This image illustrates this rather than a person with trees

Each person is someone different, has a different Id, if you change the same instance, you are changing the same person

    
28.02.2015 / 21:15