在Java中使用参数实现方法

时间:2015-02-28 14:52:29

标签: java arrays object parameter-passing

我做了一项任务,要求我创建一个基本的社交网络'在使用Java的控制台中。

我已经设置了一个CreateUser.java文件,其中包含一次创建一个用户的方法 - 如下所示。 (我已经使用默认和替代构造函数设置了User.java文件)。

public class CreateUser {
    public static void createAndSaveUser(){
    Scanner keyboard = new Scanner(System.in);
    User newUser = new User("email", "foreName", "surName", "Password");

    final int NOOFUSERSBEINGCREATED = 1;
    User[] newUsers = new User [NOOFUSERSBEINGCREATED];

    for (int count = 0; count < NOOFUSERSBEINGCREATED; count++){
        System.out.print("EMAIL: ");
        newUser.setEmail(keyboard.next());
        System.out.print("FIRST NAME: ");
        newUser.setFirstName(keyboard.next());
        System.out.print("SECOND NAME: ");
        newUser.setLastName(keyboard.next());
        System.out.print("YOUR PASSWORD: ");
        newUser.setPassword(keyboard.next());
        newUser.getUserID();

        newUsers [count] = new User (newUser.getEmail(), newUser.getFirstname(), newUser.getLastname(), newUser.getPassword());
    }//for

    //public static void continueMenu(){
    char choice;
    boolean choice2 = false;

    do {
        System.out.println("DO YOU WISH TO CONTINUE? (TYPE Y/N)");
        choice = keyboard.next().charAt(0);
        keyboard.nextLine();
        switch (choice) {
            case 'Y':
                //Store Details
                Logos.successLogo();
                System.out.println("Success! You've been Tagged. Your Tag is " + newUser.getUserID() + "\nUse it to log in.");
                choice2 = true;
                break;
            case 'y':
                //Store Details
                Logos.successLogo();
                System.out.println("Success! You've been Tagged. Your Tag is " + newUser.getUserID() + "\nUse it to log in.");
                choice2 = true;
                break;
            case 'N':
                System.out.println("\nYou have chosen not to continue. Please restart Tag.");
                choice2 = true;
                break;
            case 'n':
                System.out.println("\nYou have chosen not to continue. Please restart Tag.");
                choice2 = true;
                break;
            default:
                System.out.println("\nOops! Option not found. Type Y to continue or N to cancel.");
        }//switch
    } while (!choice2);
    //}//continueMenu

}//createAndSaveUser
}

然后我创建了一个LogIn方法,该方法由create user方法传递信息。

public class LogIn extends CreateUser {

public static void logIn(User [] newUsers, User newUser){
    Scanner keyboard = new Scanner(System.in);

    newUser.getUserID();
    newUsers[0].getEmail();
    newUsers[3].getPassword();

    boolean correctLogin = false;
    String logInAttempt;

    do {
        System.out.println("EMAIL OR TAG: ");
        logInAttempt = keyboard.nextLine();
        if (logInAttempt != newUsers[0].getEmail() || logInAttempt != newUser.getUserID()){
            System.out.println("You have entered an invalid Tag or email. Please try again");
            correctLogin = false;
        }
        else {
            System.out.println("We've found you! Please enter your Password to continue.");
        }
    }while (!correctLogin);
}
}

我现在想知道如何在MainMenu.java文件中实现这个LogIn方法 - 如下所示。我知道我需要在尝试调用方法时为方法添加参数,但我对如何构造参数一无所知。

public static void mainMenu(){
    Scanner keyboard = new Scanner(System.in);

    int choice;
    System.out.println();
    System.out.println("****** WELCOME TO TAG ******");
    System.out.println("PLEASE SELECT ONE OF THE FOLLOWING OPTIONS (TYPE 1-4):");
    System.out.println("1. LOG IN\n2. SIGN UP\n3. ADMIN LOG IN\n4. ABOUT TAG");
    choice = keyboard.nextInt();

    switch (choice){
        case 1:
            LogIn.logIn();//HERE'S THE PROBLEM
            break;
        case 2:
            CreateUser.createAndSaveUser();
            break;
        case 3:
            System.out.println("You have chosen to log in to ADMIN");
            break;
        case 4:
            System.out.println("****** ABOUT TAG ******");
            System.out.println("TAG assigns you with a Unique Tag (int) to log in with. You can also use your email address.");
            break;
        default:
            System.out.println("ERR0R 404 SELECTION NOT FOUND. Oops! Please try again.");
    }//choice
}//mainMenu

任何帮助将不胜感激!这里也是一个初学者,所以请不要在答案中过于放肆。谢谢!

0 个答案:

没有答案