Very simple question but very little luck.
My problem is that when I attempt to call on get methods getUserID
and getPassword
of class User
in class Login
, I get null.
I have looked at other posts with a similar problem to mine. Although I found few posts to guide me, a common issue was that the poster had not initialised the class object but I have done that.
I need specific code here because I've tried a lot of things but can't get it to work. I hope that I provided enough info to help you assist me.
public class User
{
private String userID;
private String password;
private Employee employee;
private String authorityLevel;
/**
* Constructor for User class - Initialise a fixed password and employee object.
*/
public User()
{
employee = new Employee();
password = "password";
}
/**
* Create a user ID and print the user the details of their user account.
*/
public void createUser(Employee employee)
{
// Combine staff ID with authority key to make the user ID.
userID = employee.getID() + "" + employee.getAuthorityLevel();
// Check that there is a staff ID to create the user ID.
// It also ensures that an employee profile has been created before an attempt
// to make a user account.
if(employee.getID() == null){
System.out.println("There are no Employee details to make a User with.");
System.out.println("Please enter the Employee details before you make a user");
}
else{
System.out.println("Your user ID is: "+userID);
System.out.println("Your user password is: "+password);
}
}
/**
* @return The user ID.
*/
public String getUserID()
{
return userID;
}
/**
* @return The password.
*/
public String getPassword()
{
return password;
}
}
I want to access the userID and password getter methods in the User class to use in the Login class.
public class Login
{
private User user;
private boolean accessGranted;
private String userID;
private String password;
private boolean loggedIn;
private boolean loggedOut;
/**
* Constructor for the Login class - initialise a user object.
*/
public Login()
{
user = new User();
}
/**
* Attempt to start a login session.
*/
public void login(String userID,String password)
{
// Check that credentials entered are correct for the account the user wishes to log in to.
if((password == user.getPassword()) && (userID == user.getUserID())){
accessGranted = true;
if((accessGranted == true) && (userID.contains("H"))){
System.out.println("Your login session has started.");
System.out.println("You are now viewing Yuconz System as HR staff.");
}
if((accessGranted == true) && (userID.contains("D"))){
System.out.println("Your login session has started.");
System.out.println("You are now viewing Yuconz System as Director staff.");
}
if((accessGranted == true) && (userID.contains("E"))){
System.out.println("Your login session has started.");
System.out.println("You are now viewing Yuconz System as Employee staff.");
}
loggedIn = true;
}
else{
System.out.println("ACCESS DENIED BRUTHA!");
}
}
One class creates the user account where the only details are a userID and a password. The other class is where the user can use the details of that account to login. In Login
's login
method I am checking that the ID and password entered match the ID and password of the account that they are trying to access.
It seems that you never call public void createUser(Employee employee)
on the instance of User.
SO
private String userID;
in never initialized...
Try this :
public class User{
private String userID;
private String password;
private Employee employee;
private String authorityLevel;
public User(Employee employee){
this.employee = employee;
password = "password";
createUser();
}
private void createUser(){
userID = employee.getID() + "" + employee.getAuthorityLevel();
if(userID == null){
System.out.println("There are no Employee details to make a User with.");
System.out.println("Please enter the Employee details before you make a user");
}else{
System.out.println("Your user ID is: "+userID);
System.out.println("Your user password is: "+password);
}
}
public String getUserID(){
return userID;
}
public String getPassword(){
return password;
}
}
Passing the Employee the the User's constructor is the best solution if you need an instance of Employee to creat a User.
If you do not have any instance of Employee
in your login but just an id
and a password
then you can change the User's constructor : :
public class User{
private String userID;
private String password;
private String authorityLevel;
public User(String userID, String password){
this.userID = userID;
this.password = password;
checkUser();
}
private void checkUser(){
if(userID == null){
System.out.println("There are no Employee details to make a User with.");
System.out.println("Please enter the Employee details before you make a user");
}else{
System.out.println("Your user ID is: " + userID);
System.out.println("Your user password is: " + password);
}
}
//...
}
You can instanciate the User
in the Login
public class Login
private User user;
public Login(){
}
public void login(String userID,String password){
user = new User(userID, password);
//...
}
The strings userID
and password
belong to the Employee
object. You're not initialising those values in your User
class, this is why they are null
.
You could do it like this
public String getUerID() {
return employee.getUserID();
If you want them to belong to the User
class the way you declared them in the class, you need to change your User
constructor. eg
public User(String id, String pwd) {
this.userID = id;
this.password = pwd;
}
Then you need to figure out if you still want the Employee object or not.
You should reconsider some design decisions
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.