Java Arraylist - Overriding values

Asked

Viewed 2,070 times

5

Guys need to perform the following activity:

"Consider an application to store the following data of a person in an address book: name, address, and phone. Specify a TAD to store the data of persons and the operations necessary to enter, query and delete data of persons."

Well, I need to create a class that registers a user and performs operations like query registration and deletion, well, I’m layman in java but I was able to record the data in an Arraylist, however whenever I register a new user, the previous one is overwritten, storing only the last registered. Below is the code, divided into Main class, Person and Operations. I want to know why you are overwriting and whether the code in general is in line with the statement of the question. I 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);
}}

Classe Pessoa:

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";
}}
  • why you put user as Object ?

  • And how would I do that? What’s the difference?

  • "everything is an object", that object can only be person, so you put Person.

1 answer

3


You need to create new instances of this Person object 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, instead of people with trees inserir a descrição da imagem aqui

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

  • Right, I made the change, instantiating each person as a different object, however the query still returns the last registered record. How do I go through all the arraylist data?

  • You are accessing it directly, put as public Static and this. at the time no for, look how it was answered now

  • It worked fine, but without wanting to abuse too much, how would I accomplish the removal of a certain record? I would have to register along with name, address and phone an identifier code?

  • If you think the answer helped you vote, if you’re right, mark it right (:

  • helped yes, I scored too. It would help me in my other question?

  • Creates another question I’m monitoring

  • I fully agree, I avoid touching the structure at the time of doubting, only, I do not know if this is the best way to answer, I think I would have to change 80% code/ modeling to stay something in the java semantics

  • mgibsonbr, I am an early student of Java, would have some social network, or some more direct contact that can explain me a little bit about what you said, or it gets difficult?

  • @mgibsonbr, Truth... in his question, he was accessing in a static way, I corrected putting the Static and put this no for, It was kind of meaningless even.

  • @mgibsonbr, tested now in eclipse, does not work no, it may have different functioning between versions of java Danlucio, you find these concepts well detailed in this book Java Book

  • Guys, how we use overflow chat, let’s discuss this, having two people with a good understanding is great for a student.

Show 7 more comments

Browser other questions tagged

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