Array is not showing the data that should be there

1

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?

    
asked by anonymous 09.11.2018 / 19:38

2 answers

2

The problem here is conceptual first of all, so any solution you are asking for will continue to be wrong, the real solution is to throw it all away and start doing it the right way again.

It does not make sense to have an array inside a class called Usuario , after all there is a one user and not a bunch of them. Either the class is about a user, or it's about a collection of users. Then you start to have other problems. You create a user (the new and the builder call does exactly this) and then call a method called criarUsuario() , this also does not make sense, the user is already created and without data, which no longer made sense. Then create a new user and get a user, who obviously has nothing, you just created, so it makes less sense.

If you create a user class like it should be where it only takes care of the individual user, that the object is initialized by a constructor as it should be, and that the user interface (in this case the use of Scanner ) is separated and placed in the main class or a separate class just to take care of it and not mix with the user, it begins to be easier to understand what the code does, each with its responsibility, and it is clear that these methods of creating and searching they do not make sense there, they are part of another class, and that user is an entity of his own, and everything starts to make sense, including because he will no longer create a user to have a user create and will not create to search as well. p>

There are still other confusing things in the question. The first method is setNome() , you even understand that it's not about him you're talking about, but it's an indication that you do not care about clarity and accuracy of what you're defining. What threaded list has to do with the problem? Programming is to worry about this, especially in object orientation. Without knowing exactly what you want, that is, without deeply understanding the problem, the solution will never come out good because there is problem in the problem. Programming is more about solving problems than creating codes.

Class Interface does not seem to be a good name and maybe, just maybe, and just for an exercise the array should be in it (might want a more complex and closer solution than really is done).

    
09.11.2018 / 19:58
0

As indicated by the friend above I changed to the following:

Interface class:

import java.util.Scanner ;
import java.util.ArrayList;
import java.util.List;

public class Interface
{    
    private List usuarios = new ArrayList();   

    public void menuPrincipal()
    {
        Scanner entrada = new Scanner(System.in);
        this.apresentaMenu();
        int opcao = entrada.nextInt();
        while(opcao!=5)
        {
            switch(opcao)
            {
                case 1:
                System.out.println("Cadastro de usuario");
                System.out.println("Forneça o nome");
                entrada.nextLine();
                String nome = entrada.nextLine();
                System.out.println("Forneça o cpf");
                String cpf = entrada.nextLine();
                Usuario umUsuario= new Usuario();

                usuarios.add(umUsuario);
                break;

                case 2 :
                System.out.println("Busca de usuario");
                System.out.println(usuarios.size());

User class:

public class Usuario
{
    private String nome;
    private String cpf;


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

}
    
09.11.2018 / 20:30