swing - Java UserList not populating correctly upon user registration -
i'm writing basic user registration , login swing application. currently, list has 3 users hardcoded in authenticate when login button clicked.
the issue i'm having when go register new user. have debugged , after new user added list registration view, number of elements in list increases 4 should. however, when return login page, number of elements in list 3, users hard coded in - problem.
here of code. i'll add if requested. (also, i'm not super concerned security @ point, want working).
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); }
auth method
public class loginmodel { private boolean auth; private final usermodel model; public loginmodel() { model = new usermodel(); auth = false; } public boolean tryauth(user user) { (int = 0; < 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 method called. userlist elements = 3; (this problem)
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 - adds new user list. 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());
this 1 of problems:
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"); } } }
you're creating new model meaning you're discarding changes made old one. solution not this. pass in old model
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 original updated model logincontroller controller = new logincontroller(model, view); view.setvisible(true); } else { system.out.println("password no match"); } } }
or better yet, have controller update view's state rather create new view. user not want new windows thrown @ him every time adds new user, keep original model and controller, instead have controller update view whenever model changes.
Comments
Post a Comment