2022-05-18

My login method returns both true and false output, why?

I am still learning how to code by self thought so it is kind of difficult to learn alone, i often use other code for reference but i still cannot figure out what is wrong with my code.

    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        
        System.out.println("[1] Register");
        System.out.println("[2] LogIn as Client");
        System.out.println("[3] LogIn as Admin");
        System.out.println("[4] Exit");
        
        System.out.print("Enter your choice here: ");
        int choice = input.nextInt();
        switch(choice){
            case 1:
                System.out.println("[Register Your Account]");

                System.out.print("-----Enter your name: ");
                String name = input.next();
        
                System.out.print("-----Enter your desired 4-digit pin: ");
                int pin = input.nextInt();
          
                System.out.print("-----Enter your first deposit: ");
                int balance = input.nextInt();
                
                account client = new account(name,pin,balance);
                
                client.setName(name);
                client.setPin(pin);
                client.setBalance(balance); 
                
                addToArrayList(client);
  
            case 2:
                System.out.println("[LogIn Your Account]");
                System.out.println("----Enter Your Pin: ");
                int logPin = input.nextInt();
                
                logIn(logPin);
        
                break;

            case 3:
                System.out.println("Case 3");
                

                break;
                
            case 4:
                System.out.println("Case 4");
                break;
        }
}  
    
    static void addToArrayList(account client){
        userList.add(client);
        
        displayRegister(userList);   
    }
    
    static void displayRegister(ArrayList<account> users){
        for (account client : users){
        System.out.println("(Here's what we received)");
        System.out.println(client.getName());
        System.out.println(client.getPin());
        System.out.println(client.getBalance());

        }
    }

why does my login give an output of both TRUE AND FALSE when i entered a correct pin after register, and if i entered a wrong pin the program ends?

    static void logIn (int logPin){
        if(!userList.isEmpty()){
                    int i = 0;
                    boolean isNotExist = false;
                    
                    for(var acc: userList){
                        int pinCode = acc.getPin();
                        
                        if(logPin==acc.getPin()){
                            System.out.println("(LogIn Successfully!)");
                            isNotExist=true;
                            mainMenu();
                        }
                        else{
                            i++;
                        }
                    }
                    if(isNotExist){
                        System.out.println("(Account does not exist...)");
                    }
                }else{
                    System.out.println("(No data...)");
         }
               
    }


No comments:

Post a Comment