Skip to content

Commit f837d19

Browse files
committed
Used Factory design pattern to change the method "sendVerificationCode" into an object that would either send the code via phone number or email
1 parent a2a33b7 commit f837d19

8 files changed

+51
-15
lines changed

src/main/java/EmailDelivery.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class EmailDelivery implements ISendVerificationCode{
2+
public void sendVerificationCode(String email, int code){
3+
/*TODO: When this is implemented, the verification code will be sent to the email specified by String email*/
4+
System.out.println("Verification code sent to " + email);
5+
}
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public interface ISendVerificationCode {
2+
void sendVerificationCode(String email, int code);
3+
}

src/main/java/PhoneDelivery.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class PhoneDelivery implements ISendVerificationCode{
2+
public void sendVerificationCode(String phoneNumber, int code){
3+
/*TODO: When this is implemented properly, the verification code will be sent to phone number specified by
4+
* String phoneNumber*/
5+
System.out.println("Verification code sent to " + phoneNumber);
6+
}
7+
}

src/main/java/UserDatabase.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ public class UserDatabase implements UserExists, UserRetriever, UserCreator, IRe
55
File accounts;
66
List<User> accountList;
77
public UserDatabase(){
8-
this.accounts = new File("UserAccounts.csv");
8+
this.accounts = new File("TestUserDatabase3.csv");
99
this.accountList = this.getList();
1010
}
1111
public UserDatabase(File accounts){
12+
if(!accounts.exists()){
13+
try {
14+
accounts.createNewFile();
15+
} catch (IOException e) {
16+
throw new RuntimeException(e);
17+
}
18+
}
1219
this.accounts = accounts;
1320
this.accountList = this.getList();
1421
}

src/main/java/UserRegistrationUI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void registerUser(String username, String password, String email) {
8686
}
8787
//For Testing purposes
8888
public static void main(String[] args){
89-
UserDatabase testDB = new UserDatabase(new File("TestUserDatabase3.csv"));
89+
UserDatabase testDB = new UserDatabase();
9090
System.out.println(testDB.UserExists("RandomUser", "[email protected]"));
9191
UserRegistrationUI testUI = new UserRegistrationUI(testDB);
9292
testUI.GetUserCredentials();

src/main/java/UserVerificationUI.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,8 @@ public void verify(String email){
4141
verifyButton.addActionListener(this);
4242
verificationFrame.setVisible(true);
4343

44-
this.sendVerificationCode(email);
45-
}
46-
//Sends this.code to the email address given by String email
47-
public void sendVerificationCode(String email){
48-
/*TODO: When this is implemented, a verification code(this.code) will be sent to email with email address "email",
49-
The code will be a random number that is generated, when the user presses the register
50-
button(see UserRegistrationUI).*/
44+
verificationMethodFactory mailMan = new verificationMethodFactory(email, "Email", code);
45+
mailMan.deliverCode();
5146
}
5247

5348
//For testing purposes
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class verificationMethodFactory {
2+
String credential;
3+
String type;
4+
int code;
5+
ISendVerificationCode mailMan;
6+
public verificationMethodFactory(String credential, String type, int code){
7+
this.credential = credential;
8+
this.type = type;
9+
if(type.equals("Phone")){
10+
mailMan = new PhoneDelivery();
11+
} else if (type.equals("Email")){
12+
mailMan = new EmailDelivery();
13+
}
14+
}
15+
public void deliverCode(){
16+
mailMan.sendVerificationCode(credential, code);
17+
}
18+
}

src/test/java/UserDatabaseTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@
77
public class UserDatabaseTest {
88
@Test
99
public void addingFilesRightEmailAndUser(){
10-
File accounts = new File("TestUserDatabase2.csv");
10+
File accounts = new File("Test");
1111
UserDatabase accountDatabase = new UserDatabase(accounts);
1212
accountDatabase.createUser("MadhavGopakumar", "123", "[email protected]", "Basic");
1313
Assertions.assertTrue(accountDatabase.UserExists("MadhavGopakumar", "[email protected]"));
1414
}
1515
@Test
1616
public void addingMultipleFiles(){
17-
File accounts = new File("TestUserDatabase2.csv");
17+
File accounts = new File("Test");
1818
UserDatabase accountDatabase = new UserDatabase(accounts);
1919
accountDatabase.createUser("MeenakshiGopakumar", "123", "[email protected]", "Basic");
2020
Assertions.assertTrue(accountDatabase.UserExists("MeenakshiGopakumar", "[email protected]"));
2121
}
2222
@Test
2323
public void rightEmailWrongUser(){
24-
File accounts = new File("TestUserDatabase2.csv");
24+
File accounts = new File("Test");
2525
UserDatabase accountDatabase = new UserDatabase(accounts);
2626
accountDatabase.createUser("MadhavGopakumar", "123", "[email protected]", "Basic");
2727
Assertions.assertTrue(accountDatabase.UserExists("MadG", "[email protected]"));
2828
}
2929
@Test
3030
public void rightUserWrongEmail(){
31-
File accounts = new File("TestUserDatabase2.csv");
31+
File accounts = new File("Test");
3232
UserDatabase accountDatabase = new UserDatabase(accounts);
3333
Assertions.assertTrue(accountDatabase.UserExists("MeenakshiGopakumar", "ma"));
3434
}
3535
@Test
3636
public void listedUsers(){
37-
File accounts = new File("TestUserDatabase2.csv");
37+
File accounts = new File("Test");
3838
UserDatabase accountDatabase = new UserDatabase(accounts);
3939
accountDatabase.createUser("MeenakshiGopakumar", "123", "[email protected]", "Basic");
4040
List<User> lst = accountDatabase.getList();
@@ -44,7 +44,7 @@ public void listedUsers(){
4444

4545
@Test
4646
public void userGot() {
47-
File accounts = new File("TestUserDatabase2.csv");
47+
File accounts = new File("Test");
4848
UserDatabase accountDatabase = new UserDatabase(accounts);
4949
accountDatabase.createUser("MeenakshiGopakumar", "123", "[email protected]", "Basic");
5050
User user = accountDatabase.getUser("MeenakshiGopakumar");

0 commit comments

Comments
 (0)