我正在尝试查看用户在方法addUser()中输入的密码是否与在方法deleteUser()中输入的密码匹配。这两种方法都会查看充满了FacebookUser对象的用户数组列表,但是,我无法弄清楚如何从每种方法中匹配适当的密码。
我尝试了多个代码,我什至不记得了,有时候即使正确,我也总是会返回“不正确的密码”。但是只要我在数组列表中只有一个对象,它就可以工作,直到我添加另一个用户为止。这将使两个用户最终使用相同的密码。
public class Facebook extends FacebookUser implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
Scanner input = new Scanner(System.in);
private ArrayList<FacebookUser> users;
public Facebook(String username, String password) {
super(username, password);
users = new ArrayList<>();
}
// prints users
public void listUsers() {
if( users.isEmpty())
{
System.out.println("List is empty");
}
else
{
for (FacebookUser users : this.users)
{
System.out.println("Username: " + users.getUsername());
}
}
}
// adds a user
public void addUser() {
//creating an instance of FacebookUser class
FacebookUser user1 = new FacebookUser();
System.out.println("Enter username to add: ");
String newUser = input.nextLine();
user1.setUsername(newUser);
// using the setter method in UserAccount to set the username for "fb" object
//if statement checks if any object contains the same username
if (users.contains(user1)) {
System.out.println("Error, username already exists");
}
else {
System.out.println("Please enter password: ");
String password = input.nextLine();
System.out.println("Please enter password hint: ");
String passwordHint = input.nextLine();
FacebookUser user2 = new FacebookUser();
//creating another object for the "official" new user
user2.setPassword(password);
user2.setUsername(newUser);
user2.setPasswordHint(passwordHint);
users.add(user2);
System.out.println(newUser + " has been added");
}
}
// deletes a user
public void deleteUser() {
FacebookUser user1= new FacebookUser();
System.out.println("Enter username you want to remove: ");
String removeUser = input.nextLine();
user1.setUsername(removeUser);
if (users.contains(user1)) {
System.out.println("Enter password");
String checkPassword = input.nextLine();
user1.setPassword(checkPassword);
if (user1.password.equals(checkPassword)) {
users.remove(user1);
System.out.println(removeUser + " has been removed");
} else {
System.out.println("Incorrect password");
}
} else {
System.out.println("User does not exist");
}
}
答案 0 :(得分:0)
问题是,在deleteUser()
中,您正在将用户输入的密码设置为user1
,然后用if (user1.password.equals(checkPassword))
检查密码。因此,此if
条件始终为true
。
我在下面的代码中注释了不必要的行。
(我假设您在equals()
类中正确地重写了FacebookUser
方法。因为contains()
和remove()
方法依赖于equals()
方法。)>
public void deleteUser() {
FacebookUser user1= new FacebookUser();
System.out.println("Enter username you want to remove: ");
String removeUser = input.nextLine();
user1.setUsername(removeUser);
if (users.contains(user1)) {
System.out.println("Enter password");
String checkPassword = input.nextLine();
// I commented this line
//user1.setPassword(checkPassword);
if (user1.password.equals(checkPassword)) {
users.remove(user1);
System.out.println(removeUser + " has been removed");
} else {
System.out.println("Incorrect password");
}
} else {
System.out.println("User does not exist");
}
}
答案 1 :(得分:0)
如果不覆盖包含的实现,则代码将无法工作。尝试类似的事情。
public void deleteUser() {
FacebookUser user1= new FacebookUser();
System.out.println("Enter username you want to remove: ");
String removeUser = input.nextLine();
boolean isPresent = false;
for(FacebookUser user:users){
if(user.getUsername.equals(removeUser)){
isPresent = true;
System.out.println("Enter Password");
password = sc.nextLine();
if(user.getPassword.equals(password)){
users.remove(user);
break;
}else{
System.out.println("Incorrect Password");
}
}
}
if(!isPresent){
System.out.pritln("User does not exist");
}
}
答案 2 :(得分:0)
尝试一下。 当您使用user.contain(user1)时,它将比较user 1中user 1的所有字段。即它将检查用户名和密码与user1相同的用户。但是之前未为此用户1设置密码。因此,它将永远不会在列表中找到它。
,并且在arraylist上使用.remove()时,它将index作为参数。因此,始终传递要删除的用户而不是用户的索引。
如果仍然有任何错误,请在评论中发布错误,然后我会告诉您解决方法。
public void deleteUser() {
FacebookUser user1= new FacebookUser();
System.out.println("Enter username you want to remove: ");
String removeUser = input.nextLine();
int i=0;
for(FacebookUser users:this.users)
{
boolean present = false;
if(users.getUserName().equals(removeuser))
{
present = true;
System.out.println("Enter password");
String checkPassword = input.nextLine();
users.setPassword(checkPassword);
if(users.getPassword().equals(checkPassword))
{
this.users.remove(i);
System.out.println(removeUser + " has been removed");
break;
}
else
System.out.println("Incorrect password");
}
i++;
}
if(!present)
System.out.println("User does not exist");
}