Home > Enterprise >  How can I create a reference to an object in my main function (given the user input) (Java)?
How can I create a reference to an object in my main function (given the user input) (Java)?

Time:11-22

I'm making a class Usuario which is stored in an ArrayList in another class called Usuarios and I'm trying to make a code that allows the user to type his name and the object of the class Usuario is stored in a variable called usuario_logado for further occasions.

Here is my Usuarios class:

import java.util.ArrayList; 
public class Usuarios {
    private static ArrayList<Usuario> UsersList = new ArrayList<Usuario>();

    public static ArrayList<Usuario> getUsuarios(){
        return Usuarios.UsersList;
    }

    public static void addUser(Usuario u){
        UsersList.add(u);
    } 
}

Here is my Usuario class:

import java.util.ArrayList; 
/**
 * Usuario
 */
public class Usuario {

    private static int indexes = 0;
    private int index;
    private String nome;
    private String email;

    public Usuario(){
    }
    
    public Usuario(String nome, String email){
       
        this.nome = nome;
        this.email = email;
        Usuarios.addUser(this);
        this.index = indexes;
        indexes  ;
    }
    public String getNome(){
        return nome;
    }

    public String getEmail(){
        return email;
    }

    public int getIndex(){
        return index;
    }

    public String getString(){
        return "Nome : "   this.nome   " E-mail : "   this.email   " index:"   this.index; 
    }
}

And here is the main function in the class app:

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

public class App {
    public static void main(String[] args) {

        Usuario user0 = new Usuario("None", "None");
        Usuario user1 = new Usuario("Thomas", "[email protected]");
        Usuario user2 = new Usuario("juao", "[email protected]");
        Usuario user3 = new Usuario("marcia", "[email protected]");
        Usuario user4 = new Usuario("Ednaldo", "[email protected]");
        Usuario user5 = new Usuario("Fausto", "[email protected]");
       
        Usuario usuario_logado = new Usuario();

        Scanner input = new Scanner(System.in);

        System.out.print("Login \n Username :");
        String nome = input.next();

        for(Usuario user : Usuarios.getUsuarios()){
            if(user.getNome().equals(nome)){
                usuario_logado = user;
            }
        }
        usuario_logado.getString(); 
    }
}

Is there any way that I can store the usuario(user) in the main function?

CodePudding user response:

You need to do a couple of changes to your code. First, remove the static variable in Usuarios and make it an instance variable:

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

public class Usuarios {
    private List<Usuario> usersList = new ArrayList<>();

    public List<Usuario> getUsuarios(){
        return usersList;
    }

    public void addUser(Usuario u){
        usersList.add(u);
    }
}

Additionally, remove the line Usuarios.addUser(this); in Usuario as follows:

public class Usuario {

    private static int indexes = 0;
    private int index;
    private String nome;
    private String email;

    public Usuario(){
    }

    public Usuario(String nome, String email){
        this.nome = nome;
        this.email = email;
        this.index = indexes;
        indexes  ;
    }
    public String getNome(){
        return nome;
    }

    public String getEmail(){
        return email;
    }

    public int getIndex(){
        return index;
    }

    public String getString(){
        return "Nome : "   this.nome   " E-mail : "   this.email   " index:"   this.index;
    }
}

And finally, the most important thing, change your main class so that you create an instance of Usuarios and add each and every Usuario that you create to that Usuarios instance as follows:

import java.util.*;

public class App {
    public static void main(String[] args) {
        Usuarios users = new Usuarios();
        Usuario user0 = new Usuario("None", "None");
        users.addUser(user0);
        Usuario user1 = new Usuario("Thomas", "[email protected]");
        users.addUser(user1);
        Usuario user2 = new Usuario("juao", "[email protected]");
        users.addUser(user2);
        Usuario user3 = new Usuario("marcia", "[email protected]");
        users.addUser(user3);
        Usuario user4 = new Usuario("Ednaldo", "[email protected]");
        users.addUser(user4);
        Usuario user5 = new Usuario("Fausto", "[email protected]");
        users.addUser(user5);

        Usuario usuario_logado = new Usuario();

        Scanner input = new Scanner(System.in);

        System.out.print("Login \n Username :");
        String nome = input.next();

        for(Usuario user : users.getUsuarios()){
            if(user.getNome().equals(nome)){
                usuario_logado = user;
            }
        }
        System.out.println(usuario_logado.getString());
    }
}
  • Related