如何使用方法抽象来检查userInput以防止登录详细信息?

时间:2016-10-03 14:31:24

标签: java

我们逐步开展了一个项目。最后一个里程碑将此划分为以下内容:使用以下四种方法创建一个类:main方法,创建客户信息的方法(void),创建用户名和密码的方法(void)以及最后登录方法(返回类型)。

我已经实现了登录方法,当转移到loginDetails的实现时,我遇到了问题。我不明白如何在void方法中创建loginDetails,并且还能够以不同的方法检查它。我的loginDetail方法没有向main返回值,我的变量是方法的本地变量。

如何在void方法中创建用户详细信息,并能够在单独的方法中创建登录检查?

这是我的代码,我想使用loginDetails();中生成的详细信息作为要检查的值,而不是手动插入的字符串。

public class Customer {
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {


        loginDetails("testtest", "testtest", "11111121111");
        System.out.print(logIn("ABC", "212"));
    }

    public static void customerInformation() {
//Create customerInformation
    }

    public static void loginDetails(String firstName, String lastName, String number) {
        String userName, password;

        userName = firstName.charAt(0) + lastName.substring(0, 3);
        password = lastName.substring(0, 3) + number.substring(7);

    }

    public static String logIn(String userName, String password) {
        int count = 0;
        while (count < 3) {
            System.out.print("Input Username");
            String inputUserName = input.nextLine();
            System.out.print("Input password");
            String inputPassword = input.nextLine();

            if (inputUserName.equals(userName) && inputPassword.equals(password)) {
                return "You are now logged in";
            } else {
                count++;
                if (count < 3)
                    System.out.println("Wrong Username or password. Try again");
            }
        } //While
        return "Try again in a few hours"; //Third try
    } //logIn
}

1 个答案:

答案 0 :(得分:0)

class Customer {
    static Scanner input = new Scanner(System.in);
    static String userName=null;
    static String password=null;
    public static void main(String[] args) {


        loginDetails("testtest", "testtest", "11111121111");
        System.out.print(logIn("ABC", "212"));
    }

    public static void customerInformation() {
//Create customerInformation
    }

    public static void loginDetails(String firstName, String lastName, String number) {

        userName = firstName.charAt(0) + lastName.substring(0, 3);
        password = lastName.substring(0, 3) + number.substring(7);

    }

    public static String logIn(String userName, String password) {
        int count = 0;
        while (count < 3) {
            System.out.print("Input Username");
            String inputUserName = input.nextLine();
            System.out.print("Input password");
            String inputPassword = input.nextLine();

            if (inputUserName.equals(userName) && inputPassword.equals(password)) {
                return "You are now logged in";
            } else {
                count++;
                if (count < 3)
                    System.out.println("Wrong Username or password. Try again");
            }
        } //While
        return "Try again in a few hours"; //Third try
    } //logIn
}