|
| 1 | +package interface_adapters.user_registration_interface_adapters; |
| 2 | + |
| 3 | +import use_cases.user_login_use_case.UserCreator; |
| 4 | +import interface_adapters.user_registration_interface_adapters.UserVerifier; |
| 5 | +import use_cases.user_login_use_case.verificationMethodFactory; |
| 6 | + |
| 7 | +import javax.swing.*; |
| 8 | +import java.awt.event.ActionEvent; |
| 9 | +import java.awt.event.ActionListener; |
| 10 | +import java.util.Random; |
| 11 | +public class UserRegistrationController implements UserVerifier, ActionListener, UserRegistrator { |
| 12 | + private final String username; |
| 13 | + private final String password; |
| 14 | + private final String email; |
| 15 | + private String preference; |
| 16 | + private boolean userExists = false; |
| 17 | + private UserCreator database; |
| 18 | + Random random; |
| 19 | + private final int code; |
| 20 | + private JTextField verificationCodeText; |
| 21 | + private JLabel success; |
| 22 | + |
| 23 | + /*public UserRegistrationController(int code, String Username, String Password, String email, UserDatabase database){ |
| 24 | + this.code = code; |
| 25 | + this.username = Username; |
| 26 | + this.password = Password; |
| 27 | + this.email = email; |
| 28 | + this.database = database; |
| 29 | + }*/ |
| 30 | + |
| 31 | + public UserRegistrationController(UserRegistrationGateway properties){ |
| 32 | + this.username = properties.getUsername(); |
| 33 | + this.password = properties.getPassword(); |
| 34 | + this.email = properties.getEmail(); |
| 35 | + this.userExists = properties.isUserExists(); |
| 36 | + this.code = properties.getCode(); |
| 37 | + this.database = properties.getDatabase(); |
| 38 | + } |
| 39 | + |
| 40 | + //Asks for the verification code from the user, and matches it with this.code to potentially verify the user |
| 41 | + public void verify(String email){ |
| 42 | + JFrame verificationFrame = new JFrame(); |
| 43 | + verificationFrame.setSize(400, 200); |
| 44 | + verificationFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 45 | + JPanel verificationPanel = new JPanel(); |
| 46 | + verificationFrame.add(verificationPanel); |
| 47 | + |
| 48 | + verificationPanel.setLayout(null); |
| 49 | + JLabel verificationLabel = new JLabel("Verification Code"); |
| 50 | + verificationLabel.setBounds(10,25, 200, 25); |
| 51 | + verificationCodeText = new JTextField(); |
| 52 | + verificationCodeText.setBounds(125, 20, 165, 25); |
| 53 | + |
| 54 | + verificationPanel.add(verificationLabel); |
| 55 | + verificationPanel.add(verificationCodeText); |
| 56 | + |
| 57 | + //Success/Failure Labels |
| 58 | + success = new JLabel(""); |
| 59 | + success.setBounds(10, 50, 100, 25); |
| 60 | + verificationPanel.add(success); |
| 61 | + |
| 62 | + JButton verifyButton = new JButton("verify"); |
| 63 | + verifyButton.setBounds(125, 50, 100, 25); |
| 64 | + verificationPanel.add(verifyButton); |
| 65 | + verifyButton.addActionListener(this); |
| 66 | + verificationFrame.setVisible(true); |
| 67 | + |
| 68 | + verificationMethodFactory mailMan = new verificationMethodFactory(email, "Email", code); |
| 69 | + mailMan.deliverCode(); |
| 70 | + } |
| 71 | + |
| 72 | + public void registerUser() { |
| 73 | + if(this.userExists){ |
| 74 | + System.out.println("An account with this username or email already exists"); |
| 75 | + accountExistsMessage(); |
| 76 | + }else{ |
| 77 | + this.verify(email); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static void accountExistsMessage(){ |
| 82 | + JFrame accountExistsFrame = new JFrame(); |
| 83 | + accountExistsFrame.setSize(400, 100); |
| 84 | + accountExistsFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 85 | + JPanel accountExistsPanel = new JPanel(); |
| 86 | + accountExistsPanel.setLayout(null); |
| 87 | + accountExistsFrame.add(accountExistsPanel); |
| 88 | + JLabel errorMessage = new JLabel("An account with this username or email already exists"); |
| 89 | + errorMessage.setBounds(10,20, 350, 20); |
| 90 | + accountExistsPanel.add(errorMessage); |
| 91 | + accountExistsFrame.setVisible(true); |
| 92 | + } |
| 93 | + |
| 94 | + public static void verificationSuccessMessage(){ |
| 95 | + JFrame verificationSuccessFrame = new JFrame(); |
| 96 | + verificationSuccessFrame.setSize(400, 100); |
| 97 | + verificationSuccessFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 98 | + JPanel verificationSuccessPanel = new JPanel(); |
| 99 | + verificationSuccessPanel.setLayout(null); |
| 100 | + verificationSuccessFrame.add(verificationSuccessPanel); |
| 101 | + JLabel errorMessage = new JLabel("Could not verify please try again"); |
| 102 | + errorMessage.setBounds(10,20, 350, 20); |
| 103 | + verificationSuccessPanel.add(errorMessage); |
| 104 | + verificationSuccessFrame.setVisible(true); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void actionPerformed(ActionEvent e) { |
| 109 | + int verCode = Integer.parseInt(verificationCodeText.getText()); |
| 110 | + if(verCode == this.code){ |
| 111 | + database.createUser(this.username, this.password, this.email, "Basic"); |
| 112 | + System.out.println("Verification successful"); |
| 113 | + }else{ |
| 114 | + verificationSuccessMessage(); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments