【发布时间】:2016-08-07 09:21:51
【问题描述】:
很简单的问题,但运气很差。
我的问题是,当我尝试在 Login 类中调用 User 类的 get 方法 getUserID 和 getPassword 时,我得到 null。
我看过与我有类似问题的其他帖子。虽然我发现很少有帖子可以指导我,但一个常见的问题是发帖者没有初始化类对象,但我已经完成了。
我在这里需要特定的代码,因为我尝试了很多东西,但无法让它工作。我希望我提供了足够的信息来帮助您。
用户类
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;
}
}
我想访问 User 类中的 userID 和 password getter 方法以在 Login 类中使用。
登录类
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!");
}
}
一个类创建用户帐户,其中唯一的详细信息是用户 ID 和密码。另一个类是用户可以使用该帐户的详细信息登录的地方。在Login 的login 方法中,我正在检查输入的ID 和密码是否与他们尝试访问的帐户的ID 和密码匹配。
【问题讨论】: