如何在 Java 中为不同用户使用相同的代码进行密码条目



(注意:我是Java和编程新手,使用Netbeans)

我的任务是创建代码,该代码将首先要求用户名,然后要求输入密码。我的解决方案是为每个用户的密码输入设置一个单独的代码块。但是,这非常冗长。我用于密码输入的代码在这里:

List<String> passwords = new ArrayList<>();   //array which is populated by user input
    Scanner passwordinput = new Scanner(System.in);
    do {
        System.out.print("Create password? (y/n)");        //create password request
        if (passwordinput.next().startsWith("y")) {
            System.out.print("Create password (no spaces): ");
            passwords.add(passwordinput.next());
        } else {
            break;
        }
    } while (true);
    System.out.print("Enter password: ");                    //enter password request
    String enterpass;
    enterpass = passwordinput.next();
    if (passwords.contains(enterpass)) {
        System.out.println("wohoo you're in");
    } else {
        System.out.println("bad luck brudda");
    }

总之,我问用户是否要创建密码,然后输入密码(将其放入字符串中)。然后,要求他们输入该密码以继续。

有什么方法可以为每个用户使用相同的代码块,但不必每次都复制它?

欢迎任何其他改进。

看看地图。在 HashMap 中,您将值映射到固定键。因此,每个用户名(密钥)只能存在一次,但密码(值)可以始终相同。
你需要一个HashMap。这个哈希映射有一个字符串作为键和一个字符串作为值。密钥是您的用户名,密码是您的值。所以你这样做:

HashMap<String, String> logins = new HashMap<String, String>();
String username1 = "FIRST_USERNAME";
String password1 = "FIRST_PASSWORD";
String username2 = "SECOND_USERNAME";
String password2 = "SECOND_PASSWORD";
logins.put(username2, password2);
logins.put(username1, password1); // adds the username and the password to your "loginlist"
String inputUser = scanner.nextLine();    
String inputPassword = scanner.nextLine();
if(inputPassword.equals(logins.get(inputUser)){
 // Password and user correct
}

编辑:
下面是如何使用方法执行此操作的示例。也许那时你更明白了。希望您了解一些有关异常的信息。如果没有,你应该了解它们,因为它们对Java是必不可少的

public class LoginManager {
private Map<String, String> logins;
public LoginManager() {
    this.logins = new HashMap<String, String>();
}
public void logIn(final String pUserName, final String pPassword) {
    if (pPassword == null) {
        throw new IllegalArgumentException("No password given");
    }
    if (!pPassword.equals(this.logins.get(pUserName))) {
        throw new IllegalStateException("Wrong username or password");
    }
}
public void registerUser(final String pUserName, final String pPassword) {
    if (pUserName != null && pPassword != null) {
        if (!pUserName.isEmpty() && !pPassword.isEmpty()) {
            if (!this.logins.containsKey(pUserName)) {
                this.logins.put(pUserName, pPassword);
                return;
            }
        }
        else {
            throw new IllegalStateException("Username already in use");
        }
    }
    throw new IllegalArgumentException("Username or password not given");
}
public static void main(final String[] args) {
    final String choice1 = "1";
    final String choice2 = "2";
    LoginManager manager = new LoginManager();
    Scanner input = new Scanner(System.in);
    boolean loginTried = false;
    while (!loginTried) {
        System.out.print("Register new user(1) or login (2): ");
        switch (input.nextLine()) {
            case choice1:
                System.out.print("New username: ");
                String userName = input.nextLine();
                System.out.print("Password: ");
                String password = input.nextLine();
                try {
                    manager.registerUser(userName, password);
                    System.out.println("User registered");
                }
                catch (Exception error) {
                    System.out.println(error.getMessage());
                }
                break;
            case choice2:
                loginTried = true;
                System.out.print("Username: ");
                String loginName = input.nextLine();
                System.out.print("Password: ");
                String loginPassword = input.nextLine();
                try {
                    manager.logIn(loginName, loginPassword);
                    System.out.println("Login was successfully");
                }
                catch (Exception error) {
                    System.out.println("Login failed: " + error.getMessage());
                }
                break;
            default:
                System.out.println("Invalid choice. Choose 1 or 2");
                break;
        }
    }
    input.close();
 }
}

相关内容

  • 没有找到相关文章

最新更新