如何从他的登录信息打印用户类型?

时间:2018-11-15 19:03:49

标签: java login

我无法使用帐户类型连接登录信息,就像我想在输入管理员的用户名和密码时打印“ Welcome Manager”一样。

    UserAccount users[] = new UserAccount[3];
    users[0] = UserAccount.createUser("test0", "test0"); <<Username,Pass>>
    users[1] = UserAccount.createUser("test1", "test1");
    users[2] = UserAccount.createUser("test2", "test2");

    personArray = new Person[3]; <<third paramter take UserAccount>>
    personArray[0] = new Manager("Zen","manager@mail.com",users[0],new Date(2/3/2013));
    personArray[1] = new Specialist("Tom","specialist@mail.com",users[1]);
    personArray[2] = new Customer("John","customer@mail.com",users[2],"ny");


    Scanner s = new Scanner(System.in);

    System.out.println("Main menu");
    System.out.print("(1) User login \n(2) new Customer \n(3) Exit \n==> ");
    int inp = s.nextInt();
    switch (inp) {
    case 1: 
        UserAccount loggedUser = UserAccount.login();

        if (loggedUser != null) {
            System.out.println("----------------------\nHello " + loggedUser.getUsername());
            System.out.println("Welcome to the ITS App\n----------------------");
            <<I want to print here the account type>>
        }

2 个答案:

答案 0 :(得分:0)

如果您可以获取与UserAccount关联的人员,则可以执行以下操作

if(person instanceof Manager){
      System.out.println("Welcome Manager");
}

或者,假设Person是一个接口,则可以放入一个函数String getType(),该函数在实现Person的每个类中实现以返回表示类型的String。因此,在您的个人课程中,您将:

  public String getType();

然后在您的经理班上,您应该输入:

  public String getType(){
       return "Manager";
  }

然后打印出您的消息,您可以执行以下操作:

 System.out.println("Welcome "+person.getType());

答案 1 :(得分:0)

在获得UserAccount后添加此内容:

 Person person = Arrays.stream(personArray).filter(person -> person.getUser().equals(loggedUser)).findFirst().orElse(null);

    if(person instanceof Manager){
    System.out.println("Welcome Manager");
    }