From 762d1e2b923b904d1b9692d6d1eb4325c99a3718 Mon Sep 17 00:00:00 2001 From: Tiare Mar Date: Fri, 2 Dec 2022 18:42:28 -0500 Subject: [PATCH] new current user entity to track logged-in user --- src/main/java/Main.java | 4 +- src/main/java/entities/CurrentUser.java | 42 +++++++++++++++++++ ...torMain.java => InstructorMainScreen.java} | 4 +- .../calendar_scheduler/CalendarScreen.java | 2 +- .../ScheduleCTScreen.java | 2 +- .../ScheduleCTView.java | 2 +- .../CourseCreationScreen.java | 2 +- .../login_registration/LoginScreen.java | 2 +- .../login_registration/RegisterScreen.java | 2 +- .../EventCreationScreen.java | 2 +- .../login_usecase/LoginInteractor.java | 4 ++ .../UserRegInteractor.java | 3 ++ 12 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 src/main/java/entities/CurrentUser.java rename src/main/java/screens/{InstructorMain.java => InstructorMainScreen.java} (93%) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a222b32..9a6fc6d 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -108,7 +108,7 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio screens.add("course", courseCreationScreen); StudentMainScreen studentMainScreen = new StudentMainScreen(screens, cardLayout); - screens.add("main", studentMainScreen); + screens.add("StudentMain", studentMainScreen); RegisterScreen registerScreen = new RegisterScreen(userRegisterController, cardLayout, screens); screens.add("register", registerScreen); @@ -116,7 +116,7 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio LoginScreen loginScreen = new LoginScreen(loginController, cardLayout, screens); screens.add("login", loginScreen); - InstructorMain instructorMainScreen = new InstructorMain(screens, cardLayout); + InstructorMainScreen instructorMainScreen = new InstructorMainScreen(screens, cardLayout); screens.add("InstructorMain", instructorMainScreen); WelcomeScreen welcomeScreen = new WelcomeScreen(cardLayout, screens); diff --git a/src/main/java/entities/CurrentUser.java b/src/main/java/entities/CurrentUser.java new file mode 100644 index 0000000..c5fb816 --- /dev/null +++ b/src/main/java/entities/CurrentUser.java @@ -0,0 +1,42 @@ +package entities; + +/** + * An entity to hold the current user that is logged into the program + */ + +public class CurrentUser { + + private static User currentUser; + + public static User getCurrentUser() { + return currentUser; + } + + public static void setCurrentUser(User currentUser) { + CurrentUser.currentUser = currentUser; + } + + /** + * Return true if the currently logged-in user is a StudentUser, false if null or a different user + * @return boolean representing whether the current user is a student + */ + public boolean isStudent() { + if (currentUser != null) { + return currentUser instanceof StudentUser; + } else { + return false; + } + } + + /** + * Return true if the currently logged-in user is an InstructorUser, false if null or a different user + * @return boolean representing whether the current user is an instructor + */ + public boolean isInstructor() { + if (currentUser != null) { + return currentUser instanceof InstructorUser; + } else { + return false; + } + } +} diff --git a/src/main/java/screens/InstructorMain.java b/src/main/java/screens/InstructorMainScreen.java similarity index 93% rename from src/main/java/screens/InstructorMain.java rename to src/main/java/screens/InstructorMainScreen.java index ca37401..44927f9 100644 --- a/src/main/java/screens/InstructorMain.java +++ b/src/main/java/screens/InstructorMainScreen.java @@ -5,7 +5,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -public class InstructorMain extends JPanel implements ActionListener { +public class InstructorMainScreen extends JPanel implements ActionListener { /** @@ -27,7 +27,7 @@ public class InstructorMain extends JPanel implements ActionListener { /** * The window of the main screen with buttons connecting to each use case */ - public InstructorMain(JPanel screens, CardLayout cardLayout) { + public InstructorMainScreen(JPanel screens, CardLayout cardLayout) { this.cardLayout = cardLayout; this.screens = screens; diff --git a/src/main/java/screens/calendar_scheduler/CalendarScreen.java b/src/main/java/screens/calendar_scheduler/CalendarScreen.java index 52c2e4b..8d9e9a7 100644 --- a/src/main/java/screens/calendar_scheduler/CalendarScreen.java +++ b/src/main/java/screens/calendar_scheduler/CalendarScreen.java @@ -176,7 +176,7 @@ public void actionPerformed(ActionEvent e) { // Trigger button to return to main dashboard if (e.getSource() == exitToMain) { - screenLayout.show(screens, "main"); + screenLayout.show(screens, "StudentMain"); } // Trigger button for changing user's view panel diff --git a/src/main/java/screens/collaborative_task_scheduling/ScheduleCTScreen.java b/src/main/java/screens/collaborative_task_scheduling/ScheduleCTScreen.java index cc9e94e..a077d94 100644 --- a/src/main/java/screens/collaborative_task_scheduling/ScheduleCTScreen.java +++ b/src/main/java/screens/collaborative_task_scheduling/ScheduleCTScreen.java @@ -82,7 +82,7 @@ public ScheduleCTScreen(ScheduleCTController scheduleCTController, JPanel screen */ public void actionPerformed(ActionEvent evt) { if (evt.getActionCommand().equals("Cancel")) { - screenLayout.show(screens, "main"); + screenLayout.show(screens, "StudentMain"); } else if (evt.getActionCommand().equals("Schedule")) { try { scheduleCTController.isConflict(taskTitle.getText(), startTime.getText(), endTime.getText()); diff --git a/src/main/java/screens/collaborative_task_scheduling/ScheduleCTView.java b/src/main/java/screens/collaborative_task_scheduling/ScheduleCTView.java index 34264fb..3f955a3 100644 --- a/src/main/java/screens/collaborative_task_scheduling/ScheduleCTView.java +++ b/src/main/java/screens/collaborative_task_scheduling/ScheduleCTView.java @@ -63,7 +63,7 @@ public void present(ScheduleCTFormatter scheduleCTFormatter) { public void actionPerformed(ActionEvent evt) { try { - screenLayout.show(screens, "main"); + screenLayout.show(screens, "StudentMain"); } catch (Exception e) { JOptionPane.showMessageDialog(this, e.getMessage()); } diff --git a/src/main/java/screens/courses_features/CourseCreationScreen.java b/src/main/java/screens/courses_features/CourseCreationScreen.java index 4ee988c..5829861 100644 --- a/src/main/java/screens/courses_features/CourseCreationScreen.java +++ b/src/main/java/screens/courses_features/CourseCreationScreen.java @@ -82,7 +82,7 @@ public CourseCreationScreen(CourseCreationController controller, JPanel screens, public void actionPerformed(ActionEvent evt) { // instructor decides to cancel the course creation process if (evt.getActionCommand().equals("Cancel")) { - screenLayout.show(screens, "main"); + screenLayout.show(screens, "StudentMain"); } else if (evt.getActionCommand().equals("Save")) { try { // initialize new Arraylist and add task diff --git a/src/main/java/screens/login_registration/LoginScreen.java b/src/main/java/screens/login_registration/LoginScreen.java index b5d49f8..802a2a5 100644 --- a/src/main/java/screens/login_registration/LoginScreen.java +++ b/src/main/java/screens/login_registration/LoginScreen.java @@ -77,7 +77,7 @@ public void actionPerformed(ActionEvent evt) { if (l.getTypeOfUser().equals("Instructor")) { cardLayout.show(screens, "InstructorMain"); } else { - cardLayout.show(screens, "main"); + cardLayout.show(screens, "StudentMain"); } } catch (Exception e) { showMessageDialog(this, e.getMessage()); diff --git a/src/main/java/screens/login_registration/RegisterScreen.java b/src/main/java/screens/login_registration/RegisterScreen.java index 69f9906..4bd5fcc 100644 --- a/src/main/java/screens/login_registration/RegisterScreen.java +++ b/src/main/java/screens/login_registration/RegisterScreen.java @@ -112,7 +112,7 @@ public void actionPerformed(ActionEvent evt) { String.valueOf(typeOfUser.getText())); showMessageDialog(this, "%s created.".format(username.getText())); if (String.valueOf(typeOfUser.getText()).equals("Student")) { - cardLayout.show(screens, "main"); + cardLayout.show(screens, "StudentMain"); } else if (String.valueOf(typeOfUser.getText()).equals("Instructor")) { cardLayout.show(screens, "InstructorMain"); } diff --git a/src/main/java/screens/task_management/event_creation_screens/EventCreationScreen.java b/src/main/java/screens/task_management/event_creation_screens/EventCreationScreen.java index 4036f9c..09bdecd 100644 --- a/src/main/java/screens/task_management/event_creation_screens/EventCreationScreen.java +++ b/src/main/java/screens/task_management/event_creation_screens/EventCreationScreen.java @@ -83,7 +83,7 @@ public void actionPerformed(ActionEvent evt) { showMessageDialog(this, e.getMessage()); } } else if (evt.getActionCommand().equals("Cancel")) { - screenLayout.show(screens, "main"); + screenLayout.show(screens, "StudentMain"); } } } diff --git a/src/main/java/use_cases/login_registration/login_usecase/LoginInteractor.java b/src/main/java/use_cases/login_registration/login_usecase/LoginInteractor.java index 2085d6f..f20380d 100644 --- a/src/main/java/use_cases/login_registration/login_usecase/LoginInteractor.java +++ b/src/main/java/use_cases/login_registration/login_usecase/LoginInteractor.java @@ -1,5 +1,6 @@ package use_cases.login_registration.login_usecase; +import entities.CurrentUser; import entities.InstructorUser; import entities.User; import screens.login_registration.LoginFailed; @@ -46,6 +47,9 @@ public LoginResponseModel create(LoginRequestModel requestModel) throws LoginFai this.user = createUser(requestModel); + //set the program's current user + CurrentUser.setCurrentUser(user); + LoginResponseModel loginRes; if (this.user instanceof InstructorUser) { diff --git a/src/main/java/use_cases/login_registration/user_register_usecase/UserRegInteractor.java b/src/main/java/use_cases/login_registration/user_register_usecase/UserRegInteractor.java index 3561a7b..2050604 100644 --- a/src/main/java/use_cases/login_registration/user_register_usecase/UserRegInteractor.java +++ b/src/main/java/use_cases/login_registration/user_register_usecase/UserRegInteractor.java @@ -66,6 +66,9 @@ public UserRegResponse create(UserRegRequest request) throws IOException { LocalDateTime now = LocalDateTime.now(); + //set the program's currently logged in user + CurrentUser.setCurrentUser(user); + UserRegSaveRequest userModel = getUserRegSaveRequest(now); userGateway.save(userModel);