Skip to content

Commit 4746d6e

Browse files
committed
Changed User class so that it follows a Simple Factory design pattern, just in case we want to add different types of Users
1 parent 05b4154 commit 4746d6e

File tree

6 files changed

+21
-10
lines changed

6 files changed

+21
-10
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/User.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import java.io.Serializable;
2-
public class User implements Serializable{
3-
private String username;
4-
private String password;
5-
private String email;
2+
public abstract class User implements Serializable{
3+
protected String username;
4+
protected String password;
5+
protected String email;
66
boolean verified = false;
77
boolean online = false;
88
public User(String username, String password, String email){
@@ -16,7 +16,7 @@ public String getEmail(){
1616
public String getUsername(){
1717
return this.username;
1818
}
19-
public String getPassword(){
19+
private String getPassword(){
2020
return this.password;
2121
}
2222

src/main/java/UserCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
public interface UserCreator {
3-
void createUser(String username, String password, String email);
3+
void createUser(String username, String password, String email, String type);
44
}

src/main/java/UserDatabase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public boolean UserExists(String username, String email) {
2929
// The order is username, password, email address, verification status, status
3030
//
3131
@Override
32-
public void createUser(String username, String password, String email){
33-
User newUser = new User(username, password, email);
32+
public void createUser(String username, String password, String email, String type){
33+
User newUser = UserFactory.BirthUser(username, password, email, type);
3434
try(FileOutputStream fileOut = new FileOutputStream(accounts)){
3535
try(ObjectOutputStream out = new ObjectOutputStream(fileOut)){
3636
out.writeObject(newUser);

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+
}

src/test/java/UserDatabaseTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ public class UserDatabaseTest {
88
public void addingFilesRightEmailAndUser(){
99
File accounts = new File("TestUserDatabase2.csv");
1010
UserDatabase accountDatabase = new UserDatabase(accounts);
11-
accountDatabase.createUser("MadhavGopakumar", "123", "[email protected]");
11+
accountDatabase.createUser("MadhavGopakumar", "123", "[email protected]", "Basic");
1212
Assertions.assertTrue(accountDatabase.UserExists("MadhavGopakumar", "[email protected]"));
1313
}
1414
@Test
1515
public void addingMultipleFiles(){
1616
File accounts = new File("TestUserDatabase2.csv");
1717
UserDatabase accountDatabase = new UserDatabase(accounts);
18-
accountDatabase.createUser("MeenakshiGopakumar", "123", "[email protected]");
18+
accountDatabase.createUser("MeenakshiGopakumar", "123", "[email protected]", "Basic");
1919
Assertions.assertTrue(accountDatabase.UserExists("MeenakshiGopakumar", "[email protected]"));
2020
}
2121
@Test

0 commit comments

Comments
 (0)