我正在为我的挥杆应用程序编写基本用户注册和登录。目前,当单击登录按钮时,列表中有3个硬编码的用户成功进行了身份验证。
我遇到的问题是当我注册新用户时。我已经调试过,在从注册视图中将新用户添加到List之后,List中的元素数量增加到4。但是,当我返回登录页面时,List中的元素数量将返回到3,这是我硬编码的用户 - 这就是问题所在。
以下是我的一些代码。如果请求,我会添加任何内容。 (另外,我现在并不是非常关心安全问题,我只想让这个工作起来)。
public class User {
private final String username;
private final String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getCheckable() {
return username.toLowerCase() + "," + password.toLowerCase();
}
@Override
public String toString() {
return this.username + this.password;
}
public boolean equals(User user) {
return this.username.equals(user.username) &&
this.password.equals(user.password);
}
验证方法
public class LoginModel {
private boolean auth;
private final UserModel model;
public LoginModel() {
model = new UserModel();
auth = false;
}
public boolean tryAuth(User user) {
for (int i = 0; i < model.getUserList().size(); i++) {
if (model.getUserList().get(i).equals(user)) {
auth = true;
}
}
return auth;
}
的usermodel
public class UserModel {
private final List<User> userList = new ArrayList<>();
public UserModel() {
userList.add(new User("joe", "password"));
userList.add(new User("nick", "password"));
userList.add(new User("garrett", "password"));
}
public void addToList(User user) {
userList.add(user);
}
public List<User> getUserList() {
return this.userList;
}
LoginController - 调用auth方法。 UserList elements = 3; (这是问题)
public class LoginController {
private final LoginModel model;
private final LoginView view;
private User user;
public LoginController(LoginModel model, LoginView view) {
this.model = model;
this.view = view;
class LoginButtonPress implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
user = new User(view.getUserNameText(), view.getPasswordText());
if (model.tryAuth(user)) {
view.dispose();
MainMenuModel menuModel = new MainMenuModel();
MainMenuView menuView = new MainMenuView();
MainMenuController menuController = new MainMenuController(menuModel, menuView);
} else {
System.out.println("incorrect");
}
}
}
view.addActionListenerLogin(new LoginButtonPress());
UserController - 将新用户添加到列表中。 UserList elements = 4;
public class UserController {
private final UserModel model;
private final UserView view;
private final LoginModel loginModel = new LoginModel();
public UserController(UserModel model, UserView view) {
this.model = model;
this.view = view;
class CreateAccountButtonPress implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (view.getPasswordTextField().equals(view.getConfirmPasswordTextField())) {
model.addToList(new User(view.getUsernameTextField(), view.getPasswordTextField()));
view.dispose();
LoginView view = new LoginView();
LoginModel model = new LoginModel();
LoginController controller = new LoginController(model, view);
view.setVisible(true);
} else {
System.out.println("password no match");
}
}
}
view.addActionListenerCreateAccountButton(new CreateAccountButtonPress());
答案 0 :(得分:3)
可疑:
public boolean equals(User user)
从不那样做。转到
@Override
public boolean equals(Object other)
代替;并实现正确等于比较。您重载等于(),而非覆盖。而这只能实现一件事:造成微妙的,奇怪的错误。
如果在迭代列表时导致对象被忽略,我不会感到惊讶。
答案 1 :(得分:3)
这是你的一个问题:
class CreateAccountButtonPress implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (view.getPasswordTextField().equals(view.getConfirmPasswordTextField())) {
model.addToList(new User(view.getUsernameTextField(), view.getPasswordTextField()));
view.dispose();
LoginView view = new LoginView();
LoginModel model = new LoginModel(); // ****** here*****
LoginController controller = new LoginController(model, view);
view.setVisible(true);
} else {
System.out.println("password no match");
}
}
}
您正在创建新模型,这意味着您将丢弃对旧模式所做的所有更改。解决方案不是这样做的。你可以传入旧模型
class CreateAccountButtonPress implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (view.getPasswordTextField().equals(view.getConfirmPasswordTextField())) {
model.addToList(new User(view.getUsernameTextField(), view.getPasswordTextField()));
view.dispose();
LoginView view = new LoginView();
// LoginModel model = new LoginModel();
// use the original updated model
LoginController controller = new LoginController(model, view);
view.setVisible(true);
} else {
System.out.println("password no match");
}
}
}
或者更好的是,只需让控制器更新视图的状态而不是创建新视图。您的用户不希望每次添加新用户时都向他投掷新窗口,因此请保留原始模型和控制器,而是让控制器在模型更改时更新视图。
答案 2 :(得分:0)
我认为你正在某处创建新的UserModel对象。您可以像这样更改UserModel类。
public class UserModel {
private final static List<User> userList = new ArrayList<>();
static
{
userList.add(new User("joe", "password"));
userList.add(new User("nick", "password"));
userList.add(new User("garrett", "password"));
}
public void addToList(User user) {
userList.add(user);
}
public List<User> getUserList() {
return this.userList;
}
}