pkv pkv - 2 months ago 15
Java Question

How can I use method abstraction for checking userInput against login details?

We worked progressively on a project. Last milestone is dividing this into the following: Create a class with four methods: main method, a method for creating customer information(void), a method for creating user name and password (void) and finally a method for log-in(return type).

I have implemented the log-in method, when moving to implementation of the

loginDetails
, I ran into problems. I don't understand how I can create
loginDetails
in a void method, and also be able to check against it in a different method. My
loginDetail
method is not returning a value to main, and my variables are local for the methods.

How can I create the user details in a void method, and be able to create the login check in a separate method?

This is my code, I would like to use the details generated in
loginDetails();
as the values to check against instead of the manually inserted strings.

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
}

Answer
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
}