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

switched objects #96

Open
wants to merge 5 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
75 changes: 75 additions & 0 deletions src/main/java/app/MainWithInMemory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package app;

import java.awt.CardLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import data_access.InMemoryUserDataAccessObject;
import interface_adapter.ViewManagerModel;
import interface_adapter.logged_in.LoggedInViewModel;
import interface_adapter.login.LoginViewModel;
import interface_adapter.signup.SignupViewModel;
import view.LoggedInView;
import view.LoginView;
import view.SignupView;
import view.ViewManager;

/**
* The version of Main with an in-memory database used to persist user data.
*/
public class MainWithInMemory {

/**
* The main method for starting the program with an in-memory database used to persist user data.
* @param args input to main
*/
public static void main(String[] args) {
// Build the main program window, the main panel containing the
// various cards, and stitch them together.

// The main application window.
final JFrame application = new JFrame("Login Example");
application.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

final CardLayout cardLayout = new CardLayout();

// The various View objects. Only one view is visible at a time.
final JPanel views = new JPanel(cardLayout);
application.add(views);

// This keeps track of and manages which view is currently showing.
final ViewManagerModel viewManagerModel = new ViewManagerModel();
new ViewManager(views, cardLayout, viewManagerModel);

// The data for the views, such as username and password, are in the ViewModels.
// This information will be changed by a presenter object that is reporting the
// results from the use case. The ViewModels are "observable", and will
// be "observed" by the Views.
final LoginViewModel loginViewModel = new LoginViewModel();
final LoggedInViewModel loggedInViewModel = new LoggedInViewModel();
final SignupViewModel signupViewModel = new SignupViewModel();

// Use the InMemoryUserDataAccessObject for in-memory data persistence.
final InMemoryUserDataAccessObject userDataAccessObject = new InMemoryUserDataAccessObject();

final SignupView signupView = SignupUseCaseFactory.create(viewManagerModel, loginViewModel,
signupViewModel, userDataAccessObject);
views.add(signupView, signupView.getViewName());

final LoginView loginView = LoginUseCaseFactory.create(viewManagerModel, loginViewModel,
loggedInViewModel, userDataAccessObject);
views.add(loginView, loginView.getViewName());

final LoggedInView loggedInView = ChangePasswordUseCaseFactory.create(viewManagerModel,
loggedInViewModel, userDataAccessObject);
views.add(loggedInView, loggedInView.getViewName());

viewManagerModel.setState(signupView.getViewName());
viewManagerModel.firePropertyChanged();

application.pack();
application.setVisible(true);
}
}
10 changes: 10 additions & 0 deletions src/main/java/data_access/DBUserDataAccessObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ public User get(String username) {
}
}

@Override
public void setCurrentUser(String name) {

}

@Override
public String getCurrentUser() {
return "";
}

@Override
public boolean existsByName(String username) {
final OkHttpClient client = new OkHttpClient().newBuilder()
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/data_access/FileUserDataAccessObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ public User get(String username) {
return accounts.get(username);
}

@Override
public void setCurrentUser(String name) {

}

@Override
public String getCurrentUser() {
return "";
}

@Override
public boolean existsByName(String identifier) {
return accounts.containsKey(identifier);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/data_access/InMemoryUserDataAccessObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public User get(String username) {
return users.get(username);
}

@Override
public void setCurrentUser(String name) {
this.currentUser = name;
}

@Override
public String getCurrentUser() {
return this.currentUser;
}

@Override
public void changePassword(User user) {
// Replace the old entry with the new password
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/use_case/login/LoginInteractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public void execute(LoginInputData loginInputData) {

final User user = userDataAccessObject.get(loginInputData.getUsername());

userDataAccessObject.setCurrentUser(user.getName());

final LoginOutputData loginOutputData = new LoginOutputData(user.getName(), false);
loginPresenter.prepareSuccessView(loginOutputData);
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/use_case/login/LoginUserDataAccessInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ public interface LoginUserDataAccessInterface {
*/
User get(String username);

/**
* Sets the current user by storing the username of the logged-in user.
* This method is used to keep track of which user is currently logged in.
*
* @param name the username of the user who has logged in
*/
void setCurrentUser(String name);

/**
* Retrieves the username of the currently logged-in user.
*
* @return the username of the current user, or {@code null} if no user is logged in
*/
String getCurrentUser();

}
26 changes: 26 additions & 0 deletions src/test/java/use_case/login/LoginInteractorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ public void prepareFailView(String error) {
interactor.execute(inputData);
}

@Test
public void successUserLoggedInTest() {

LoginInputData inputData = new LoginInputData("Paul", "password");
LoginUserDataAccessInterface userRepository = new InMemoryUserDataAccessObject();
UserFactory factory = new CommonUserFactory();
User user = factory.create("Paul", "password");
userRepository.save(user);

LoginOutputBoundary successPresenter = new LoginOutputBoundary() {
@Override
public void prepareSuccessView(LoginOutputData user) {
assertEquals("Paul", userRepository.getCurrentUser());
}

@Override
public void prepareFailView(String error) {
fail("Use case failure is unexpected.");
}
};

LoginInputBoundary interactor = new LoginInteractor(userRepository, successPresenter);
assertNull(userRepository.getCurrentUser());
interactor.execute(inputData);
}


@Test
public void failurePasswordMismatchTest() {
Expand Down