Skip to content

Commit 2f00f3a

Browse files
committed
There was design problems with user verification, so I changed the design to include a controller object
1 parent f19fb12 commit 2f00f3a

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package Controllers;
22

33
public interface UserRegistrationUseCase {
4-
void registerUser(String username, String password, String email);
4+
void registerUser();
55
}

src/main/java/UI/UserVerificationUI.java renamed to src/main/java/UI/UserRegistrationController.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
package UI;
22

3+
import Controllers.UserRegistrationUseCase;
34
import UseCase.UserVerifier;
45
import UseCase.verificationMethodFactory;
56

67
import javax.swing.*;
78
import java.awt.event.ActionEvent;
89
import java.awt.event.ActionListener;
910
import java.util.Random;
10-
public class UserVerificationUI implements UserVerifier, ActionListener {
11+
public class UserRegistrationController implements UserVerifier, ActionListener, UserRegistrationUseCase {
12+
private final String username;
13+
private final String password;
14+
private final String email;
15+
private UserDatabase database;
1116
Random random;
1217
private final int code;
1318
private JTextField verificationCodeText;
1419
private JLabel success;
1520

16-
public UserVerificationUI(int code){
21+
public UserRegistrationController(int code, String Username, String Password, String email, UserDatabase database){
1722
this.code = code;
23+
this.username = Username;
24+
this.password = Password;
25+
this.email = email;
26+
this.database = database;
1827
}
1928

2029
//Asks for the verification code from the user, and matches it with this.code to potentially verify the user
2130
public void verify(String email){
22-
//Creating the UI to input the verification code
2331
JFrame verificationFrame = new JFrame();
2432
verificationFrame.setSize(400, 200);
2533
verificationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@@ -50,6 +58,14 @@ public void verify(String email){
5058
mailMan.deliverCode();
5159
}
5260

61+
public void registerUser() {
62+
if(database.UserExists(this.username, this.email)){
63+
System.out.println("A user with this username or email already exists");
64+
}else{
65+
this.verify(email);
66+
}
67+
}
68+
5369
//For testing purposes
5470
/*public static void main(String[] args){
5571
UI.UserVerificationUI ver = new UI.UserVerificationUI(389);
@@ -59,11 +75,11 @@ public void verify(String email){
5975
@Override
6076
public void actionPerformed(ActionEvent e) {
6177
int verCode = Integer.parseInt(verificationCodeText.getText());
62-
if(code == verCode){
63-
//TODO: Going to change below to a label, and will link to LoginUI
64-
success.setText("verified!");
78+
if(verCode == this.code){
79+
database.createUser(this.username, this.password, this.email, "Basic");
80+
System.out.println("Verification successful");
6581
}else{
66-
success.setText("Try again");
82+
System.out.println("Could not verify please try again");
6783
}
6884
}
6985
}

src/main/java/UI/UserRegistrationUI.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package UI;
22

3-
import Controllers.UserRegistrationUseCase;
43
import javax.swing.*;
54
import java.awt.event.ActionEvent;
65
import java.awt.event.ActionListener;
76
import java.io.File;
87

9-
public class UserRegistrationUI implements UserRegistrationUseCase, ActionListener {
8+
public class UserRegistrationUI implements ActionListener {
109
private final UserDatabase database;
1110
private JLabel registrationSuccess;
1211
private JTextField usernameText;
@@ -73,17 +72,21 @@ void GetUserCredentials(){
7372

7473
registerFrame.setVisible(true);
7574
}
76-
@Override
77-
public void registerUser(String username, String password, String email) {
75+
/*@Override*/
76+
/*public void registerUser(String username, String password, String email) {
7877
if(database.UserExists(username, email)){
7978
registrationSuccess.setText("The username or password is already in use, please try again");
8079
}else{
81-
database.createUser(username, password, email, "Basic");
82-
registrationSuccess.setText("Your account has been created, please verify to login");
83-
UserVerificationUI verifyUser = new UserVerificationUI(code);
84-
verifyUser.verify(email);
80+
registrationSuccess.setText("Please verify to create your account");
81+
UserVerificationUI verifyUser = new UserVerificationUI(code, email);
82+
if(verifyUser.verify(email)){
83+
database.createUser(username, password, email, "Basic");
84+
registrationSuccess.setText("Your account is now created");
85+
}else{
86+
registrationSuccess.setText("You could not be verified, please try again");
87+
};
8588
}
86-
}
89+
}*/
8790
//For Testing purposes
8891
public static void main(String[] args){
8992
UserDatabase testDB = new UserDatabase(new File("Test5"));
@@ -99,7 +102,7 @@ public void actionPerformed(ActionEvent e) {
99102
String username = usernameText.getText();
100103
String password = passwordText.getText();
101104
String email = emailText.getText();
102-
this.registerUser(username, password, email);
103-
105+
UserRegistrationController verifyUser = new UserRegistrationController(code, username, password, email, database);
106+
verifyUser.registerUser();
104107
}
105108
}

0 commit comments

Comments
 (0)