Skip to content

Commit

Permalink
Merge pull request #49 from CSC207-2022F-UofT/Charlotte3
Browse files Browse the repository at this point in the history
[+] Modify UserJoinUseCase and connect it with our GUI
  • Loading branch information
ScottCTD authored Nov 24, 2022
2 parents fb1d10e + a5b6d0e commit f5b82b1
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 160 deletions.
16 changes: 10 additions & 6 deletions src/main/java/billgates/Main.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package billgates;

import billgates.database.MySQLDatabaseGateway;
import billgates.entities.User;
import billgates.interface_adapters.BillPanelUpdatable;
import billgates.interface_adapters.DatabaseGateway;
import billgates.interface_adapters.UserJoinUpdatable;
import billgates.use_cases.bill_update.BillUpdateController;
import billgates.use_cases.bill_update.BillUpdateOutputPort;
import billgates.use_cases.bill_update.BillUpdatePresenter;
import billgates.use_cases.bill_update.BillUpdateUseCase;
import billgates.use_cases.delete_entry.DeleteEntryController;
import billgates.use_cases.delete_entry.DeleteEntryInputPort;
import billgates.use_cases.delete_entry.DeleteEntryUseCase;
import billgates.use_cases.user_join.*;
import billgates.view.gui.MainFrame;

import javax.swing.*;
Expand All @@ -29,18 +30,14 @@ public static void main(String[] args){
} catch (UnsupportedLookAndFeelException ignored) {
}

// TODO: remove this: this is for debugging purposes
User.getInstance(0, "Scott", "12345678", 0);

// init database gateway
DatabaseGateway databaseGateway = new MySQLDatabaseGateway();

databaseGateway.setUserId(User.getInstance().getBillId());

// init the main frame
MainFrame mainFrame = new MainFrame();
mainFrame.setBillUpdateController(initBillUpdateUseCase(databaseGateway, mainFrame.getBillPanel()));
mainFrame.setDeleteEntryController(initDeleteEntryUseCase(databaseGateway));
mainFrame.setUserJoinController(initUserJoinUseCase(databaseGateway, mainFrame.getActionPanel()));

mainFrame.setVisible(true);
// init column widths
Expand All @@ -60,6 +57,13 @@ private static DeleteEntryController initDeleteEntryUseCase(DatabaseGateway data
return new DeleteEntryController(useCase);
}

private static UserJoinController initUserJoinUseCase(DatabaseGateway databaseGateway,
UserJoinUpdatable updatable) {
UserJoinOutputPort userJoinPresenter = new UserJoinPresenter(updatable);
UserJoinInputPort userJoinUseCase = new UserJoinUseCase(databaseGateway, userJoinPresenter);
return new UserJoinController(userJoinUseCase);
}

// add new functions to init all other use cases

}
7 changes: 3 additions & 4 deletions src/main/java/billgates/entities/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,16 @@ private User(int id, String name, String password, int billId) {
}

