首次登录无效,第二次有效

时间:2017-08-14 05:40:46

标签: java

这是一个读取文件的程序,如果它不存在或为空,它会调用一个方法将对象添加到ArrayList,然后调用将该对象写入文件的WriteFile方法。

try{
        FileReader fr = new FileReader(managerFile);
        BufferedReader br = new BufferedReader(fr);


    }catch (IOException e){
        System.out.println("New System. Register as Manager to start using it.");
        manager.add(AddManager());
        WriteFile(managerFile, manager);
    }
    do{
        try{
            select = Personnel();

            switch(select){
                case 1:
                    if(BLogIN(beautician)){
                        Action(select, Menu(select),customer);
                    }
                    else{
                        Main.main(args);
                    }
                    break;
                case 2:
                    if(MLogIN(manager)){
                        Action(select, Menu(select), customer);
                    }
                    else{
                        Main.main(args);
                    }
                    break;
                case 3:
                    break;
                default:
                    System.out.println("Invalid Selection.");
                    System.out.println("Please choose only from the options above");
                    System.out.println("");
                    break;
            }
        }catch (InputMismatchException e){

        }
    }while(select < 1 || select > 3);

写入文件后,它调用Login方法将早期的ArrayList传递给此方法以处理登录

public static boolean MLogIN(ArrayList<Manager> a){

    boolean valid = true;

    Scanner scan = new Scanner(System.in);

    System.out.print("Enter Manager ID :");
    String id = scan.next();
    System.out.print("Enter Password :");
    String pass = scan.next();
    for(int i = 0; i < a.size(); i++){
        valid = id.equals(a.get(i).getID());
        valid = pass.equals(a.get(i).getPass());
    }

    return valid;
}

输入正确的ID和密码后,它表示无效,但我第二次登录,则有效。为什么呢?

1 个答案:

答案 0 :(得分:-1)

您检查用户输入是否正确的条件不正确。 它应该如下所示:

public static boolean MLogIN(ArrayList<Manager> a){

    boolean valid = false;

    Scanner scan = new Scanner(System.in);

    System.out.print("Enter Manager ID :");
    String id = scan.next();
    System.out.print("Enter Password :");
    String pass = scan.next();

    for(int i = 0; i < a.size(); i++) {
        if (id.equals(a.get(i).getID()) && pass.equals(a.get(i).getPass())) {
            valid = true;
            break;
        }
    }

    return valid;
}