简体   繁体   中英

Login and seperate menus for two different types of user

I'm new to java and programming. I am stuck on one section of an assignment given to me in which I have to create a login for two different types of user which will display two different menus depending on which login is used. I am using Eclipse and the console.

The two different types of user are Boss and Worker and they must login using a username and password. The Boss menu must have the following menu options after logging in:

  1. Setup Worker Schedule
  2. View Worker Schedule
  3. Move Worker

The Worker menu must have the following menu options after logging in:

  1. View Schedule

I'd really appreciate any help with this, thanks in advance.

EDIT: Okay, so I now have the following code:

import java.util.Scanner;

public class Depot {
    public static void main(String[] arguments){

        String bossName;
        String bossPassword;
        String workerName;
        String workerPassword;

        System.out.println("Enter your name: ");
        Scanner authenticate = new Scanner(System.in);
        String userName = authenticate.nextLine();
        System.out.println("Your username is " + userName);

        System.out.println("Enter your password: ");
        String passWord = authenticate.nextLine();
        System.out.println("Your password is " + passWord);

        if (userName.equals(bossName) && passWord.equals(bossPassword)) {

            int selection;

            Scanner bossMenu = new Scanner(System.in);
            System.out.println("1. Setup Worker Schedule");
            System.out.println("2. View Worker Schedule");
            System.out.println("3. Move Worker");
            System.out.println("4. Quit");

            do {
                selection = bossMenu.nextInt();

                if (selection == 1) {
                    System.out.println("1");
                }
                else if (selection == 2) {
                    System.out.println("2");
                }
                else if (selection == 3) {
                    System.out.println("3");
                }
                else {
                    System.out.println("4");
                }  
            } 
                while(selection != 4);
                bossMenu.close();
        }
        else if (userName.equals(workerName) && passWord.equals(workerPassword)) {

            int selection;

            Scanner userMenu = new Scanner(System.in);
            System.out.println("1. View Worker Schedule");
            System.out.println("2. Quit");

            do {
                selection = userMenu.nextInt();

                if (selection == 1) {
                    System.out.println("1");
                }
            } 
                while(selection != 2);
                userMenu.close();
        }
    }
}

However, the following two lines of code are giving me an error:

