两种不同类型用户

时间:2016-04-05 15:47:06

标签: java eclipse login menu console

我是java和编程的新手。我被困在给我的一个任务的一部分,我必须为两个不同类型的用户创建一个登录,这将根据使用的登录显示两个不同的菜单。我正在使用Eclipse和控制台。

两种不同类型的用户是Boss和Worker,他们必须使用用户名和密码登录。登录后,Boss菜单必须具有以下菜单选项:

  1. 设置工作人员时间表
  2. 查看工作人员时间表
  3. 移动工人
  4. 登录后,工具菜单必须具有以下菜单选项:

    1. 查看时间表
    2. 我非常感谢你提供任何帮助,谢谢你。

      编辑: 好的,我现在有以下代码:

      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();
              }
          }
      }
      

      但是,以下两行代码给出了一个错误:

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

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

      bossName,bossPassword,workerName和workerPassword可能尚未初始化?

1 个答案:

答案 0 :(得分:2)

首先,通过使用扫描程序获取凭据,这是构建扫描程序对象的基本方法,您需要在代码的最开头处包含以下import语句:

import java.util.Scanner;

要创建扫描仪,请执行以下操作:

Scanner scannerName = new Scanner(System.in);

告诉扫描器从输入流中读取,这将是键盘。要从扫描仪获取数据,首先提示用户输入所需数据,然后使用扫描仪的.next ___方法之一检索输入并存储在变量中。我不打算告诉你使用哪一个,查看Java API中的Scanner页面,看看你是否可以自己搞清楚。

看起来应该是这样的:

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

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

一旦您将凭证存储在String变量中,我将使用userLoginString和userPasswordString作为示例,您将需要针对某些存储值验证这些凭据。因此,创建String变量bossName,bossPassword,workerName,workerPassword。

获得用户凭据后,我会对这些登录凭据执行验证。您可以使用String类的逻辑运算符和方法来执行此操作,如下所示:

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

逻辑&& (“和”)运算符将确保仅当用户的凭据与存储的凭据匹配时才会显示正确的菜单。如果用户输入正确的名称(boss或worker)但密码错误(反之亦然),则大括号内的语句将不会执行。

更新 到目前为止,这是您的代码的注释版本,并提供了一些有关如何使其更好的提示。如果你只是为顶部的String变量提供值,它将编译并运行正常,但我还有一些建议可以让它更好一些:

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();
        }
    }
}

再次更新!!! 实现Boss和Worker的更好方法是使用继承和多态。从一个具有Boss和Worker共同特征的抽象超类开始。我将其称为Employee超类。它有firstName,lastName和password实例变量,你应该为每个变量添加getter和setter:

// 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();
}

然后,您的Boss和Worker类可以扩展此Employee类,并且它们将具有所有相同的方法和实例变量。你必须在每个方法中提供一个重写的getMenu()方法,因为那个方法在Employee类中是抽象的。以下是您的Boss类应该是什么样子的示例,您需要自己实现getMenu()和Worker类:​​

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
}

一旦你拥有了Employee,Worker和Boss类,你就可以尝试将程序重新编写为Objects来代替以前的简单变量。以下是如何开始的示例:

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;
            }
        }
    }
}