Array is not showing the data that should be there

Asked

Viewed 77 times

2

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 this 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 the user names it does not return me anything. Besides this tells me that the list is empty. Where I am missing?

2 answers

3


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

Doesn’t make any sense to have a array within a class called Usuario, after all there is a user and not a lot of them. Or the class is about one user, or it’s about a collection of users. Then you start having other problems. You create a user (the new and the call from builder does just that) and then calls a method called criarUsuario(), This also makes no sense, the user is already created and without data, which no longer made sense. Then create a user again and send a search for a user, who obviously has nothing, you just create, so it makes less sense yet.

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 the Scanner) be separated and placed in the main class or a separate class just to take care of it and not mix with the user, then it begins to become easier to understand what the code does, each with its own responsibility, and then it becomes clear that these methods of creating and searching do not make sense there, that are part of another class, and that user is an entity of its own, and everything starts to make sense, including because it will no longer create a user to have created a user and will not create also to search.

There are still other confusing things in the question. The first method is the setNome(), You can understand that it’s not about him that you’re talking about, but it’s an indication that you don’t care about the clarity and accuracy of what you’re defining. What chained list has to do with the problem? Programming is worrying about it, 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 code.

Class Interface doesn’t seem to be a good name and maybe, just maybe, and just for an exercise the array should be in it (it may be that you want a solution that is more complex and closer to what is actually done).

  • I understand your notes and how you said beginner in java, but we will then the most indicated is to create a list of objects of the type User in the class Interface and not in the class itself ?

  • So for those who are starting maybe it is the case to do something simpler first. Have you thought about it? I’m not saying, just giving a hint that you might be leaving for something that’s not right. But I might be wrong. And that’s what I said.

  • Can you take a look at the change I made below if conceptually it’s better this way? further I will include more lists of other types of objects should I include in the same class yet ? create a class of lists ? which is more appropriate ?

  • The site doesn’t work like that, you ask a question and you get an answer, it’s not an interactive way that you tidy up and we help, it’s different from a forum, but looking over much improved, and it’s likely that the problem mentioned no longer exists. There is still a problem that the user is not initializing, no constructor. For an exercise is good, but in real cases it is always much more complex than this. You can make the registration of users to be a class, but simple enough is exaggeration, the screen part could be a class for this to separate from the menu

0

As indicated by the above friend I switched 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;
    }

}

Browser other questions tagged

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