Skip to content

Commit

Permalink
Merge pull request #67 from CSC207-2022F-UofT/5-feature-1-user-regist…
Browse files Browse the repository at this point in the history
…ration-&-log-in

User registration & log in
  • Loading branch information
CC-3636 authored Dec 2, 2022
2 parents b11b56d + f45ab57 commit 8025c7c
Show file tree
Hide file tree
Showing 34 changed files with 796 additions and 146 deletions.
40 changes: 32 additions & 8 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
import use_cases.collaborative_task_scheduling.scheduling_ct_use_case.*;
import use_cases.calendar_scheduler.schedule_conflict_use_case.ScheduleConflictPresenter;
import use_cases.calendar_scheduler.scheduler_use_case.SchedulerPresenter;
import use_cases.login_registration.login_usecase.LoginGateway;
import use_cases.login_registration.login_usecase.LoginInputBoundary;
import use_cases.login_registration.login_usecase.LoginInteractor;
import use_cases.login_registration.login_usecase.LoginPresenter;
import use_cases.login_registration.user_register_usecase.*;
import use_cases.task_management.event_creation_use_case.*;

Expand All @@ -21,7 +25,7 @@

public class Main {

public static void main(String[] args) {
public static void main(String[] args) throws IOException, ClassNotFoundException {

// Build the main program window
JFrame application = new JFrame("32 Things To Do");
Expand All @@ -35,12 +39,29 @@ public static void main(String[] args) {
HashMap<String, Course> allCourses = new HashMap<>();

// Create the components for injection into the use cases
UserRegGateway regUser = new InMemoryUser();
UserRegGateway regUser = new FileUser("src/main/java/data/users.ser");
UserFactory fac = new GeneralUserFactory();
UserRegPresenter userPresenter = new UserRegResponseFormatter();
UserRegInputBoundary userInteractor = new UserRegInteractor(regUser, userPresenter);
UserRegInputBoundary userInteractor = new UserRegInteractor(regUser, userPresenter, fac);
UserRegController userRegisterController = new UserRegController(userInteractor);

User user = ((UserRegInteractor) userInteractor).getUser();
// Adding in login use case
LoginGateway loginUser = new FileUser("src/main/java/data/users.ser");
LoginPresenter loginPresenter = new LoginResponseFormatter();
LoginInputBoundary loginInteractor = new LoginInteractor(loginUser, loginPresenter);
LoginController loginController = new LoginController(loginInteractor);
//

// initialize User based on whether they log in or register
// if you don't register, then you are logging in:
User user;
if ((((UserRegInteractor) userInteractor).getUser() instanceof StudentUser) |
(((UserRegInteractor) userInteractor).getUser() instanceof InstructorUser)) {
user = ((UserRegInteractor) userInteractor).getUser();
} else {
user = ((LoginInteractor) loginInteractor).getUser();
}


SchedulerPresenter schedulerPresenter = new SchedulerResponseFormatter();
ScheduleConflictPresenter scheduleConflictPresenter = new ScheduleConflictResponseFormatter();
Expand Down Expand Up @@ -86,14 +107,17 @@ public static void main(String[] args) {
CourseCreationScreen courseCreationScreen = new CourseCreationScreen(courseCreationController, screens, cardLayout);
screens.add("course", courseCreationScreen);

MainScreen mainScreen = new MainScreen(screens, cardLayout);
screens.add("main", mainScreen);
StudentMainScreen studentMainScreen = new StudentMainScreen(screens, cardLayout);
screens.add("main", studentMainScreen);

RegisterScreen registerScreen = new RegisterScreen(userRegisterController, cardLayout, screens);
screens.add("register", registerScreen);

// LoginScreen loginScreen = new LoginScreen(loginController);
// screens.add("login", loginScreen);
LoginScreen loginScreen = new LoginScreen(loginController, cardLayout, screens);
screens.add("login", loginScreen);

InstructorMain instructorMainScreen = new InstructorMain(screens, cardLayout);
screens.add("InstructorMain", instructorMainScreen);

WelcomeScreen welcomeScreen = new WelcomeScreen(cardLayout, screens);
screens.add("welcome", welcomeScreen);
Expand Down
Binary file added src/main/java/data/users.ser
Binary file not shown.
23 changes: 23 additions & 0 deletions src/main/java/entities/GeneralUserFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package entities;

// Entity Layer

public class GeneralUserFactory implements UserFactory {

/**
* @param name the name of this StudentUser
* @param password the password of this StudentUser
* @return a new StudentUser
*/

@Override
public StudentUser createStudent(String name, String password) {
return new StudentUser(name, password);
}

@Override
public InstructorUser createInstructor(String name, String password) {
return new InstructorUser(name, password);
}

}
18 changes: 0 additions & 18 deletions src/main/java/entities/InstructorUserFactory.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/entities/StudentUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public ArrayList<String> getCourses() {
}

public void addCourse(String course) {
this.toDoList.add(course);
this.courses.add(course);
}

public void setCourses(ArrayList<String> c) { this.courses = c; }
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/entities/StudentUserFactory.java

This file was deleted.

14 changes: 9 additions & 5 deletions src/main/java/entities/UserFactory.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package entities;


// Entity Layer

public interface UserFactory {
/**
* Creates Users with the specified name and password
*
* @param name the name of this user
* @param password the password of this user
* @return a User object
*/
User create(String name, String password);

StudentUser createStudent(String name, String password);

InstructorUser createInstructor(String name, String password);

}
88 changes: 88 additions & 0 deletions src/main/java/screens/InstructorMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package screens;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class InstructorMain extends JPanel implements ActionListener {


/**
* The selectable buttons on the main screen
*/
JButton taskCreate;
JButton calendar;
JButton courses;
JButton scheduleCT;

JButton logout;

/**
* Objects for connecting to the other screens
*/
CardLayout cardLayout;
JPanel screens;

/**
* The window of the main screen with buttons connecting to each use case
*/
public InstructorMain(JPanel screens, CardLayout cardLayout) {

this.cardLayout = cardLayout;
this.screens = screens;

// Create label for title of screen
JLabel title = new JLabel("32 Things To Do");
title.setAlignmentX(Component.CENTER_ALIGNMENT);

// Create buttons
taskCreate = new JButton("New Task");
calendar = new JButton("Calendar");
courses = new JButton("Courses");
// scheduleCT = new JButton("Schedule Collaborative Task");
logout = new JButton("Logout");

taskCreate.addActionListener(this);
calendar.addActionListener(this);
courses.addActionListener(this);
// scheduleCT.addActionListener(this);
logout.addActionListener(this);

// Create panel for buttons
JPanel buttons = new JPanel();
buttons.add(taskCreate);
buttons.add(calendar);
buttons.add(courses);
// buttons.add(scheduleCT);
buttons.add(logout);

// Add all components to the panel
this.add(title);
this.add(buttons);
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
}

/**
* Trigger the corresponding use case upon button click
*/
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == taskCreate) {
cardLayout.show(screens, "toDoList");
}
if (evt.getSource() == calendar) {
cardLayout.show(screens, "calendar");
}

if (evt.getSource() == courses) {
cardLayout.show(screens, "course");
}
// if (evt.getSource() == scheduleCT) {
// cardLayout.show(screens, "scheduleCT");
// }
if (evt.getSource() == logout) {
cardLayout.show(screens, "welcome");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainScreen extends JPanel implements ActionListener {
public class StudentMainScreen extends JPanel implements ActionListener {

/**
* The selectable buttons on the main screen
Expand All @@ -16,6 +16,8 @@ public class MainScreen extends JPanel implements ActionListener {
JButton courses;
JButton scheduleCT;

JButton logout;

/**
* Objects for connecting to the other screens
*/
Expand All @@ -25,7 +27,7 @@ public class MainScreen extends JPanel implements ActionListener {
/**
* The window of the main screen with buttons connecting to each use case
*/
public MainScreen(JPanel screens, CardLayout cardLayout) {
public StudentMainScreen(JPanel screens, CardLayout cardLayout) {

this.cardLayout = cardLayout;
this.screens = screens;
Expand All @@ -40,12 +42,14 @@ public MainScreen(JPanel screens, CardLayout cardLayout) {
progressTracker = new JButton("Progress Tracker");
courses = new JButton("Courses");
scheduleCT = new JButton("Schedule Collaborative Task");
logout = new JButton("Logout");

toDoList.addActionListener(this);
calendar.addActionListener(this);
progressTracker.addActionListener(this);
courses.addActionListener(this);
scheduleCT.addActionListener(this);
logout.addActionListener(this);

// Create panel for buttons
JPanel buttons = new JPanel();
Expand All @@ -54,6 +58,7 @@ public MainScreen(JPanel screens, CardLayout cardLayout) {
buttons.add(progressTracker);
buttons.add(courses);
buttons.add(scheduleCT);
buttons.add(logout);

// Add all components to the panel
this.add(title);
Expand All @@ -80,6 +85,9 @@ public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == scheduleCT) {
cardLayout.show(screens, "scheduleCT");
}
if (evt.getSource() == logout) {
cardLayout.show(screens, "welcome");
}

}

Expand Down
44 changes: 29 additions & 15 deletions src/main/java/screens/login_registration/FileUser.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package screens.login_registration;

import use_cases.login_registration.login_usecase.LoginGateway;
import use_cases.login_registration.logout_usecase.LogoutGateway;
import use_cases.login_registration.user_register_usecase.UserRegGateway;
import use_cases.login_registration.user_register_usecase.UserRegSaveRequest;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

public class FileUser implements UserRegGateway, LoginGateway, LogoutGateway {

private final HashMap<String, UserRegSaveRequest> accounts;
// private final HashMap<String, UserRegSaveRequest> accounts;
private static HashMap<String, UserRegSaveRequest> accounts;

private final String filePath;

Expand All @@ -22,7 +24,21 @@ public FileUser(String path) throws IOException, ClassNotFoundException {
*/

this.filePath = path;
this.accounts = readFile();

// method that checks if a path or file exists or not and then
// writes an empty map to a new file if it doesn't exist
// and reads the existing file if it does exist

if (Files.exists(Path.of(path))) {
// this.accounts = readFile();
accounts = readFile();
} else {
// this.accounts = new HashMap<String, UserRegSaveRequest>();
accounts = new HashMap<String, UserRegSaveRequest>();
save();
}


}

private HashMap<String, UserRegSaveRequest> readFile() throws IOException, ClassNotFoundException {
Expand All @@ -46,10 +62,16 @@ public void save(UserRegSaveRequest requestModel) throws IOException {

private void save() throws IOException {
/*
* Write the new map of usernames to UserRegSaveRequest objects into the User database file.
* Write the map of usernames to UserRegSaveRequest objects into the User database file.
*/

FileOutputStream fileWriter = new FileOutputStream(filePath);
FileOutputStream fileWriter;
// if (Files.exists(Path.of(filePath))) {
// fileWriter = new FileOutputStream(filePath);
// } else {
// fileWriter = new FileOutputStream("src/main/java/data/users.ser");
// }
fileWriter = new FileOutputStream(filePath);
ObjectOutputStream out = new ObjectOutputStream(fileWriter);
out.writeObject(accounts);
out.close();
Expand All @@ -72,16 +94,8 @@ public String passOf(String name) {
}

public Map<String, UserRegSaveRequest> getAccounts() {
return this.accounts;
// return this.accounts;
return accounts;
}

// /**
// * @param name The username of the user
// * @return the UserRegSaveRequest object associated with this username.
// * Preconditions: name is a key in accounts
// */
// public UserRegSaveRequest getUserSaveReq(String name) throws KeyException, IOException {
//// accounts.get(name);
// return accounts.get(name);
// }
}
Loading

0 comments on commit 8025c7c

Please sign in to comment.