当循环找到匹配的用户名和密码时,如何重置循环?

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

How to reset the loop when the loop found the matched username and password?

问题

public void actionPerformed(ActionEvent e){
        
        if(e.getSource() == loginButton){           
            for(int i = 0; i < AcademicSystem.allUser.size();){
                User usr = AcademicSystem.allUser.get(i);
                User pswrd = AcademicSystem.allUser.get(i);
                
                System.out.println(usr.getLogin_username() + "\t" + userName.getText());
                System.out.println(pswrd.getLogin_password() + "\t"  + password.getText());
                System.out.println("");
                
                if(userName.getText().equals(usr.getLogin_username()) && password.getText().equals(pswrd.getLogin_password())){
                    JOptionPane.showMessageDialog(loginButton, "Username and password matched!");
                    JOptionPane.showMessageDialog(loginButton, "User[" + userName.getText() + "] have successfully logged in");
           
                    Dashboard db = new Dashboard();
                    setVisible(false);
                    db.setVisible(true);
                    break;
                }
                
                else if (!AcademicSystem.allUser.contains(userName.getText()) && !AcademicSystem.allUser.contains(password.getText())){
                   i++;
                }
            }
        }
    }
英文:

This code section is part of code in my program. I am currently write a GUI program. This part of code is about to extract the username and password from the ArrayList and if matched then user are allowed to login. My desired output is if this loop find the matched username and password, it will break and proceed to another interface; Oppositely, if didn't matched, the loop will continue to find the matched one. So then, how should my if-else going to be ? I can't pop up the message that show username and password is wrong because in else block, I need it to loop until find the matched.

public void actionPerformed(ActionEvent e){
        
        if(e.getSource() == loginButton){           
            for(int i =0;i&lt;AcademicSystem.allUser.size();){
                User usr = AcademicSystem.allUser.get(i);
                User pswrd = AcademicSystem.allUser.get(i);
                
                System.out.println(usr.getLogin_username() + &quot;\t&quot; + userName.getText());
                System.out.println(pswrd.getLogin_password() + &quot;\t&quot;  + password.getText());
                System.out.println(&quot;&quot;);
                
                if(userName.getText().equals(usr.getLogin_username())&amp;&amp;password.getText().equals(pswrd.getLogin_password())){
                    JOptionPane.showMessageDialog(loginButton,&quot;Username and password matched!&quot;);
                    JOptionPane.showMessageDialog(loginButton, &quot;User[&quot; + userName.getText() + &quot;] have successfully logged in&quot;);
           
                    Dashboard db = new Dashboard();
                    setVisible(false);
                    db.setVisible(true);
                    break;
                }
                
                else if (!AcademicSystem.allUser.contains(userName.getText())&amp;&amp;!AcademicSystem.allUser.contains(password.getText())){
                   i++;
                }
            }
        }
    }

答案1

得分: 2

不要试图在循环中处理所有事情。在循环结束之前,您实际上不知道应该采取什么操作,因此,相反地,遍历可用的用户并找到与条件匹配的用户,如果找不到,让匹配保持为 "null"。

然后在循环结束时,您可以检查 "matched" 对象的状态并采取适当的操作。

例如:

User matchedUser = null;

String userName = this.userName.getText();
String password = this.password.getText();
for (User user : AcademicSystem.allUser) {
    if (userName.equals(usr.getLogin_username()) && password.equals(pswrd.getLogin_password())) {
        matchedUser = user;
        break;
    }
}

if (matchedUser != null) {
    // 操作成功
} else {
    // 验证失败
}

这种方法可以将逻辑转移到 "AcademicSystem" 中,并且可以减少对以下类似内容的调用:

User matchedUser = AcademicSystem.authenticate(userName.getText(), password.getText());
if (matchedUser != null) {
    // 操作成功
} else {
    // 验证失败
}

这将把验证用户的责任移交到其所属的领域,并在此过程中减少代码复杂性,双赢。

英文:

Don't try and do everything in you loop. Until the loop ends, you don't actually know what action you should take, so, instead, loop through the available users and find one which matches the criteria or, if you can't, let the match remain null.

Then when the loop ends, you can check the state of the matched object and take appropriate action.

For example:

User matchedUser = null;

String userName = this.userName.getText();
String password = this.password.getText();
for (User user : AcademicSystem.allUser) {
    if (userName.equals(usr.getLogin_username()) &amp;&amp; password.equals(pswrd.getLogin_password())) {
        matchedUser = user;
        break;
    }
}

if (matchedUser != null) {
    // You&#39;re good to go
} else {
    // Authentication failed
}

This approach would all you to move the logic to the AcademicSystem and would then allow you to reduce the call to something more like:

User matchedUser = AcademicSystem.authenticate(userName.getText(), password.getText());
if (matchedUser != null) {
    // You&#39;re good to go
} else {
    // Authentication failed
}

This moves the responsibility for validating the user to the domain it belongs in and reduces you overall code complexity in the process, win/win

huangapple
  • 本文由 发表于 2020年7月25日 14:53:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63085311.html
匿名

发表评论

匿名网友

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

确定