Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Users.Cashier;
- import Users.Manager;
- import Users.Trainer;
- import Users.User;
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.List;
- public class LoginScreen {
- //Creating a variable that later determines what kind of user signed in
- int result;
- //Creating a method that runs the login, with a int value as returntype
- public int runLogin() {
- //Creating variables that the user types in
- int ID;
- String Password;
- //Creating variables, that are used later to determine if user has signed in, and what role
- User Loggedinuser = null;
- String Roleuser = "";
- //Creating a arraylist
- List<User> listOfUsers = new ArrayList<>();
- //Adding items to the arraylist, using the different constructers that is inherited from the superclass (Users.User)
- listOfUsers.add(new Trainer(31243, "Test123", "Trainer"));
- listOfUsers.add(new Cashier(21300, "Test321", "Cashier"));
- listOfUsers.add(new Manager(11300, "Hej123", "Manager"));
- listOfUsers.add(new Trainer(31200,"1234","Trainer"));
- //Adding scanners - we are adding because or else the nextInt will skip nextLine
- Scanner inp = new Scanner(System.in);
- Scanner intInp = new Scanner(System.in);
- System.out.println();
- System.out.println();
- //Creating a boolean value, so the program keeps running if a mistake is made, and so we can break it when we want
- boolean breakout = true;
- while (breakout) {
- System.out.println("Dolphin sign in system");
- System.out.println();
- //Asking user to type in, and fill the variables we made previously
- System.out.print("ID: ");
- ID = inp.nextInt();
- System.out.print("Password: ");
- Password = intInp.nextLine();
- //We ask that it looks through the arraylist we made
- for (User user : listOfUsers) {
- //If the userinput ID matches one on the arraylist
- if (user.getID()==(ID)) {
- //Then if also the user password matches that spot on the arraylists password
- if (user.getPassword().equals(Password)) {
- //Then we set the variable we made earlier to user
- Loggedinuser = user;
- break;
- }
- }
- }
- //If the variable we made earlier Loggedinuser is something else than null, then the user successfully signed in
- if (Loggedinuser != null) {
- System.out.println();
- System.out.println("User signed in: " + Loggedinuser.getID() + " as " + Loggedinuser.getClearance());
- System.out.println();
- Roleuser = Loggedinuser.getClearance();
- breakout = false;
- //If that is not the case, then the username or password must have been wrong, since the value started as null
- } else {
- System.out.println();
- System.out.println("Invalid username/password, try again");
- System.out.println();
- }
- }
- //Here we return values depending on what clearance they signed in with, used later to determine what access they have
- if (Roleuser.equalsIgnoreCase("Manager")) {
- result = 1;
- } else if (Roleuser.equalsIgnoreCase("Cashier")) {
- result = 2;
- } else if(Roleuser.equalsIgnoreCase("Trainer")){
- result = 3;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement