我有一项任务,我们要设置2个用户帐户。为两者设置用户名和密码。设置密码后,系统会要求用户输入两次密码并验证它们是否相同。我们被告知使用哈希代码来验证用户名和密码的字符串值,以便比较字符串而不是内存位置。我的代码可能还有其他问题,但是现在我正在尝试让密码检查工作。它告诉我密码即使它们也不匹配。我确定我做了一些蠢事,所以请善待,我还在学习!
这是我的UserAccount类:
public boolean checkPassword(String password)
{
if (password == this.password)
return true;
else return false;}
public void deactivateAccount()
{
this.active = false;
}
public String toString() {
return "The user name is " + username + " and the password is " + password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
result = prime * result
+ ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserAccount other = (UserAccount) obj;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
}
这是我的UserAccount驱动程序:
import java.util.Scanner;
public class UserAccountDriver {
public static void main(String[] args) {
UserAccount userAccount1 = new UserAccount(null, null);
UserAccount userAccount2 = new UserAccount(null, null);
String userName1 = null;
String password1 = null;
String password1check = null;
String userName2 = null;
String password2 = null;
String password2check = null;
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("Enter a user name for the first account: ");
userName1 = input.nextLine();
userAccount1.setUsername(userName1);
boolean password1Set = false;
while (password1Set == false)
{
System.out.println("Enter a password for the first account: ");
password1 = input.nextLine();
userAccount1.setPassword(password1);
System.out.println("Enter the password again: ");
password1check = input.nextLine();
if (userAccount1.checkPassword(password1check)==false)
{
System.out.println("Passwords do not match, please try again.");
password1Set = false;
}
else password1Set = true;
}
userAccount1.setPassword(password1);
System.out.println("Passwords match, password is set.");
System.out.println("Enter a user name for the second account: ");
userName2 = input.nextLine();
userAccount2.setUsername(userName2);
boolean password2Set = false;
while (password2Set == false)
{
System.out.println("Enter a password for the first account: ");
password2 = input.nextLine();
userAccount2.setPassword(password2);
System.out.println("Enter the password again: ");
password2check = input.nextLine();
if (userAccount2.checkPassword(password2check)==false)
{
System.out.println("Passwords do not match, please try again.");
password2Set = false;
}
else password2Set = true;
}
userAccount2.setPassword(password2);
System.out.println("Passwords match, password is set.");
System.out.println(userAccount1);
System.out.println(userAccount2);
if (userAccount1.equals(userAccount2)) {
System.out.println("The user accounts have the same name, the 2nd account will be deactivated.");
userAccount2.deactivateAccount();
} else {
System.out.println("The user accounts are not duplicates, both will remain active.");
}
}
}
谢谢!