Advertisement
Guest User

Untitled

a guest
Nov 28th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.92 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.Scanner;
  3.  
  4. //Class to handle registration and login processes separately from the game controller
  5. public class RegistrationAndLogin
  6.  
  7. {
  8.  
  9. //Method of type Player which handles the registration process and returns the new Player object registered
  10. public Player Register(List<Player> thePlayerList)
  11. {
  12. //creates temporary variables to hold the player's information during registration
  13. String tempName, tempSurname, tempUsername, tempPassword;
  14.  
  15. //each variable is assigned values by calling the methods responsible for getting the user's details
  16. tempName = setName();
  17. tempSurname = setSurname();
  18. tempUsername = setUsername(thePlayerList);
  19. tempPassword = setPassword();
  20.  
  21. System.out.println("\n You have successfully registered a new player with username " + tempUsername);
  22. System.out.println(" You need to log in to play the game.");
  23.  
  24. //creates new player object using the player's details, with default score & number of games player equal to 0
  25. Player newPlayer = new Player(tempName,tempSurname,tempUsername,tempPassword, 0, 0);
  26.  
  27. //adds the new player's details to the array list
  28. return newPlayer;
  29. }
  30.  
  31. private String setName()
  32. {
  33. Scanner scan = new Scanner(System.in);
  34. System.out.print("\n Enter your first name: ");
  35. String name = scan.nextLine();
  36. while (!InputValidation.nameValidation(name)) //while the user's input does not pass the validation, prompt to enter new name
  37. {
  38. System.out.print("\n Please enter your first name again: ");
  39. name = scan.nextLine();
  40. }
  41. return name;
  42. }
  43.  
  44. private String setSurname()
  45. {
  46. Scanner scan = new Scanner(System.in);
  47. System.out.print("\n Enter your last name: ");
  48. String surname = scan.nextLine();
  49. while (!InputValidation.nameValidation(surname))//while the user's input does not pass the validation, prompt to enter new surname
  50. {
  51. System.out.print("\n Please enter your last name again: ");
  52. surname = scan.nextLine();
  53. }
  54. return surname;
  55. }
  56.  
  57. private String setUsername(List<Player> thePlayerList)
  58. {
  59. Scanner scan = new Scanner(System.in);
  60. System.out.print("\n Guidelines for choosing your username: ");
  61. System.out.print("\n 1) Your player username needs to be at least 3 characters long and no more than 12.");
  62. System.out.print("\n 2) The first character can not be a number.");
  63. System.out.print("\n 3) The username can not contain any special characters. ");
  64. System.out.print("\n Enter your desired player username: ");
  65. String username = scan.nextLine();
  66.  
  67. //program will prompt user to choose a different username if the username they have entered does not pass
  68. //the validation and if another user has already registered with the same username
  69. while ((!InputValidation.usernameValidation(username)) || (!isUsernameAvailable(username, thePlayerList)))
  70. {
  71. System.out.print("\n Please enter a new username: ");
  72. username = scan.nextLine();
  73. }
  74. return username.toLowerCase(); //all usernames are case-insensitive
  75. }
  76.  
  77. public boolean isUsernameAvailable(String aUsername, List<Player> thePlayerList)
  78. {
  79. boolean valid = true;
  80. for (int i=0; i < thePlayerList.size(); i++) //loops through the player list
  81. {
  82. //for every player, checks whether the user's input matches an already registered player's username
  83. if (aUsername.equalsIgnoreCase(thePlayerList.get(i).getUsername()))
  84. {
  85. System.out.print("\n This username is already in use.");
  86. valid = false; //if it does, the method returns a false value so the loop in the calling method can repeat
  87. break; //usernames will be unique, so we know there won't be another one, so no need to continue the loop
  88. }
  89. }
  90. return valid;
  91. }
  92.  
  93. private String setPassword()
  94. {
  95. Scanner scan = new Scanner(System.in);
  96. System.out.print("\n Guidelines for choosing your password: ");
  97. System.out.print("\n 1) Your password needs to be at least 8 characters long but no more than 15.");
  98. System.out.print("\n 2) Your password should contain at least one uppercase letter");
  99. System.out.print("\n 3) Your password should contain at least one special character. ");
  100. System.out.print("\n Enter your desired player password: ");
  101. String password = scan.nextLine();
  102. while (!InputValidation.passwordValidation(password)) //while the user's input does not pass the validation, prompt to enter new password
  103. {
  104. System.out.print("\n Please enter a new password: ");
  105. password = scan.nextLine();
  106. }
  107. return password;
  108. }
  109.  
  110. //method of type integer which returns the value of the player logged in
  111. //if the user quits during the login process, the value returned will be the initial value of the current player
  112. //if the user successfuly logs in, the value returned will be that of the new user
  113. public int Login(List<Player> thePlayerList, int theCurrentPlayer)
  114. {
  115. int tempPlayer = -1;
  116. //user will only be allowed to login after they have registered
  117. //if the ArrayList is empty it means no player profiles (objects) have been created yet
  118. if (thePlayerList.isEmpty())
  119. {
  120. System.out.println("\n You need to be registered before you can login.");
  121. System.out.println(" Register by choosing R from the main menu.");
  122. System.out.println();
  123. return theCurrentPlayer; //redundant
  124. }
  125. else
  126. {
  127. Scanner scan = new Scanner(System.in);
  128. System.out.println("\n Enter your username: ");
  129. String verification = scan.nextLine();
  130.  
  131. //loops through the array to check whether the username entered by the user (ignoring case)
  132. //is equal to any of the usernames in the player database
  133. for (int i=0; i < thePlayerList.size(); i++)
  134. {
  135. if (verification.equalsIgnoreCase(thePlayerList.get(i).getUsername()))
  136. {
  137. tempPlayer = i;
  138. break; //once a match has been found no need to run the whole loop (unique usernames)
  139. }
  140. }
  141.  
  142. //if true means no username was matched (tempPlayer was initialised as -1)
  143. while(tempPlayer == -1)
  144. {
  145. System.out.println("\n No such username exists.");
  146. System.out.println(" Please enter your username again, or type 'quit' to quit the login process: ");
  147. verification = scan.nextLine();
  148. //if user wants to quit
  149. if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
  150. {
  151. break; //exits the username verification loop
  152. }
  153. else
  154. {
  155. for (int i=0; i < thePlayerList.size(); i++)
  156. {
  157. if (verification.equalsIgnoreCase(thePlayerList.get(i).getUsername()))
  158. {
  159. tempPlayer = i;
  160. break; //exits the username verification loop
  161. }
  162. }
  163. }
  164. }
  165.  
  166. //need to repeat this check in the case the user wants to quit, in order to exit the whole method
  167. if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
  168. {
  169. return theCurrentPlayer; //will exit method as user no longer wants to continue
  170. //the player logged in is the same one as before the login process was invoked
  171. }
  172.  
  173. //if username is correct, we continue the verification process
  174. System.out.println("\n Enter your password: ");
  175. verification = scan.nextLine();
  176. while(!thePlayerList.get(tempPlayer).getPassword().equals(verification))
  177. {
  178. System.out.println("\n You have entered a wrong password.");
  179. System.out.println(" Please enter your password again, or type 'quit' to quit the login process: ");
  180. verification = scan.nextLine();
  181. if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
  182. {
  183. break; //escapes this while loop
  184. }
  185. }
  186.  
  187. //need to repeat this check in the case the user wants to quit, in order to exit the whole method
  188. if (verification.equalsIgnoreCase("quit")||verification.equalsIgnoreCase("'quit'"))
  189. {
  190. return theCurrentPlayer; //will exit method as user no longer wants to continue
  191. //the player logged in is the same one as before the login process was invoked
  192. }
  193.  
  194. //if the interpreter reaches this point it means that the user has managed to log in successfuly
  195. System.out.println("\n Welcome back " + thePlayerList.get(tempPlayer).getName() + "!");
  196. System.out.println(" Your total score is " + thePlayerList.get(tempPlayer).getTotalScore() + ".");
  197. System.out.println();
  198. return tempPlayer; //changes the new currently logged in Player
  199.  
  200. }
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement