Search code examples
javaauthenticationarraylistmodular

Create a login system in Java using Arraylist


I'm a junior developer and for my project, my goal is to start with a login.

I tried to check the ID and Password on the same function, individually, one inside another like the one I'll show next

I've got an Employee class:

public class Empleado{
    private String id;
    private String passwd;

    public Empleado(String id, String passwd) {
        this.id = id;
        this.passwd = passwd;
    }

}

And this is how far I've gotten on Main:

String identificador;
boolean idCheck;
String contrasena;
boolean passCheck;

ArrayList<Empleado> misEmpleados = new ArrayList<>();
misEmpleados.add(new Empleado("EmpleadoA","passA"));
misEmpleados.add(new Empleado("EmpleadoB","passB"));
misEmpleados.add(new Empleado("EmpleadoC","passC"));

do{
  System.out.print("Enter ID: ");
  identificador = input.next();

  idCheck = comprobarId(false, identificador, misEmpleados);
}while(!idCheck);

public boolean comprobarId(boolean check, String id, ArrayList<Empleado> recepcionistas){
        String contrasena;

        for (int i = 0; i < recepcionistas.size(); i++) {

            if(recepcionistas.get(i).getId().equals(id)){

                System.out.print("Introducir contraseña: ");
                contrasena = input.next();

                check = comprobarPass(id, contrasena, recepcionistas);
            }
        }
        return check;
    }


public boolean comprobarPass(String id, String pass, ArrayList<Empleado> recepcionistas){

        for (int i = 0; i < recepcionistas.size(); i++) {

            if(recepcionistas.get(i).getId().equals(pass)){
                check = true;
            }
        }
        return check;
    }

Hope someone could help me a bit... Been struggling with this for 7 hours now.

Excuse my english and any other mistake, this is my first post/question


Solution

  • You already get the Empleado corresponding to the input id, pass only this Empleado to the comprobarPass method and not the whole list recepcionistas.

    In your code, you are trying to do the job twice by looping in both method.

    If you do the loop only once, to find the empleado, you don't need to do it again and you can use the empleado reference.

    Another issue with you code is the 2 variables check: You set the value of a variable check in the method comprobarPass but this variable is not declared. Plus, in the comprobarId the primitive booleancheckvariable is passed as a parameter, modified then returned. This usage of primitive type variable is a bad practice. Prefer to remove this parameter and return a local variable.

    Rule of thumb: The simpler, the better!

    public boolean comprobarId(String id, ArrayList<Empleado> recepcionistas){
        String contrasena;
        boolean check = false;
    
        for (int i = 0; i < recepcionistas.size(); i++) {
    
            if(recepcionistas.get(i).getId().equals(id)){
    
                System.out.print("Introducir contraseña: ");
                contrasena = input.next();
    
                check = comprobarPass(contrasena, recepcionistas.get(i)); // pass the empleado, not the list
            }
        }
        return check;
    }
    
    
    public boolean comprobarPass(String pass, Empleado recepcionista){
        return recepcionista.getPasswd().equals(pass));
    }