Class Interface
:
switch(opcao)
{
case 1:
Usuario umUsuario = new Usuario();
umUsuario.criarUsuario();
break;
case 2 :
System.out.println("Busca de usuario");
System.out.println("Forneça o cpf do usuario");
Usuario buscaUsuario = new Usuario();
buscaUsuario.buscarUsuario();
break;
}
Class Usuario
import java.util.Scanner ;
import java.util.ArrayList;
import java.util.List;
public class Usuario
{
private String nome;
private String cpf;
private List usuarios = new ArrayList();
public void setNome(String nome)
{
this.nome = nome ;
}
public void setCpf(String cpf)
{
this.cpf = cpf ;
}
public String getCpf()
{
return this.cpf;
}
public String getNome()
{
return this.nome;
}
public void criarUsuario(){
Usuario u = new Usuario();
Scanner input = new Scanner(System.in);
System.out.println("Digite o nome do usuario:");
u.setNome(input.nextLine());
System.out.println("Digite o cpf:");
u.setCpf(input.nextLine());
usuarios.add(u);
System.out.println(usuarios.size());
}
public void buscarUsuario(){
System.out.println("oi");
System.out.println(usuarios.size());
for (int i = 0; i < usuarios.size(); i++)
{
Usuario busca = (Usuario) usuarios.get(i);
System.out.println(busca.getNome());
}
}
}
The first method is ok I can create users and save in my list, but in the second method public void buscarUsuario()
where for now I just want to list all user names it does not return anything to me. Besides this tells me that the list is empty. Where am I going wrong?