/**
* Get or create an instance of a user.
* Create the instance of User.
*
* @param id the id of this user
* @param name the name of this user
* @param password the password of this user
* @param billID the main bill id of this user
* @return a new user instance
*/
public static User getInstance(int id, String name, String password, int billID) {
if (instance == null)
instance = new User(id, name, password, billID);
public static User createInstance(int id, String name, String password, int billID) {
instance = new User(id, name, password, billID);
return instance;
}

Expand Down
61 changes: 43 additions & 18 deletions src/main/java/billgates/use_cases/user_join/UserJoinUseCase.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package billgates.use_cases.user_join;

import billgates.database.QueryUserData;
import billgates.entities.User;
import billgates.interface_adapters.DatabaseGateway;

import java.util.List;

public class UserJoinUseCase implements UserJoinInputPort {
private final DatabaseGateway gateway;
private final UserJoinOutputPort outputPort;
Expand All @@ -13,23 +17,44 @@ public UserJoinUseCase(DatabaseGateway gateway, UserJoinOutputPort outputPort) {

@Override
public void join(UserJoinRequestModel model) {
// QueryUserData userData = gateway.getUserData();
// if (!userData.getUsers().contains(model.getUsername())) {
// // TODO add the user in the database.
//
// // TODO log the user in
// outputPort.display(new UserJoinResponseModel(true, "Registered successfully"));
// }
// else {
// int userIndex = userData.getUsers().indexOf(model.getUsername());
// if (userData.getPasswords().get(userIndex).equals(model.getPassword())) {
// // TODO log the user in
// outputPort.display(new UserJoinResponseModel(true, "Logged in successfully"));
// } else {
// outputPort.display(new UserJoinResponseModel(false, "Incorrect password " +
// "or the username already exists."));
// }
// // TODOs above depends on the implementation of DatabaseGateway
// }
List<QueryUserData> usersData = this.gateway.getUserData();
boolean exist = usersData.stream().anyMatch(d -> d.getUsername().equals(model.getUsername()));
// if the user does not exist
if (!exist) {
// register
// Create a QueryUserData in the database
QueryUserData tempUser = new QueryUserData(model.getUsername(), model.getPassword());
this.gateway.insertUser(tempUser);
QueryUserData actualUser = this.gateway.getUserData(model.getUsername());
// create a new bill
this.gateway.createBillTable(actualUser.getBillID());
// set the database user id
this.gateway.setUserId(actualUser.getUserID());
// Create a local User
User.createInstance(actualUser.getUserID(), actualUser.getUsername(),
actualUser.getPassword(), actualUser.getBillID());
// Notify the user that they have successfully registered
this.outputPort.display(new UserJoinResponseModel(true, "Registered successfully"));
}
// if the user exists
else {
QueryUserData actualUser = this.gateway.getUserData(model.getUsername());
// if the password matches
if (actualUser.getPassword().equals(model.getPassword())) {
// Create a local User
User.createInstance(actualUser.getUserID(), actualUser.getUsername(),
actualUser.getPassword(), actualUser.getBillID());
// set the database user id
this.gateway.setUserId(actualUser.getUserID());
// Notify the user that they have successfully login
this.outputPort.display(new UserJoinResponseModel(true, "Logged in successfully"));
}
// incorrect password
else {
// Notify the user that they couldn't log in
this.outputPort.display(new UserJoinResponseModel(false, "Incorrect password " +
"or the username already exists."));
}
}
}
}
98 changes: 63 additions & 35 deletions src/main/java/billgates/view/gui/ActionPanel.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package billgates.view.gui;

import billgates.interface_adapters.UserJoinUpdatable;
import billgates.use_cases.user_join.UserJoinViewModel;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.Arrays;
import java.util.Objects;

/**
* Clean Architecture Layer: Frameworks & Drivers
*
* @author Charlotte, Scott
*/
public class ActionPanel extends JPanel {
public class ActionPanel extends JPanel implements UserJoinUpdatable {

public static final int DEFAULT_WIDTH = (int) (MainFrame.DEFAULT_WIDTH / 3.5);
public static final int DEFAULT_HEIGHT = MainFrame.DEFAULT_HEIGHT;
Expand Down Expand Up @@ -56,6 +58,9 @@ private void initSignInPanel() {
// Set the size of signInPanel
this.signInPanel.setMaximumSize(new Dimension(DEFAULT_SIGN_IN_PANEL_WIDTH, DEFAULT_SIGN_IN_PANEL_HEIGHT));

// Restrict the input of usernameField (user cannot input whitespace for their username)
this.usernameField.setDocument(new RegexDocument("\\S*"));

// add and layout components
// username label
this.signInPanel.add(this.usernameLabel);
Expand Down Expand Up @@ -142,46 +147,40 @@ private void initBorder() {
}

private void signIn() {
// Get the username and password from user
// Get the username and password from the user
String userName = this.usernameField.getText();
String userPassword = Arrays.toString(this.passwordField.getPassword());
System.out.println("Name: " + userName);
System.out.println("Password: " + userPassword);

// TODO: Call the controller of UserJoinUseCase

// If the user has successfully signed in,
if (this.checkUsername() & this.checkPassword()) {
// disable the signInButton, and enable the signOutButton and addEntryButton
this.signInButton.setEnabled(false);
this.signOutButton.setEnabled(true);
this.addEntryButton.setEnabled(true);

// the usernameField and passwordField shouldn't be editable
this.usernameField.setEditable(false);
this.passwordField.setEditable(false);

// enable importMenu
TopMenuBar menuBar = (TopMenuBar) this.getRootPane().getJMenuBar();
menuBar.getImportMenu().setEnabled(true);
String userPassword = String.valueOf(this.passwordField.getPassword());

// enable billTable
BillTable billTable = this.mainFrame.getBillPanel().getBillTable();
billTable.setVisible(true);
billTable.setEnabled(true);

// if the user had successfully signed in, then we update the bill
SwingUtilities.invokeLater(() -> this.mainFrame.getBillUpdateController().update(-1));
// If the username and password are legal, we should then call the controller of UserJoinUseCase
if (this.checkUsername() && this.checkPassword()) {
// Call the UserJoinController
SwingUtilities.invokeLater(() -> this.mainFrame.getUserJoinController().userJoin(userName, userPassword));
}
}

private boolean checkUsername() {
// Will be implemented further
int usernameLength = this.usernameField.getText().length();
if (usernameLength == 0) {
JOptionPane.showMessageDialog(this.mainFrame, "Username cannot be empty!");
return false;
}
else if (usernameLength > 10) {
JOptionPane.showMessageDialog(this.mainFrame, "Username exceeds the maximum length!");
return false;
}
return true;
}

private boolean checkPassword() {
// Will be implemented further
int passwordLength = String.valueOf(this.passwordField.getPassword()).length();
if (passwordLength == 0) {
JOptionPane.showMessageDialog(this.mainFrame, "Password cannot be empty!");
return false;
}
else if (passwordLength > 16) {
JOptionPane.showMessageDialog(this.mainFrame, "Password exceeds the maximum length!");
return false;
}
return true;
}

Expand All @@ -201,11 +200,11 @@ private void signOut() {
this.passwordField.setText("");

// Disable the importMenu
TopMenuBar menuBar = (TopMenuBar) this.getRootPane().getJMenuBar();
menuBar.getImportMenu().setEnabled(false);
TopMenuBar topMenuBar = (TopMenuBar) this.mainFrame.getJMenuBar();
topMenuBar.getImportMenu().setEnabled(false);

// Disable the billTable
BillTable billTable = this.mainFrame.getBillPanel().getBillTable();
BillTable billTable = (BillTable) this.mainFrame.getBillPanel().getBillTable();
billTable.setEnabled(false);
billTable.setVisible(false);
}
Expand Down Expand Up @@ -236,4 +235,33 @@ private void deleteEntry() {
public JButton getDeleteEntryButton() {
return this.deleteEntryButton;
}

@Override
public void view(UserJoinViewModel viewModel) {
// If the user join successfully,
if (viewModel.isJoined()) {
// disable the signInButton, and enable the signOutButton and addEntryButton
this.signInButton.setEnabled(false);
this.signOutButton.setEnabled(true);
this.addEntryButton.setEnabled(true);

// the usernameField and passwordField shouldn't be editable
this.usernameField.setEditable(false);
this.passwordField.setEditable(false);

// enable importMenu
TopMenuBar topMenuBar = (TopMenuBar) this.mainFrame.getJMenuBar();
topMenuBar.getImportMenu().setEnabled(true);

SwingUtilities.invokeLater(() -> this.mainFrame.getBillUpdateController().update(-2));

// enable billTable
BillTable billTable = this.mainFrame.getBillPanel().getBillTable();
billTable.setVisible(true);
billTable.setEnabled(true);
}

// Show a message dialog with whatever the text from the viewModel
JOptionPane.showMessageDialog(this.mainFrame, viewModel.getReasonRejected());
}
}
97 changes: 0 additions & 97 deletions src/main/java/billgates/view/gui/BillDemo.java

This file was deleted.

Loading

0 comments on commit f5b82b1

Please sign in to comment.