使用ArrayList在Java中创建一个登录系统。

huangapple go评论57阅读模式
英文:

Create a login system in Java using Arraylist

问题

我是初级开发人员,我的项目目标是从登录开始。

我尝试在同一个函数中分别检查ID和密码,一个嵌套在另一个函数内,就像我接下来要展示的那样:

我有一个员工类:

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

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

以下是我在主函数中的代码进展:

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("Enter password: ");
                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;
}

希望有人能稍微帮助我...我已经为此苦苦挣扎了7个小时。

请原谅我的英语和其他错误,这是我第一次发帖/提问。

英文:

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&lt;Empleado&gt; misEmpleados = new ArrayList&lt;&gt;();
misEmpleados.add(new Empleado(&quot;EmpleadoA&quot;,&quot;passA&quot;));
misEmpleados.add(new Empleado(&quot;EmpleadoB&quot;,&quot;passB&quot;));
misEmpleados.add(new Empleado(&quot;EmpleadoC&quot;,&quot;passC&quot;));
do{
System.out.print(&quot;Enter ID: &quot;);
identificador = input.next();
idCheck = comprobarId(false, identificador, misEmpleados);
}while(!idCheck);
public boolean comprobarId(boolean check, String id, ArrayList&lt;Empleado&gt; recepcionistas){
String contrasena;
for (int i = 0; i &lt; recepcionistas.size(); i++) {
if(recepcionistas.get(i).getId().equals(id)){
System.out.print(&quot;Introducir contrase&#241;a: &quot;);
contrasena = input.next();
check = comprobarPass(id, contrasena, recepcionistas);
}
}
return check;
}
public boolean comprobarPass(String id, String pass, ArrayList&lt;Empleado&gt; recepcionistas){
for (int i = 0; i &lt; 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

答案1

得分: 1

你已经获取了与输入ID对应的“Empleado”,只需将此“Empleado”传递给“comprobarPass”方法,而不是整个列表“recepcionistas”。

在你的代码中,你试图在两个方法中都循环执行任务。

如果只在循环一次的情况下查找“Empleado”,则无需再次执行循环,可以使用“Empleado”引用。

你代码中的另一个问题是变量“check”:你在“comprobarPass”方法中设置了变量“check”的值,但是这个变量没有声明。此外,在“comprobarId”方法中,将基本布尔类型变量“check”作为参数传递,然后进行修改,然后返回。这种对基本类型变量的使用是一种不好的做法。建议删除此参数并返回一个局部变量。

原则是:越简单越好!

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)); // 传递 Empleado,而不是列表
        }
    }
    return check;
}

public boolean comprobarPass(String pass, Empleado recepcionista){
    return recepcionista.getPasswd().equals(pass));
}
英文:

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&lt;Empleado&gt; recepcionistas){
String contrasena;
boolean check = false;
for (int i = 0; i &lt; recepcionistas.size(); i++) {
if(recepcionistas.get(i).getId().equals(id)){
System.out.print(&quot;Introducir contrase&#241;a: &quot;);
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));
}

答案2

得分: -1

在你的comprobarPass函数中,你将员工ID与密码进行了比较,而不是与密码进行比较。我认为你的意思是将这部分代码进行更改:

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

改为这样:

Empleado e = recepcionistas.get(i);
if(e.getId().equals(id) && e.getPasswd().equals(pass)){
    check = true;
}

这样会在一个函数中同时检查ID和密码是否匹配,而不是分开检查ID和密码的比较。

英文:

In your comprobarPass function, you're comparing the employee Id to the password, rather than the password. I think you mean to change this

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

to this

Empleado e = recepcionistas.get(i);
if(e.getId().equals(id) &amp;&amp; e.getPasswd().equals(pass)){
check = true;
}

This will instead check if both the id and password are a match, in one function rather than having a separate check for both id and password comparison

huangapple
  • 本文由 发表于 2020年4月8日 18:13:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61098267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定