简体   繁体   中英

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

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", "anotheremail@hotmail.com");
        Usuario user2 = new Usuario("juao", "juaozinhogeimepleis@hotmail.com");
        Usuario user3 = new Usuario("marcia", "jooj@hotmail.com");
        Usuario user4 = new Usuario("Ednaldo", "valenada@hotmail.com");
        Usuario user5 = new Usuario("Fausto", "olokobixo@hotmail.com");
       
        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?

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", "anotheremail@hotmail.com");
        users.addUser(user1);
        Usuario user2 = new Usuario("juao", "juaozinhogeimepleis@hotmail.com");
        users.addUser(user2);
        Usuario user3 = new Usuario("marcia", "jooj@hotmail.com");
        users.addUser(user3);
        Usuario user4 = new Usuario("Ednaldo", "valenada@hotmail.com");
        users.addUser(user4);
        Usuario user5 = new Usuario("Fausto", "olokobixo@hotmail.com");
        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());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM