Skip to content

Commit

Permalink
new current user entity to track logged-in user
Browse files Browse the repository at this point in the history
  • Loading branch information
TiareMar committed Dec 2, 2022
1 parent 8025c7c commit 762d1e2
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ 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);

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);
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/entities/CurrentUser.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
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 InstructorMain extends JPanel implements ActionListener {
public class InstructorMainScreen extends JPanel implements ActionListener {


/**
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/screens/login_registration/LoginScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 762d1e2

Please sign in to comment.