|
3 | 3 | import javax.swing.*;
|
4 | 4 | import java.awt.event.ActionEvent;
|
5 | 5 | import java.awt.event.ActionListener;
|
| 6 | +import java.util.Random; |
6 | 7 | import java.io.File;
|
7 | 8 |
|
8 | 9 | public class UserRegistrationUI implements UserRegistrationUseCase, ActionListener {
|
9 |
| - private UserDatabase database; |
10 |
| - |
11 |
| - //Front end related objects |
12 |
| - private JFrame RegisterFrame; |
13 |
| - private JPanel RegisterPanel; |
14 |
| - private JLabel usernameLabel; |
15 |
| - private JLabel passwordLabel; |
16 |
| - private JLabel emailLabel; |
| 10 | + private final UserDatabase database; |
| 11 | + |
17 | 12 | private JTextField usernameText;
|
18 | 13 | private JTextField passwordText;
|
19 | 14 | private JTextField emailText;
|
20 | 15 | private JButton register;
|
21 | 16 |
|
| 17 | + private final int code; |
| 18 | + |
22 | 19 | public UserRegistrationUI(UserDatabase database) {
|
23 | 20 | this.database = database;
|
| 21 | + code = new Random().nextInt(1244254); |
24 | 22 | }
|
25 | 23 | void GetUserCredentials(){
|
26 |
| - RegisterFrame = new JFrame(); |
27 |
| - RegisterFrame.setSize(300, 300); |
28 |
| - RegisterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
29 |
| - RegisterPanel = new JPanel(); |
30 |
| - RegisterFrame.add(RegisterPanel); |
| 24 | + //Front end related objects |
| 25 | + JFrame registerFrame = new JFrame(); |
| 26 | + registerFrame.setSize(300, 300); |
| 27 | + registerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 28 | + JPanel registerPanel = new JPanel(); |
| 29 | + registerFrame.add(registerPanel); |
31 | 30 |
|
32 | 31 | //The textbox for entering the Username
|
33 |
| - RegisterPanel.setLayout(null); |
34 |
| - usernameLabel = new JLabel("Username"); |
| 32 | + registerPanel.setLayout(null); |
| 33 | + JLabel usernameLabel = new JLabel("Username"); |
35 | 34 | usernameLabel.setBounds(10, 25, 100, 25);
|
36 |
| - RegisterPanel.add(usernameLabel); |
| 35 | + registerPanel.add(usernameLabel); |
37 | 36 |
|
38 | 37 | usernameText = new JTextField(20);
|
39 | 38 | usernameText.setBounds(100, 20, 165, 25);
|
40 |
| - RegisterPanel.add(usernameText); |
| 39 | + registerPanel.add(usernameText); |
41 | 40 |
|
42 | 41 | //The textbox for entering the password
|
43 |
| - passwordLabel = new JLabel("Password"); |
| 42 | + JLabel passwordLabel = new JLabel("Password"); |
44 | 43 | passwordLabel.setBounds(10, 55, 100, 25);
|
45 |
| - RegisterPanel.add(passwordLabel); |
| 44 | + registerPanel.add(passwordLabel); |
46 | 45 |
|
47 | 46 | passwordText = new JPasswordField();
|
48 | 47 | passwordText.setBounds(100, 50, 165, 25);
|
49 |
| - RegisterPanel.add(passwordText); |
| 48 | + registerPanel.add(passwordText); |
50 | 49 |
|
51 | 50 | //The textbox for entering the email
|
52 |
| - emailLabel = new JLabel("Email"); |
| 51 | + JLabel emailLabel = new JLabel("Email"); |
53 | 52 | emailLabel.setBounds(10, 85, 100, 25);
|
54 |
| - RegisterPanel.add(emailLabel); |
| 53 | + registerPanel.add(emailLabel); |
55 | 54 |
|
56 | 55 | emailText = new JTextField(20);
|
57 | 56 | emailText.setBounds(100, 80, 165, 25);
|
58 |
| - RegisterPanel.add(emailText); |
| 57 | + registerPanel.add(emailText); |
59 | 58 |
|
60 | 59 | //The Button
|
61 | 60 | register = new JButton("Register");
|
62 | 61 | register.setBounds(100, 110, 165, 25);
|
63 |
| - RegisterPanel.add(register); |
| 62 | + register.addActionListener(this); |
| 63 | + registerPanel.add(register); |
64 | 64 |
|
65 | 65 |
|
66 | 66 |
|
67 |
| - RegisterFrame.setVisible(true); |
| 67 | + registerFrame.setVisible(true); |
68 | 68 | }
|
69 | 69 | @Override
|
70 | 70 | public void registerUser(String username, String password, String email) {
|
71 | 71 | if(database.UserExists(username, email)){
|
| 72 | + //Will change below to be a label |
72 | 73 | System.out.println("The username or password is already in use, please try again");
|
73 | 74 | }else{
|
74 | 75 | database.createUser(username, password, email, "Basic");
|
| 76 | + //Will change below to be a label |
75 | 77 | System.out.println("Your account has been created, please verify to login");
|
76 |
| - UserVerificationUI verifyUser = new UserVerificationUI(389); |
| 78 | + UserVerificationUI verifyUser = new UserVerificationUI(code); |
| 79 | + verifyUser.sendVerificationCode(email); |
77 | 80 | verifyUser.verify(email);
|
78 | 81 | }
|
79 | 82 | }
|
80 | 83 |
|
81 | 84 | public static void main(String[] args){
|
82 |
| - UserDatabase testDB = new UserDatabase(); |
| 85 | + UserDatabase testDB = new UserDatabase(new File("TestUserDatabase2.csv")); |
| 86 | + System. out. println( testDB. UserExists( "RandomUser", "[email protected]")); |
83 | 87 | UserRegistrationUI testUI = new UserRegistrationUI(testDB);
|
84 | 88 | testUI.GetUserCredentials();
|
85 | 89 | }
|
|
0 commit comments