Skip to content

Commit cb07aea

Browse files
committed
Wrote some code for the UI
1 parent 0b25139 commit cb07aea

14 files changed

+231
-2
lines changed

src/main/java/BasicUser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class BasicUser extends User{
2+
public BasicUser(String Username, String Password, String Email){
3+
super(Username, Password, Email);
4+
}
5+
}

src/main/java/IRetrieveList.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import java.util.List;
2+
3+
public interface IRetrieveList {
4+
List<User> getList();
5+
}

src/main/java/User.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.io.Serializable;
2+
public abstract class User implements Serializable, Changeable{
3+
protected String username;
4+
protected String password;
5+
protected String email;
6+
boolean verified = false;
7+
boolean online = false;
8+
public User(String username, String password, String email){
9+
this.username = username;
10+
this.password = password;
11+
this.email = email;
12+
}
13+
public String getEmail(){
14+
return this.email;
15+
}
16+
public String getUsername(){
17+
return this.username;
18+
}
19+
private String getPassword(){
20+
return this.password;
21+
}
22+
23+
@Override
24+
// from Changeable
25+
public void changeFeature(String feature, String newFeature){
26+
if (feature == "Username"){
27+
this.username = newFeature;
28+
} else if (feature == "Password"){
29+
this.password = newFeature;
30+
} else if (feature == "Email"){
31+
this.email = newFeature;
32+
}
33+
}
34+
35+
}

src/main/java/UserAccounts.csv

Whitespace-only changes.

src/main/java/UserCreator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
public interface UserCreator {
3+
void createUser(String username, String password, String email, String type);
4+
}

src/main/java/UserDatabase.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.io.*;
2+
import java.util.ArrayList;
3+
import java.util.List;
4+
public class UserDatabase implements UserExists, UserRetriever, UserCreator, IRetrieveList{
5+
File accounts;
6+
public UserDatabase(){
7+
this.accounts = new File("UserAccounts.csv");
8+
}
9+
public UserDatabase(File accounts){
10+
this.accounts = accounts;
11+
}
12+
@Override
13+
public boolean UserExists(String username, String email) {
14+
User user = null;
15+
try(FileInputStream fileIn = new FileInputStream(accounts);
16+
ObjectInputStream in = new ObjectInputStream(fileIn)){
17+
do{
18+
try{
19+
user = (User)in.readObject();
20+
}catch(NullPointerException e){
21+
break;
22+
}
23+
}while(!user.getEmail().equals(email) && !user.getUsername().equals(username));
24+
}catch(IOException e){
25+
e.printStackTrace();
26+
} catch (ClassNotFoundException e) {
27+
throw new RuntimeException(e);
28+
}
29+
return user != null;
30+
}
31+
32+
// Creates a new user with a username and password, and an email address
33+
// The order is username, password, email address, verification status, status
34+
//
35+
@Override
36+
public void createUser(String username, String password, String email, String type){
37+
User newUser = UserFactory.BirthUser(username, password, email, type);
38+
try(FileOutputStream fileOut = new FileOutputStream(accounts)){
39+
try(ObjectOutputStream out = new ObjectOutputStream(fileOut)){
40+
out.writeObject(newUser);
41+
out.close();
42+
fileOut.close();
43+
}catch(Exception e){
44+
System.out.println("Error");
45+
}
46+
}catch(Exception e){
47+
System.out.println("Error");
48+
}
49+
}
50+
51+
@Override
52+
// To be edited to get user from the array format rather than the serialized format.
53+
public User getUser(String username) {
54+
User ans = null;
55+
for (int i = 0; i < (this.getList().size()); i++) {
56+
if (this.getList().get(i).getUsername().equals(username)) {
57+
ans = this.getList().get(i);
58+
}
59+
}
60+
return ans;
61+
}
62+
63+
//Returns an ArrayList with the users that is extracted from the file, so that other objects can use this list.
64+
@Override
65+
public List<User> getList() {
66+
List<User> users = new ArrayList<>();
67+
try(FileInputStream fileIn = new FileInputStream(accounts);
68+
ObjectInputStream in = new ObjectInputStream(fileIn)){
69+
User user = (User) in.readObject();
70+
while(user != null){
71+
users.add(user);
72+
user = (User)in.readObject();
73+
}
74+
}catch(Exception e){
75+
e.printStackTrace();
76+
}
77+
return users;
78+
}
79+
}

src/main/java/UserExists.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public interface UserExists {
2+
boolean UserExists(String username, String password);
3+
}

src/main/java/UserFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class UserFactory {
2+
//Following the factory design pattern, just in case in the future we decide to add various different types of Users
3+
public static User BirthUser(String Username, String Password, String Email, String type){
4+
return new BasicUser(Username, Password, Email);
5+
}
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public interface UserRegPresenter {
2+
boolean UserExists();
3+
}
Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,50 @@
11
import javax.swing.*;
22

33
public class UserRegistrationUI implements UserRegistrationUseCase{
4-
UserDatabase database = new UserDatabase();
4+
private UserDatabase database;
5+
6+
public UserRegistrationUI(UserDatabase database){
7+
this.database = database;
8+
}
59
void GetUserCredentials(){
610
JFrame RegisterFrame = new JFrame();
11+
RegisterFrame.setSize(1000, 1000);
12+
RegisterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
713
JPanel RegisterPanel = new JPanel();
14+
RegisterFrame.add(RegisterPanel);
15+
16+
//The textbox for entering the Username
17+
RegisterPanel.setLayout(null);
18+
JLabel usernameLabel = new JLabel("Username");
19+
usernameLabel.setBounds(10, 25, 100, 25);
20+
21+
JTextField usernameText = new JTextField(20);
22+
usernameText.setBounds(100, 20, 165, 25);
23+
RegisterPanel.add(usernameText);
824

25+
//The textbox for entering the password
26+
JPasswordField passwordText = new JPasswordField();
27+
passwordText.setBounds(100, 50, 165, 25);
28+
RegisterPanel.add(passwordText);
29+
30+
//The textbox for entering the email
31+
JLabel
32+
33+
RegisterFrame.setVisible(true);
934
}
1035
@Override
1136
public void registerUser(String username, String password, String email) {
12-
if(database.userExists(username, password, email)){
37+
if(database.UserExists(username, email)){
1338
System.out.println("The username or password is already in use, please try again");
1439
}else{
1540
database.createUser(username, password, email, "Basic");
1641
System.out.println("Your account has been created, please verify to login");
1742
}
1843
}
44+
45+
public static void main(String[] args){
46+
UserDatabase testDB = new UserDatabase();
47+
UserRegistrationUI testUI = new UserRegistrationUI(testDB);
48+
testUI.GetUserCredentials();
49+
}
1950
}

0 commit comments

Comments
 (0)