mercredi 6 avril 2016

How to create a login System that reads usernames & passwords from another class and compares them to what the user has entered?

I'm creating a login system that gets a username and password from another Class (Drivers). I'm using an if equals statement for the menu. If the user enters text that matches what's inside the other class (Drivers) then the login works. If it doesn't match in three attempts then it force logs them out. However when I enter the correct username and password its not being recognised. I don't think that the strings from the other class (Drivers) is being recognised. Any help on how to fix what I've got so far would be highly appreciated.

Here's the code that I have so far.

package edoptsystem;


import java.util.Scanner;

import edoptsystem.Driver;


public class Sys {
static Scanner console = new Scanner(System.in);
static Scanner console1 = new Scanner(System.in);
static Driver myDriver = new Driver();
static Driver DriverPass = new Driver();


public static String DepotLoc;

public static void setDepotLoc(String Manchester) {
    DepotLoc = Manchester;
}

public static String getDepotLoc() {
    return DepotLoc;
}

public static void main(String[] args) {

    String username;
    String password;

    System.out.println("Enter one of the following comands:");
    System.out.println("1 = log On");
    System.out.println("2 = Quit");

    int choice = console.nextInt();
    if (choice == 2) {
        System.out.println("GoodBye!");
        System.exit(0);// Exits the program.

    }

    for (int i = 1; i <= 3; i++) { // For loop,
        System.out.println("Input your depot location:    Liverpool, Manchester, Bristol");
        String depot1 = console1.nextLine();


        System.out.println("Input your username:");
        username = console1.nextLine();


        System.out.println("Input your account password:");
        password = console1.nextLine();


        Driver.getDriver();
        Driver.getDriverPass();
        if (depot1.equals(DepotLoc)
                && (username.equals(Driver.getDriver())) && (password.equals(Driver.getDriverPass()))) { //
            System.out.println("Log in successful");
            break;
        } else {
            if (i == 3) //
            {
                System.out.println("you have tried three times, good bye!");
                System.exit(0);
            } else
                System.out.println("wrong combination, try again!");

        }


    }

}
}

Here's the second class.

package edoptsystem;

public class Driver {

public static String myDriver;

public static void setDriver(String username) {
    myDriver = "Joe";
}

public static String getDriver() {
    return myDriver;
}

public static String DriverPass;

public static void setDriverPass(String password) {
    DriverPass = "Password";
}

public static String getDriverPass() {
    return DriverPass;
}

   }

Aucun commentaire:

Enregistrer un commentaire