Guest User

Untitled

a guest
Mar 27th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. /**
  4. * Write a description of class ChangeUser here.
  5. *
  6. * @author Dominic Byers
  7. * @version v1
  8. */
  9. public class ChangeUser
  10. {
  11. // Creates an arraylist of the User class and the scanner that will be used in the methods below
  12. static ArrayList<User> users = new ArrayList<User>();
  13. static Scanner in = new Scanner(System.in);
  14. // User object is the current user
  15. static User currentUser;
  16. /**
  17. * Makes sure it runs the correct code for registering or signing up
  18. */
  19. public static void runUser()
  20. {
  21. System.out.println("Do you want to sign in or register? (sign in should be seperate)");
  22. String statement = in.nextLine();
  23. if (statement.equalsIgnoreCase("sign in"))
  24. {
  25. changeUser();
  26. }
  27. else if (statement.equalsIgnoreCase("register"))
  28. {
  29. register();
  30. }
  31. else
  32. {
  33. runUser();
  34. }
  35. }
  36. /**
  37. * Signs in the user if no user is signed in or changes the user if one is already signed in
  38. */
  39. public static void changeUser()
  40. {
  41. System.out.println("Type your username");
  42. String username = in.nextLine();
  43. System.out.println("Type your password");
  44. String password = in.nextLine();
  45. // Iterates through the arraylist
  46. for (int i = 0; i < users.size(); i++)
  47. {
  48. // Checks to see if the username exists
  49. if (username.equalsIgnoreCase(users.get(i).getUsername()))
  50. {
  51. // If it does, it checks the password
  52. // If the password is right, it signs in the current user
  53. // If not, it says that was an incorrect password
  54. if (password.equalsIgnoreCase(users.get(i).getPassword()))
  55. {
  56. System.out.println("Signed in successfully");
  57. currentUser = users.get(i);
  58. break;
  59. }
  60. else
  61. {
  62. System.out.println("The password did not match");
  63. changeUser();
  64. }
  65. }
  66. else if (i == users.size() - 1) // Checks to see if we're at the end of the iteration, if we are, and the username hasn't equaled any of the users, then it says that the user doesn't exist
  67. {
  68. System.out.println("That user does not exist");
  69. changeUser();
  70. }
  71. }
  72. }
  73. /**
  74. * Registers a new user to the arraylist
  75. */
  76. public static void register()
  77. {
  78. System.out.println("Choose a username for your account");
  79. String regUser = in.nextLine();
  80. System.out.println("Choose a password for your account");
  81. String regPass = in.nextLine();
  82. System.out.println("Type your password again to confirm");
  83. if (in.nextLine().equals(regPass))
  84. {
  85. users.add(new User(regUser, regPass));
  86. }
  87. }
  88. public static User getCurrentUser()
  89. {
  90. return currentUser;
  91. }
  92. }
Add Comment
Please, Sign In to add comment