简体   繁体   中英

Library System Login Logout

class Test{
    public static void main(String args[])
    {
        Patron list[] = new PatronData().getPatronData();

        /*for(Patron p: list)
        {
            System.out.println(p);
        }*/
    }
    }


    class PatronData{
    //Patron patron[] = {new     Patron("Daniel","A001","15WAD00001","A4701,Jalan Kepong,     Pahang","JK01",0.00,"012-8765432"),
    //               new Patron("Chiam","A002","15WAD00002","A4702,Jalan     Akar,Pahang","JK02",0.00,"0102288554")};
    Patron patron[] = new Patron[2];

    public Patron[] getPatronData()
    {
        patron[0] = new Patron("Daniel","A001","15WAD00001","A4701,Jalan     Kepong, Pahang","JK01",0.00,"012-8765432");
        patron[1] = new Patron("Chiam","A002","15WAD00002","A4702,Jalan     Akar,Pahang","JK02",0.00,"0102288554");
        return patron;
    }
}

class Patron{
    private String userName;
    private String password;
    private String userCode;
    private String streetAddress;
    private String postCode;
    private double overdueBalance;
    private String phoneNumber;

    Patron(String userName[], String password[], String userCode, 
    String streetAddress, String postCode, double overdueBalance, String     phoneNumber)
    {
        this.userName = userName;
        this.password = password;
        this.userCode = userCode;
        this.streetAddress = streetAddress;
        this.postCode = postCode;
        this.overdueBalance = overdueBalance;
        this.phoneNumber = phoneNumber;

        int logNMatch = 0;

        Scanner scan = new Scanner(System.in);
        do{

            System.out.print("Please Enter Your User Name > ");
            String inputUserName=scan.nextLine();
            System.out.println();
            System.out.print("Please Enter Your Password  > ");
            String inputPassword = scan.nextLine();

            if(userName.compareTo(inputUserName) == 0 &&     password.compareTo(inputPassword) == 0)
            {
                System.out.println("Logging Successful");
                System.out.print("\n\n");

            }
            else
            {
                System.out.println("Loging fail");
                System.out.println("Please again later");
                logNMatch++;
            }
        }while(logNMatch > 0);
    }
    }

Hey guys, I am learning Java in Diploma Level. I have a question. Please, I have no idea why I cannot straight away logging into "Chiam Account"I expected is when i log in the compiler will automatically check whether is the login detail match with the data in library system.

You are requesting the login information inside the constructor. Meaning that whenever you make a new Patron it will prompt you to login with that user's information.

Instead remove everything inside that do/while loop and add a method like loginFromLibrary() that will prompt the user to input their name and password. Then check all of the Patron s to see if any of their names match the username given. Then just make sure that the username matches the password.

This example will require some getter ( getPassword() and getUsername() ):

public void loginFromLibrary(Patron[] patrons){
    Scanner scan = new Scanner(System.in);
    while (true){
        // get usernmae
        System.out.println("Username > ");
        String username = scan.nextLine();
        Patron user = null;
        // check array to see if username exists
        for (Patron p : patrons){
            if (p.getUsername().equals(username)){
                user = p;
                break;
            }
        }
        if (user == null){
            // username not found
            System.out.println("Username not found");
            continue;
        }
        // get password
        System.out.println("Password > ");
        String pass = scan.nextLine();
        // check password
        if (pass.equals(user.getPassword())){
            // logged in
            break;
        } else {
            // wrong password
        }
    }
    scan.close();
}

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