Advertisement
Guest User

Untitled

a guest
May 9th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. import Users.Cashier;
  2. import Users.Manager;
  3. import Users.Trainer;
  4. import Users.User;
  5.  
  6. import java.util.Scanner;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. public class LoginScreen {
  11.  
  12. //Creating a variable that later determines what kind of user signed in
  13. int result;
  14.  
  15. //Creating a method that runs the login, with a int value as returntype
  16. public int runLogin() {
  17.  
  18. //Creating variables that the user types in
  19. int ID;
  20. String Password;
  21.  
  22. //Creating variables, that are used later to determine if user has signed in, and what role
  23. User Loggedinuser = null;
  24. String Roleuser = "";
  25.  
  26. //Creating a arraylist
  27. List<User> listOfUsers = new ArrayList<>();
  28.  
  29. //Adding items to the arraylist, using the different constructers that is inherited from the superclass (Users.User)
  30. listOfUsers.add(new Trainer(31243, "Test123", "Trainer"));
  31. listOfUsers.add(new Cashier(21300, "Test321", "Cashier"));
  32. listOfUsers.add(new Manager(11300, "Hej123", "Manager"));
  33. listOfUsers.add(new Trainer(31200,"1234","Trainer"));
  34.  
  35. //Adding scanners - we are adding because or else the nextInt will skip nextLine
  36. Scanner inp = new Scanner(System.in);
  37. Scanner intInp = new Scanner(System.in);
  38.  
  39. System.out.println();
  40. System.out.println();
  41.  
  42. //Creating a boolean value, so the program keeps running if a mistake is made, and so we can break it when we want
  43. boolean breakout = true;
  44. while (breakout) {
  45. System.out.println("Dolphin sign in system");
  46. System.out.println();
  47.  
  48. //Asking user to type in, and fill the variables we made previously
  49. System.out.print("ID: ");
  50. ID = inp.nextInt();
  51. System.out.print("Password: ");
  52. Password = intInp.nextLine();
  53.  
  54. //We ask that it looks through the arraylist we made
  55. for (User user : listOfUsers) {
  56. //If the userinput ID matches one on the arraylist
  57. if (user.getID()==(ID)) {
  58. //Then if also the user password matches that spot on the arraylists password
  59. if (user.getPassword().equals(Password)) {
  60. //Then we set the variable we made earlier to user
  61. Loggedinuser = user;
  62. break;
  63. }
  64. }
  65. }
  66. //If the variable we made earlier Loggedinuser is something else than null, then the user successfully signed in
  67. if (Loggedinuser != null) {
  68. System.out.println();
  69. System.out.println("User signed in: " + Loggedinuser.getID() + " as " + Loggedinuser.getClearance());
  70. System.out.println();
  71. Roleuser = Loggedinuser.getClearance();
  72. breakout = false;
  73. //If that is not the case, then the username or password must have been wrong, since the value started as null
  74. } else {
  75. System.out.println();
  76. System.out.println("Invalid username/password, try again");
  77. System.out.println();
  78. }
  79. }
  80. //Here we return values depending on what clearance they signed in with, used later to determine what access they have
  81. if (Roleuser.equalsIgnoreCase("Manager")) {
  82. result = 1;
  83. } else if (Roleuser.equalsIgnoreCase("Cashier")) {
  84. result = 2;
  85. } else if(Roleuser.equalsIgnoreCase("Trainer")){
  86. result = 3;
  87. }
  88. return result;
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement