Skip to content

Commit f80cbce

Browse files
committed
Merged user registration branch onto main
1 parent b729dd4 commit f80cbce

File tree

20 files changed

+578
-3
lines changed

20 files changed

+578
-3
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
22
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
33

4-
# User-specific stuff
4+
# entities.user_entities.User-specific stuff
55
.idea/**/workspace.xml
66
.idea/**/tasks.xml
77
.idea/**/usage.statistics.xml
88
.idea/**/dictionaries
99
.idea/**/shelf
1010

11-
# AWS User-specific
11+
# AWS entities.user_entities.User-specific
1212
.idea/**/aws.xml
1313

1414
# Generated files

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ repositories {
77
}
88

99
dependencies {
10+
implementation 'javax.mail:javax.mail-api:1.6.2'
11+
implementation 'com.sun.mail:javax.mail:1.6.2'
1012
implementation 'junit:junit:4.13.1'
1113
testImplementation('org.junit.jupiter:junit-jupiter:5.6.0')
1214
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package data_access;
2+
3+
import interface_adapters.IRetrieveList;
4+
import interface_adapters.user_registration_interface_adapters.UserExists;
5+
import entities.user_entities.User;
6+
import entities.user_entities.UserFactory;
7+
import use_cases.user_login_use_case.UserCreator;
8+
import interface_adapters.UserRetriever;
9+
10+
import java.io.*;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
public class UserDatabase implements UserExists, UserRetriever, UserCreator, IRetrieveList {
14+
File accounts;
15+
List<User> accountList;
16+
public UserDatabase(){
17+
this.accounts = new File("TestUserDatabase3.csv");
18+
this.accountList = this.getList();
19+
}
20+
public UserDatabase(File accounts){
21+
if(!accounts.exists()){
22+
try {
23+
accounts.createNewFile();
24+
} catch (IOException e) {
25+
throw new RuntimeException(e);
26+
}
27+
}
28+
this.accounts = accounts;
29+
this.accountList = this.getList();
30+
}
31+
@Override
32+
public boolean UserExists(String username, String email) {
33+
for(User user: this.accountList){
34+
if(user.getUsername().equals(username) || user.getEmail().equals(email)){
35+
return true;
36+
}
37+
}
38+
return false;
39+
}
40+
41+
// Creates a new user with a username and password, and an email address
42+
// The order is username, password, email address, verification status, status
43+
//
44+
@Override
45+
public void createUser(String username, String password, String email, String type){
46+
User newUser = UserFactory.BirthUser(username, password, email, type);
47+
this.accountList.add(newUser);
48+
try(FileOutputStream fileOut = new FileOutputStream(accounts)){
49+
try(ObjectOutputStream out = new ObjectOutputStream(fileOut)){
50+
out.writeObject(this.accountList);
51+
out.close();
52+
fileOut.close();
53+
}catch(Exception e){
54+
System.out.println("Error");
55+
}
56+
}catch(Exception e){
57+
System.out.println("Error");
58+
}
59+
}
60+
61+
@Override
62+
// To be edited to get user from the array format rather than the serialized format.
63+
public User getUser(String username) {
64+
User ans = null;
65+
for (int i = 0; i < (this.getList().size()); i++) {
66+
if (this.getList().get(i).getUsername().equals(username)) {
67+
ans = this.getList().get(i);
68+
}
69+
}
70+
return ans;
71+
}
72+
73+
//Returns an ArrayList with the users that is extracted from the file, so that other objects can use this list.
74+
@Override
75+
public List<User> getList() {
76+
List<User> users = new ArrayList<>();
77+
try(FileInputStream fileIn = new FileInputStream(accounts);
78+
ObjectInputStream in = new ObjectInputStream(fileIn)) {
79+
80+
users = (ArrayList<User>) in.readObject();
81+
return users;
82+
}catch(EOFException e){
83+
return users;
84+
} catch (IOException | ClassNotFoundException ex) {
85+
throw new RuntimeException(ex);
86+
}
87+
}
88+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import entities.user_entities.User;
2+
public class BasicUser extends User{
3+
public BasicUser(String Username, String Password, String Email){
4+
super(Username, Password, Email);
5+
}
6+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import use_cases.user_attribute_modification_use_case.Changeable;
2+
3+
import java.io.Serializable;
4+
public abstract class User implements Serializable, Changeable {
5+
protected String username;
6+
protected String password;
7+
protected String email;
8+
boolean verified = false;
9+
boolean online = false;
10+
public User(String username, String password, String email){
11+
this.username = username;
12+
this.password = password;
13+
this.email = email;
14+
}
15+
public String getEmail(){
16+
return this.email;
17+
}
18+
public String getUsername(){
19+
return this.username;
20+
}
21+
private String getPassword(){
22+
return this.password;
23+
}
24+
25+
@Override
26+
// from Changeable
27+
public void changeFeature(String feature, String newFeature){
28+
if (feature == "Username"){
29+
this.username = newFeature;
30+
} else if (feature == "Password"){
31+
this.password = newFeature;
32+
} else if (feature == "Email"){
33+
this.email = newFeature;
34+
}
35+
}
36+
37+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package entities.user_entities;
2+
3+
public class UserFactory {
4+
//Following the factory design pattern, just in case in the future we decide to add various different types of Users
5+
public static User BirthUser(String Username, String Password, String Email, String type){
6+
return new BasicUser(Username, Password, Email);
7+
}
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package interface_adapters;
2+
import entities.user_entities.User;
3+
import java.util.List;
4+
5+
public interface IRetrieveList {
6+
List<User> getList();
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package interface_adapters;
2+
3+
import entities.user_entities.User;
4+
import interface_adapters.user_registration_interface_adapters.UserExists;
5+
6+
public interface UserRetriever extends UserExists {
7+
User getUser(String username);
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package interface_adapters.user_registration_interface_adapters;
2+
3+
public interface UserExists {
4+
boolean UserExists(String username, String password);
5+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)