Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.Scanner;
- //Class to handle registration and login processes separately from the game controller
- public class RegistrationAndLogin
- {
- //Method of type Player which handles the registration process and returns the new Player object registered
- public Player Register(List<Player> thePlayerList)
- {
- //creates temporary variables to hold the player's information during registration
- String tempName, tempSurname, tempUsername, tempPassword;
- //each variable is assigned values by calling the methods responsible for getting the user's details
- tempName = setName();
- tempSurname = setSurname();
- tempUsername = setUsername(thePlayerList);
- tempPassword = setPassword();
- System.out.println("\n You have successfully registered a new player with username " + tempUsername);
- System.out.println(" You need to log in to play the game.");
- //creates new player object using the player's details, with default score & number of games player equal to 0
- Player newPlayer = new Player(tempName,tempSurname,tempUsername,tempPassword, 0, 0);
- //adds the new player's details to the array list
- return newPlayer;
- }
- private String setName()
- {
- Scanner scan = new Scanner(System.in);
- System.out.print("\n Enter your first name: ");
- String name = scan.nextLine();
- while (!InputValidation.nameValidation(name)) //while the user's input does not pass the validation, prompt to enter new name
- {
- System.out.print("\n Please enter your first name again: ");
- name = scan.nextLine();
- }
- return name;
- }
- private String setSurname()
- {
- Scanner scan = new Scanner(System.in);
- System.out.print("\n Enter your last name: ");
- String surname = scan.nextLine();
- while (!InputValidation.nameValidation(surname))//while the user's input does not pass the validation, prompt to enter new surname
- {
- System.out.print("\n Please enter your last name again: ");
- surname = scan.nextLine();
- }
- return surname;
- }
- private String setUsername(List<Player> thePlayerList)
- {
- Scanner scan = new Scanner(System.in);
- System.out.print("\n Guidelines for choosing your username: ");
- System.out.print("\n 1) Your player username needs to be at least 3 characters long and no more than 12.");
- System.out.print("\n 2) The first character can not be a number.");
- System.out.print("\n 3) The username can not contain any special characters. ");
- System.out.print("\n Enter your desired player username: ");
- String username = scan.nextLine();
- //program will prompt user to choose a different username if the username they have entered does not pass
- //the validation and if another user has already registered with the same username
- while ((!InputValidation.usernameValidation(username)) || (!isUsernameAvailable(username, thePlayerList)))
- {
- System.out.print("\n Please enter a new username: ");
- username = scan.nextLine();
- }
- return username.toLowerCase(); //all usernames are case-insensitive
- }
- public boolean isUsernameAvailable(String aUsername, List<Player> thePlayerList)
- {
- boolean valid = true;
- for (int i=0; i < thePlayerList.size(); i++) //loops through the player list
- {
- //for every player, checks whether the user's input matches an already registered player's username
- if (aUsername.equalsIgnoreCase(thePlayerList.get(i).getUsername()))
- {
- System.out.print("\n This username is already in use.");
- valid = false; //if it does, the method returns a false value so the loop in the calling method can repeat
- break; //usernames will be unique, so we know there won't be another one, so no need to continue the loop
- }
- }
- return valid;
- }
- private String setPassword()
- {
- Scanner scan = new Scanner(System.in);
- System.out.print("\n Guidelines for choosing your password: ");
- System.out.print("\n 1) Your password needs to be at least 8 characters long but no more than 15.");
- System.out.print("\n 2) Your password should contain at least one uppercase letter");
- System.out.print("\n 3) Your password should contain at least one special character. ");
- System.out.print("\n Enter your desired player password: ");
- String password = scan.nextLine();
- while (!InputValidation.passwordValidation(password)) //while the user's input does not pass the validation, prompt to enter new password
- {
- System.out.print("\n Please enter a new password: ");
- password = scan.nextLine();
- }
- return password;
- }
- //method of type integer which returns the value of the player logged in
- //if the user quits during the login process, the value returned will be the initial value of the current player
- //if the user successfuly logs in, the value returned will be that of the new user
- public int Login(List<Player> thePlayerList, int theCurrentPlayer)
- {
- int tempPlayer = -1;
- //user will only be allowed to login after they have registered
- //if the ArrayList is empty it means no player profiles (objects) have been created yet
- if (thePlayerList.isEmpty())
- {
- System.out.println("\n You need to be registered before you can login.");
- System.out.println(" Register by choosing R from the main menu.");
- System.out.println();
- return theCurrentPlayer; //redundant
- }
- else
- {
- Scanner scan = new Scanner(System.in);
- System.out.println("\n Enter your username: ");
- String verification = scan.nextLine();
- //loops through the array to check whether the username entered by the user (ignoring case)
- //is equal to any of the usernames in the player database
- for (int i=0; i < thePlayerList.size(); i++)
- {
- if (verification.equalsIgnoreCase(thePlayerList.get(i).getUsername()))
- {
- tempPlayer = i;
- break; //once a match has been found no need to run the whole loop (unique usernames)
- }
- }
- //if true means no username was matched (tempPlayer was initialised as -1)
- while(tempPlayer == -1)
- {
- System.out.println("\n No such username exists.");
- System.out.println(" Please enter your username again, or type 'quit' to quit the login process: ");
- verification = scan.nextLine();
- //if user wants to quit
- if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
- {
- break; //exits the username verification loop
- }
- else
- {
- for (int i=0; i < thePlayerList.size(); i++)
- {
- if (verification.equalsIgnoreCase(thePlayerList.get(i).getUsername()))
- {
- tempPlayer = i;
- break; //exits the username verification loop
- }
- }
- }
- }
- //need to repeat this check in the case the user wants to quit, in order to exit the whole method
- if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
- {
- return theCurrentPlayer; //will exit method as user no longer wants to continue
- //the player logged in is the same one as before the login process was invoked
- }
- //if username is correct, we continue the verification process
- System.out.println("\n Enter your password: ");
- verification = scan.nextLine();
- while(!thePlayerList.get(tempPlayer).getPassword().equals(verification))
- {
- System.out.println("\n You have entered a wrong password.");
- System.out.println(" Please enter your password again, or type 'quit' to quit the login process: ");
- verification = scan.nextLine();
- if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
- {
- break; //escapes this while loop
- }
- }
- //need to repeat this check in the case the user wants to quit, in order to exit the whole method
- if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
- {
- return theCurrentPlayer; //will exit method as user no longer wants to continue
- //the player logged in is the same one as before the login process was invoked
- }
- //if the interpreter reaches this point it means that the user has managed to log in successfuly
- System.out.println("\n Welcome back " + thePlayerList.get(tempPlayer).getName() + "!");
- System.out.println(" Your total score is " + thePlayerList.get(tempPlayer).getTotalScore() + ".");
- System.out.println();
- return tempPlayer; //changes the new currently logged in Player
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement