Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Registration Refactoring #34

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/main/java/data_access/UserDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public boolean UserExists(String username, String email) {
/**
* Checks if a user with given Username exists.
*/
//TODO: this method can be be simplified to "return UserExists(username, "");"
@Override
public boolean UserExists(String username) {
for(User user: this.accountList){
Expand Down Expand Up @@ -83,6 +84,7 @@ public void createUser(String username, String password, String email, String ty
*/
@Override
// To be edited to get user from the array format rather than the serialized format.
//TODO: for loop can be replaced with enhanced for loop
public User getUser(String username) {
User ans = null;
for (int i = 0; i < (this.accountList.size()); i++) {
Expand All @@ -101,15 +103,8 @@ public List<User> getList() {
ObjectInputStream in = new ObjectInputStream(fileIn)) {

users = (ArrayList<User>) in.readObject();
/*
while(true){
try{
Entities.User_Entities.User user = (Entities.User_Entities.User) in.readObject();
users.add(user);}
catch(EOFException e){
break;
}*/
return users;
//TODO: this is not a good use of exceptions, but I could not find any other way to do it.
}catch(EOFException e){
return users;
} catch (IOException | ClassNotFoundException ex) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emoji_manager/msg/Sender.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main.java.emoji_manager.msg;
package emoji_manager.msg;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/entities/user_entities/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public abstract class User implements Serializable, Changeable, Loginable, UserA
protected String email;

protected ArrayList<Chat> userChats;
boolean verified = false;
boolean online = false;
public User(String username, String password, String email, ArrayList<Chat> userChats){
this.username = username;
this.password = password;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/entities/user_entities/UserFactory.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package entities.user_entities;
import entities.chat.Chat;
import entities.user_entities.*;
import java.util.ArrayList;

public class UserFactory {
//Following the factory design pattern, just in case in the future we decide to add various different types of Users
/*
Following the factory design pattern, just in case in the future we decide to add various different types of Users,
with different privileges
*/
public static User BirthUser(String Username, String Password, String Email, String type){
return new BasicUser(Username, Password, Email, new ArrayList<>());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package interface_adapters.login_interface_adapters;

import use_cases.user_login_use_cases.UserLoginOutputBoundary;

import java.util.ArrayList;
import java.util.List;
/**
* Just a bunch of getters and setters
* **/
public class UserChatsPresenter implements UserLoginOutputBoundary {
private List<String> chats;
private boolean notExists;
private boolean notMatched;
private String username;

public UserChatsPresenter(){
this.chats = new ArrayList<>();
}

@Override
public void setUsername(String username) {
this.username = username;
}

@Override
public void setChats(List<String> chats) {
this.chats = chats;
}
public List<String> getChats(){
return this.chats;
}

@Override
public void setUserNotExists(boolean notExists) {
this.notExists = notExists;
}

public boolean isNotExists() {
return notExists;
}

@Override
public void setPasswordNotMatched(boolean notMatched) {
this.notMatched = notMatched;
}

@Override
public String getUsername() {
return this.username;
}

public boolean isNotMatched() {
return notMatched;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package interface_adapters.login_interface_adapters;

import data_access.Database;
import use_cases.user_login_use_cases.UserLoginInputBoundary;
/**
* Presenter object that gets info from the view, and passes it to the login use case objects in order
* to implement login
* **/
public class UserLoginPresenter {
private final UserLoginInputBoundary loginGuard;
private String username;
private String password;
Database database;
private UserLoginViewI loginView;

public UserLoginPresenter(Database database, UserLoginInputBoundary loginGuard){
this.database = database;
this.loginGuard = loginGuard;
}
/**
* Passes info from the view into loginInteractor(use case interactor object)
**/
public void tryLogin() {
loginGuard.login(this.username, this.password);
loginView.setChatsPresenter(loginGuard.getChatsPresenter());
loginView.display();
}
/**
* Gets the info from the view
* **/
public void setLoginCredentials(String username, String password) {
this.username = username;
this.password = password;
}
/**
* Sets the screen that will update the view after login
**/
public void setLoginView(UserLoginViewI loginView){
this.loginView = loginView;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package interface_adapters.login_interface_adapters;

import use_cases.user_login_use_cases.UserLoginOutputBoundary;
/**
* ViewInterface that will update the view after login
* **/
public interface UserLoginViewI {
void display();
void setChatsPresenter(UserLoginOutputBoundary outputBoundary);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package interface_adapters.user_registration_interface_adapters;
/**
* Presenter interface that presents information, or gets input from user
* */
public interface UserExistsOutputView {
/**
* Gets the verification code from the user
* */
void getVerificationCredentials();
/**
* Presents that the user exists in the database
* */
/*TODO: Currently this violates Interface segregation principle, as the bottom method has nothing to do with
getting verification credentials.*/
void presentUserExistsMessage();
/**
* Gets the code from the input boundary object
* @param code Verification code
* */
void getCode(int code);
/**
* Gets the user credentials from the input boundary object
* @param email Email address of the user
* @param password Password of the user
* @param username Username of the user
* */
void getUserCredentials(String username, String password, String email);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package interface_adapters.user_registration_interface_adapters;

import data_access.Database;
import use_cases.user_registration_use_cases.VerificationCodeDeliveryManager;
import use_cases.user_registration_use_cases.createMailMan;

/**
* This is the class responsible for getting processing the input given by user, and either allowing verification,
* presenting the 'user exists' message, and sending the verification code, depending on the business logic
* */
public class UserExistsPresenter {
private final VerificationCodeDeliveryManager verCodeDeliveryManager;
Database database;
UserExistsOutputView existsOutputBoundary;

public UserExistsPresenter(Database database, UserExistsOutputView existsOutputBoundary, createMailMan mailMan){
this.database = database;
this.existsOutputBoundary = existsOutputBoundary;
//The responsibility of dealing with verification is passed onto this class
this.verCodeDeliveryManager = new VerificationCodeDeliveryManager(mailMan);
}
/**
* Proceeds to verification and sends code, or presents an error message, depending on whether a user with
* such credentials is in the database.
* @param username Username
* @param email Email
* @param password Password
* */

public void register(String username, String password, String email) {
if(!database.UserExists(username, email)){
//This may need to change if verCodeDeliveryManager decides not to create integer codes.
int code = this.verCodeDeliveryManager.getVerCode();
existsOutputBoundary.getCode(code);
existsOutputBoundary.getUserCredentials(username, password, email);
existsOutputBoundary.getVerificationCredentials();
this.verCodeDeliveryManager.deliverCode(email);
}else{
existsOutputBoundary.presentUserExistsMessage();
}
}
/**
* Sets the verification stream given by the user, to send the code
* @param type The verification stream
* */
public void setCodeDeliveryMethod(String type) {
this.verCodeDeliveryManager.setMailMan(type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package interface_adapters.user_registration_interface_adapters;

public interface UserVerificationOutputView {
void getLoginCredentials();
void cannotVerify();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package interface_adapters.user_registration_interface_adapters;

import data_access.Database;

public class UserVerificationPresenter {
private final Database database;
private String username;
private String password;
private String email;

private final UserVerificationOutputView verificationOutputBoundary;

private int code;

public UserVerificationPresenter(Database database, UserVerificationOutputView verificationOutputBoundary){
this.database = database;
this.verificationOutputBoundary = verificationOutputBoundary;
}
/**
* Compares code with this.code, if they match, the program will proceed to ask for login credentials
* Else, it will present a message that verification is not possible
* @param code code inputted by the user
* */
public void verify(int code) {
System.out.println(this.code);
if(code == this.code){
database.createUser(this.username, this.password, this.email, "Basic");
verificationOutputBoundary.getLoginCredentials();
}else{
verificationOutputBoundary.cannotVerify();
}

}
/**
* Sets the code to compare for verification
* @param code verification code*/
public void setCode(int code) {
this.code = code;
}
/**
* Sets the user credentials for this object
* */
public void setCredentials(String username, String password, String email){
this.username = username;
this.password = password;
this.email = email;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package use_cases.user_registration_use_cases;
package interface_adapters.user_registration_interface_adapters;

public interface userRegCredentialsRetriever {
void getUserCredentials();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* User_search_IA.UserPresenter makes us implement showProfile to invert the dependency
*/
public class UserPresenterClass implements UserPresenter{
public class UserPresenterClass implements UserPresenter {
@Override
public String showProfile(String username) {
// setting up access to the database of users:
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/screens/login_screen/AppScreenCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package screens.login_screen;
import interface_adapters.appscreen.AppScreenLoader;
import interface_adapters.login_interface_adapters.UserLoginViewI;
import use_cases.user_login_use_cases.UserLoginOutputBoundary;

import java.util.ArrayList;
/**
* Responsible for creating the view once User presses login
* **/
public class AppScreenCreator implements UserLoginViewI {
private boolean userNotExists;
private boolean passNotMatched;
AppScreenLoader appScreenLoader;
public AppScreenCreator(){
}
/**
* Opens the chats of the user
* **/
@Override
public void display() {
if(userNotExists|| passNotMatched){
showUnableToLogin();
}else{
/*this.appScreen = new AppScreen(username, chats);*/
//Could be null pointer exception if setChatsPresenter is not called before the below
appScreenLoader.openScreen();
}
}
/**
* Shows a frame that displays a message
* **/
private void showUnableToLogin() {
System.out.println("unable to login");
}
/**
* Gets the info from the presenter in order to output info into the screen
* **/
@Override
public void setChatsPresenter(UserLoginOutputBoundary chatsPresenter){
String username = chatsPresenter.getUsername();
ArrayList<String> chats = (ArrayList<String>) chatsPresenter.getChats();
this.userNotExists = chatsPresenter.isNotExists();
this.passNotMatched = chatsPresenter.isNotMatched();
appScreenLoader = new AppScreenLoader(username, chats);
}
Madhavan7 marked this conversation as resolved.
Show resolved Hide resolved

}
Loading