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