Skip to content

Commit 922eeda

Browse files
committed
Refactored UserExistsInteractor so that it satisfies single responsibility principle
1 parent 5ec3195 commit 922eeda

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/main/java/screens/user_registration_screen/UserRegistrationUI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void getUserCredentials(){
8181
}
8282

8383
public static void main(String[] args){
84-
Database testDB = new UserDatabase(new File("newAccounts"));
84+
Database testDB = new UserDatabase(new File("newAccounts2"));
8585
UserLoginInputBoundary userLoginInteractor = new UserLoginInteractor(testDB);
8686
UserVerificationOutputBoundary loginUI = new UserLoginUI(userLoginInteractor);
8787
UserVerificationInputBoundary verificationInteractor = new UserVerificationInteractor(testDB, loginUI);

src/main/java/use_cases/user_registration_use_cases/UserExistsInteractor.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,32 @@
55
import java.util.Random;
66

77
public class UserExistsInteractor implements UserExistsInputBoundary{
8-
private final createMailMan mailManFactory;
9-
//May need to refactor this using facade design pattern since this class has too many responsibilities.
8+
private final VerificationCodeDeliveryManager verCodeDeliveryManager;
109
Database database;
1110
UserExistsOutputBoundary existsOutputBoundary;
1211

13-
private ISendVerificationCode codeMailMan;
14-
1512
public UserExistsInteractor(Database database, UserExistsOutputBoundary existsOutputBoundary, createMailMan mailMan){
1613
this.database = database;
1714
this.existsOutputBoundary = existsOutputBoundary;
18-
this.mailManFactory = mailMan;
15+
//The responsibility of dealing with verification is passed onto this class
16+
this.verCodeDeliveryManager = new VerificationCodeDeliveryManager(mailMan);
1917
}
2018
@Override
2119
public void register(String username, String password, String email) {
2220
if(!database.UserExists(username, email)){
23-
int code = new Random().nextInt(12312341);
21+
int code = this.verCodeDeliveryManager.getVerCode();
2422
existsOutputBoundary.getCode(code);
2523
existsOutputBoundary.getUserCredentials(username, password, email);
2624
existsOutputBoundary.getVerificationCredentials();
27-
codeMailMan.sendVerificationCode(email, code);
25+
this.verCodeDeliveryManager.deliverCode(email);
2826
}else{
2927
existsOutputBoundary.presentUserExistsMessage();
3028
}
3129
}
3230

3331
@Override
3432
public void setCodeDeliveryMethod(String type) {
35-
this.codeMailMan = mailManFactory.getVerificationMethod(type);
33+
this.verCodeDeliveryManager.setMailMan(type);
3634
}
3735

3836
@Override
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package use_cases.user_registration_use_cases;
2+
3+
import java.util.Random;
4+
5+
public class VerificationCodeDeliveryManager {
6+
private final int code = new Random().nextInt(134134);
7+
private final createMailMan mailManFactory;
8+
private ISendVerificationCode mailMan;
9+
public VerificationCodeDeliveryManager(createMailMan mailManFactory){
10+
this.mailManFactory = mailManFactory;
11+
//By default, the type will be email.
12+
this.setMailMan("Email");
13+
}
14+
public void setMailMan(String type){
15+
this.mailMan = mailManFactory.getVerificationMethod(type);
16+
}
17+
public void deliverCode(String email){
18+
this.mailMan.sendVerificationCode(email, this.code);
19+
}
20+
public int getVerCode(){
21+
return this.code;
22+
}
23+
}

0 commit comments

Comments
 (0)