if (userName.equals(bossName) && passWord.equals(bossPassword)) {

and

else if (userName.equals(workerName) && passWord.equals(workerPassword)) {

bossName, bossPassword, workerName and workerPassword may not have been initialized?

First, get the credentials through using the Scanner, here is a basic way to construct a Scanner object, you will need to have the following import statement at the very beginning of your code, before anything else:

import java.util.Scanner;

To create a Scanner, do the following:

Scanner scannerName = new Scanner(System.in);

That tells the Scanner to read from the input stream, which will be the keyboard. To get data from the Scanner, first prompt the user for the data you need, then use one of the Scanner's .next___ methods to retrieve the input and store in a variable. I'm not going to tell you which one to use, check out the Scanner page in the Java API and see if you can figure it out on your own.

It should look something like this:

System.out.println("Enter your name");
String userLoginString = scannerName.next____();

System.out.println("Enter your password");
String userPasswordString = scannerName.next____();

Once you have the credentials stored in String variables, I'll use userLoginString and userPasswordString as examples, you will need to validate these credentials against some stored values. So, create String variables bossName, bossPassword, workerName, workerPassword.

Once you have the user's credentials, I would perform validation on these login credentials. You could do that using the logical operators and methods of the String class, like so:

if (userLoginString.equals(bossName) && userPasswordString.equals(bossPassword)) {
    // print the boss menu
}
else if (userLoginString.equals(workerName) && userPasswordString.equals(workerPassword)) {
    // print the user menu
}

The logical && ("and") operator will ensure that the correct menu will be displayed only if the user's credentials match the stored credentials. If the user enters the correct name (boss or worker) but the wrong password (or vice-versa), the statements inside the braces will NOT execute.

UPDATE Here is a commented version of your code so far with some hints as to how to make it better. It will compile and run fine if you just provide values for the String variables at the top, but I have some more suggestions to make it a little nicer:

import java.util.Scanner;

public class Depot {
    public static void main(String[] arguments){

        // you need to initialize these to some value or else there is
        // nothing to compare them with. I tried some dummy values and
        // your code worked as expected, as long as the user entered the
        // correct values in the prompt.
        String bossName;
        String bossPassword;
        String workerName;
        String workerPassword;

        // you can just use one Scanner for the whole program, since they are
        // both just reading input from the standard input stream. Replace the
        // other Scanners with "input" and close "input" at the end
        Scanner input = new Scanner(System.in);

        System.out.println("Enter your name: ");
        // not needed
        Scanner authenticate = new Scanner(System.in);
        String userName = authenticate.nextLine();
        System.out.println("Your username is " + userName);

        System.out.println("Enter your password: ");
        String passWord = authenticate.nextLine();
        System.out.println("Your password is " + passWord);

        if (userName.equals(bossName) && passWord.equals(bossPassword)) {

            // this could be declared at the top of the program instead of
            // redeclaring in the if...else
            int selection;

            Scanner bossMenu = new Scanner(System.in);
            System.out.println("1. Setup Worker Schedule");
            System.out.println("2. View Worker Schedule");
            System.out.println("3. Move Worker");
            System.out.println("4. Quit");

            do {
                selection = bossMenu.nextInt();

                if (selection == 1) {
                    System.out.println("1");
                }
                else if (selection == 2) {
                    System.out.println("2");
                }
                else if (selection == 3) {
                    System.out.println("3");
                }
                else {
                    System.out.println("4");
                }  
            } while(selection != 4); // this is usually here
                bossMenu.close();
        }
        else if (userName.equals(workerName) && passWord.equals(workerPassword)) {

            // this could be declared at the top of the program instead of
            // redeclaring in the if...else
            int selection;

            // don't need this one, just use "input" Scanner
            Scanner userMenu = new Scanner(System.in);
            System.out.println("1. View Worker Schedule");
            System.out.println("2. Quit");

            do {
                selection = userMenu.nextInt();

                if (selection == 1) {
                    System.out.println("1");
                }
            } while(selection != 2); // this is usually here

            // you would close the "input" Scanner here
            userMenu.close();
        }
    }
}

UPDATED AGAIN!!! A better way to implement the Boss and Worker would be through using inheritance and polymorphism. Start with an abstract superclass that has common characteristics of the Boss and Worker. I'll call this the Employee superclass. It has firstName, lastName, and password instance variables, and you should add getters and setters for each:

// abstract class, CANNOT be instantiated but can be used as the supertype
// in an ArrayList<Employee>
public abstract class Employee {

    private String firstName;
    private String lastName;  
    private String password;

    public Employee() {
        // don't have to do anything, just need this so you can instantiate
        // a subclass with a no-arg constructor
    }

    // constructor that takes only the name of the Employee
    public Employee(String firstName, String lastName) {
        this(firstName, lastName, null);
    }

    // constructor that takes name and password
    public Employee(String firstName, String lastName, String password) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.password = password;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    // and so on, for the lastName and password....

    // you must implement this specifically in any subclass!
    public abstract void getMenu();
}

Then, your Boss and Worker classes could extends this Employee class and they would have all of the same methods and instance variables. You just must provide an overridden getMenu() method in each, since that one was abstract in the Employee class. Here is a sample of what your Boss class should look like, you need to implement the getMenu() yourself and the Worker class:

public class Boss extends Employee {

    // notice we don't need the instance variables in the class declaration,
    // but they are here since they are part of Employee

    public Boss() {
        // don't need to do anything here, just allows no-arg constructor
        // to be called when creating a Boss
    }

    // just calls the superclass constructor, could do more if you want
    public Boss(String firstName, String lastName) {
        super(firstName, lastName);
    }
    // just calls the superclass constructor, could do more if you want
    public Boss(String firstName, String lastName, String password) {
        super(firstName, lastName, password);
    }

    @Override
    public void getMenu() {
        // put the print statment for Boss's menu here
    }

    // don't need to re-implement other methods, we can use them since
    // they are part of the superclass
}

Once you have the Employee, Worker, and Boss classes, you're ready to try and re-write your program to Objects in place of simple variables as you were doing before. Here is an example of how that would get started:

import java.util.Scanner;

public class EmployeeTester {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        // can make workers and bosses able to be processed polymorphically
        // by assinging their references to Employee variables, since 
        // and Employee is the supertype of each, a Worker "is an" Employee
        // and a Boss "is an" Employee.
        Employee worker1 = new Worker("Bob", "Worker");
        Employee worker2 = new Worker("Sue", "Bush", "Password1"); 
        Employee worker3 = new Worker();
        Employee boss1 = new Boss("Jenny", "Boss");
        Employee boss2 = new Boss("Bill", "OtherBoss", "Password2");
        Employee boss3 = new Boss();

        // if you're going to have a lot of workers and bosses, and you don't
        // need named variables for each because their info will be included
        // in their constructors, you could do this
        Employee[] employees = {new Worker("Bob", "Bailey", "myPassword"),
                                new Worker("Sue", "Sarandon", "123Seven"),
                                new Boss("Jenny", "Strayhorn", "hardPassword"),
                                new Boss("Billy", "MeanGuy", "pifiaoanaei")};

        // then, you could iterate through this list to check if a password
        // entered matches a firstName, lastName, and password combination
        // for ANY type of employee in the array, then call the getMenu()
        // method on that employee, like so: (This could all be in a loop
        // if you wanted to process multiple Employees...)

        System.out.println("Enter firstName:");
        // you figure out which Scanner method to use!
        String firstName = input._____(); 

        System.out.println("Enter lastName:");
        String lastName = input._____();

        System.out.println("Enter password:");
        String password = input._____();

        // figure out what get____() method of the Employee class
        // needs to be called in each case, and what it should be 
        // compared to with the .equals() method.
        for (int i = 0; i < employees.length; i++) {
            if (employees[i].get______().equals(______) &&
                employees[i].get______().equals(______) &&
                employees[i].get______().equals(______)) {

                // if all of those conditions are true, print the menu
                // for this employee
                employees[i].get_____();

                // you could do more stuff here....

                // breaks out of the loop, no need to check for anymore employees
                break;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM