diff --git a/build.gradle b/build.gradle index 3072c93..6a4f51c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,4 @@ plugins { - id 'groovy' id 'java' } @@ -8,10 +7,8 @@ repositories { } dependencies { - implementation 'junit:junit:4.13.2' - //testImplementation('org.junit.jupiter:junit-jupiter:5.9.0') - testImplementation(enforcedPlatform('org.junit:junit-bom:5.9.0')) // JUnit 5 BOM - testImplementation("org.junit.jupiter:junit-jupiter") + implementation 'junit:junit:4.13.1' + testImplementation('org.junit.jupiter:junit-jupiter:5.6.0') } test { @@ -20,5 +17,3 @@ test { events "passed", "skipped", "failed" } } - -ext['junit-jupiter.version'] = '5.9.0' \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index eb794c1..2c01519 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -16,6 +16,10 @@ 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.logout_usecase.LogoutGateway; +import use_cases.login_registration.logout_usecase.LogoutInputBoundary; +import use_cases.login_registration.logout_usecase.LogoutInteractor; +import use_cases.login_registration.logout_usecase.LogoutPresenter; import use_cases.login_registration.user_register_usecase.*; import screens.task_management.FileTaskMap; @@ -93,6 +97,12 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio CourseEnrolmentInputBoundary enrolmentInteractor = new CourseEnrolmentInteractor(enrolCourse, tasksToTodolist, enrolmentPresenter); CourseEnrolmentController enrolmentController = new CourseEnrolmentController(enrolmentInteractor); + // Adding in logout use case + LogoutGateway logoutUser = new FileUser("src/main/java/data/users.ser"); + LogoutInputBoundary logoutInteractor = new LogoutInteractor(logoutUser); + LogoutController logoutController = new LogoutController(logoutInteractor); + // + // Build the GUI StudentChooseTaskCreateScreen chooseStudentTask = new StudentChooseTaskCreateScreen(schedulerPresenter, scheduleConflictPresenter, screens, cardLayout); @@ -116,7 +126,7 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio CourseEnrolmentScreen courseEnrolmentScreen = new CourseEnrolmentScreen(enrolmentController, screens, cardLayout); screens.add("courseEnrol", courseEnrolmentScreen); - StudentMainScreen studentMainScreen = new StudentMainScreen((StudentUser)user, screens, cardLayout); + StudentMainScreen studentMainScreen = new StudentMainScreen(screens, cardLayout, logoutController); screens.add("StudentMain", studentMainScreen); RegisterScreen registerScreen = new RegisterScreen(userRegisterController, cardLayout, screens); @@ -125,7 +135,7 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio LoginScreen loginScreen = new LoginScreen(loginController, cardLayout, screens); screens.add("login", loginScreen); - InstructorMainScreen instructorMainScreen = new InstructorMainScreen(screens, cardLayout); + InstructorMainScreen instructorMainScreen = new InstructorMainScreen(screens, cardLayout, logoutController); screens.add("InstructorMain", instructorMainScreen); WelcomeScreen welcomeScreen = new WelcomeScreen(cardLayout, screens); diff --git a/src/main/java/data/courses.csv b/src/main/java/data/courses.csv new file mode 100644 index 0000000..122444c --- /dev/null +++ b/src/main/java/data/courses.csv @@ -0,0 +1 @@ +course_name,course_instructor,tasks? diff --git a/src/main/java/screens/InstructorMainScreen.java b/src/main/java/screens/InstructorMainScreen.java index 604f41b..a553aa3 100644 --- a/src/main/java/screens/InstructorMainScreen.java +++ b/src/main/java/screens/InstructorMainScreen.java @@ -1,10 +1,18 @@ package screens; +import screens.login_registration.LoginFailed; +import screens.login_registration.LogoutController; +import screens.login_registration.LogoutFailed; +import use_cases.login_registration.login_usecase.LoginResponseModel; +import use_cases.login_registration.logout_usecase.LogoutResponseModel; + import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import static javax.swing.JOptionPane.showMessageDialog; + public class InstructorMainScreen extends JPanel implements ActionListener { @@ -24,13 +32,16 @@ public class InstructorMainScreen extends JPanel implements ActionListener { CardLayout cardLayout; JPanel screens; + LogoutController logoutController; + /** * The window of the main screen with buttons connecting to each use case */ - public InstructorMainScreen(JPanel screens, CardLayout cardLayout) { + public InstructorMainScreen(JPanel screens, CardLayout cardLayout, LogoutController controller) { this.cardLayout = cardLayout; this.screens = screens; + this.logoutController = controller; // Create label for title of screen JLabel title = new JLabel("32 Things To Do"); @@ -40,13 +51,11 @@ public InstructorMainScreen(JPanel screens, CardLayout cardLayout) { 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 @@ -54,7 +63,6 @@ public InstructorMainScreen(JPanel screens, CardLayout cardLayout) { buttons.add(taskCreate); buttons.add(calendar); buttons.add(courses); -// buttons.add(scheduleCT); buttons.add(logout); // Add all components to the panel @@ -77,11 +85,16 @@ public void actionPerformed(ActionEvent evt) { if (evt.getSource() == courses) { cardLayout.show(screens, "course"); } -// if (evt.getSource() == scheduleCT) { -// cardLayout.show(screens, "scheduleCT"); -// } + if (evt.getSource() == logout) { - cardLayout.show(screens, "welcome"); + try { + logoutController.create(); + showMessageDialog(this, "Successfully logged out"); + cardLayout.show(screens, "welcome"); + } catch (Exception e) { + showMessageDialog(this, "Logout failed"); + } + } } diff --git a/src/main/java/screens/StudentMainScreen.java b/src/main/java/screens/StudentMainScreen.java index d184f3f..2762ac3 100644 --- a/src/main/java/screens/StudentMainScreen.java +++ b/src/main/java/screens/StudentMainScreen.java @@ -1,6 +1,6 @@ package screens; -import entities.StudentUser; +import screens.login_registration.LogoutController; import screens.task_management.todolist_screens.ToDoListPresenter; import screens.task_management.todolist_screens.ToDoListScreen; import use_cases.task_management.todolist_use_case.ToDoListInteractor; @@ -10,6 +10,8 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import static javax.swing.JOptionPane.showMessageDialog; + public class StudentMainScreen extends JPanel implements ActionListener { /** @@ -22,22 +24,24 @@ public class StudentMainScreen extends JPanel implements ActionListener { JButton courses; JButton scheduleCT; + JButton logout; + /** * Objects for connecting to the other screens */ CardLayout cardLayout; JPanel screens; - StudentUser user; + LogoutController logoutController; /** * The window of the main screen with buttons connecting to each use case */ - public StudentMainScreen(StudentUser user, JPanel screens, CardLayout cardLayout) { + public StudentMainScreen(JPanel screens, CardLayout cardLayout, LogoutController controller) { - this.user = user; this.cardLayout = cardLayout; this.screens = screens; + this.logoutController = controller; // Create label for title of screen JLabel title = new JLabel("32 Things To Do"); @@ -50,6 +54,7 @@ public StudentMainScreen(StudentUser user, JPanel screens, CardLayout cardLayout progressTracker = new JButton("Progress Tracker"); courses = new JButton("Courses"); scheduleCT = new JButton("Schedule Collaborative Task"); + logout = new JButton("Logout"); createTask.addActionListener(this); todoList.addActionListener(this); @@ -57,6 +62,7 @@ public StudentMainScreen(StudentUser user, JPanel screens, CardLayout cardLayout progressTracker.addActionListener(this); courses.addActionListener(this); scheduleCT.addActionListener(this); + logout.addActionListener(this); // Create panel for buttons JPanel buttons = new JPanel(); @@ -66,6 +72,7 @@ public StudentMainScreen(StudentUser user, 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); @@ -103,5 +110,15 @@ public void actionPerformed(ActionEvent evt) { if (evt.getSource() == scheduleCT) { cardLayout.show(screens, "scheduleCT"); } + if (evt.getSource() == logout) { + try { + logoutController.create(); + showMessageDialog(this, "Successfully logged out"); + cardLayout.show(screens, "welcome"); + } catch (Exception e) { + showMessageDialog(this, "Logout failed"); + } + } + } } diff --git a/src/main/java/screens/login_registration/LoginScreen.java b/src/main/java/screens/login_registration/LoginScreen.java index 802a2a5..777d9e1 100644 --- a/src/main/java/screens/login_registration/LoginScreen.java +++ b/src/main/java/screens/login_registration/LoginScreen.java @@ -49,17 +49,11 @@ public LoginScreen(LoginController controller, CardLayout cardLayout, JPanel scr logIn.addActionListener(this); cancel.addActionListener(this); -// JPanel main = new JPanel(); -// main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS)); this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); -// this.add(title); this.add(usernameInfo); this.add(passwordInfo); this.add(buttons); -// this.setContentPane(main); -// -// this.pack(); } /** @@ -73,7 +67,7 @@ public void actionPerformed(ActionEvent evt) { try { LoginResponseModel l = loginController.create(username.getText(), String.valueOf(password.getPassword())); - showMessageDialog(this, "% logged in.".format(username.getText())); + showMessageDialog(this, "Successfully logged in."); if (l.getTypeOfUser().equals("Instructor")) { cardLayout.show(screens, "InstructorMain"); } else { diff --git a/src/main/java/screens/login_registration/LogoutController.java b/src/main/java/screens/login_registration/LogoutController.java index ab42e48..8e82009 100644 --- a/src/main/java/screens/login_registration/LogoutController.java +++ b/src/main/java/screens/login_registration/LogoutController.java @@ -1,22 +1,18 @@ package screens.login_registration; import use_cases.login_registration.logout_usecase.LogoutInputBoundary; -import use_cases.login_registration.logout_usecase.LogoutRequestModel; -import use_cases.login_registration.logout_usecase.LogoutResponseModel; import java.io.IOException; public class LogoutController { - final LogoutInputBoundary userInput; + final LogoutInputBoundary interactor; public LogoutController(LogoutInputBoundary accGateway) { - this.userInput = accGateway; + this.interactor = accGateway; } - LogoutResponseModel create(String name, String timeOfLogout) throws IOException { - LogoutRequestModel request = new LogoutRequestModel(); - - return userInput.create(request); + public void create() throws IOException { + interactor.create(); } } diff --git a/src/main/java/screens/login_registration/LogoutFailed.java b/src/main/java/screens/login_registration/LogoutFailed.java new file mode 100644 index 0000000..2934fb8 --- /dev/null +++ b/src/main/java/screens/login_registration/LogoutFailed.java @@ -0,0 +1,8 @@ +package screens.login_registration; + +public class LogoutFailed extends Throwable { + + public LogoutFailed(String error) { + super(error); + } +} \ No newline at end of file diff --git a/src/main/java/screens/login_registration/LogoutResponseFormatter.java b/src/main/java/screens/login_registration/LogoutResponseFormatter.java deleted file mode 100644 index c625e26..0000000 --- a/src/main/java/screens/login_registration/LogoutResponseFormatter.java +++ /dev/null @@ -1,17 +0,0 @@ -package screens.login_registration; - -import use_cases.login_registration.logout_usecase.LogoutPresenter; -import use_cases.login_registration.logout_usecase.LogoutResponseModel; - -public class LogoutResponseFormatter implements LogoutPresenter { - - /** - * @param logout the logout response - * @return the successful logout view - */ - @Override - public LogoutResponseModel prepareSuccessView(LogoutResponseModel logout) { - return logout; - } - -} diff --git a/src/main/java/screens/login_registration/RegisterScreen.java b/src/main/java/screens/login_registration/RegisterScreen.java index 4bd5fcc..06ec28b 100644 --- a/src/main/java/screens/login_registration/RegisterScreen.java +++ b/src/main/java/screens/login_registration/RegisterScreen.java @@ -53,10 +53,6 @@ public RegisterScreen(UserRegController controller, CardLayout cardLayout, JPane JLabel title = new JLabel("Register Screen"); title.setAlignmentX(Component.CENTER_ALIGNMENT); -// JPanel userButtons = new JPanel(); -// userButtons.add(instructor); -// userButtons.add(student); - LabelTextPanel usernameInfo = new LabelTextPanel( new JLabel("Choose username"), username); LabelTextPanel passwordInfo = new LabelTextPanel( @@ -65,8 +61,6 @@ public RegisterScreen(UserRegController controller, CardLayout cardLayout, JPane new JLabel("Enter password again"), repeatPassword); LabelTextPanel chooseTypeOfUser = new LabelTextPanel( new JLabel("I am a (type 'Instructor' or 'Student')") , typeOfUser); -// JLabel chooseTypeOfUser = new JLabel("I am a:"); -// userButtons.add(chooseTypeOfUser); JButton signUp = new JButton("Sign up"); JButton cancel = new JButton("Cancel"); @@ -75,11 +69,8 @@ public RegisterScreen(UserRegController controller, CardLayout cardLayout, JPane buttons.add(signUp); buttons.add(cancel); -// typeOfUser.addActionListener(this); signUp.addActionListener(this); cancel.addActionListener(this); -// instructor.addActionListener(this); -// student.addActionListener(this); this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); @@ -89,13 +80,8 @@ public RegisterScreen(UserRegController controller, CardLayout cardLayout, JPane this.add(repeatPasswordInfo); this.add(chooseTypeOfUser); this.add(buttons); -// this.add(userButtons); - } -// private void add(LabelTextPanel usernameInfo) { -// } - /** * React to a button click that results in event. */ @@ -110,7 +96,7 @@ public void actionPerformed(ActionEvent evt) { String.valueOf(password.getPassword()), String.valueOf(repeatPassword.getPassword()), String.valueOf(typeOfUser.getText())); - showMessageDialog(this, "%s created.".format(username.getText())); + showMessageDialog(this, "Registration successful and logged in"); if (String.valueOf(typeOfUser.getText()).equals("Student")) { cardLayout.show(screens, "StudentMain"); } else if (String.valueOf(typeOfUser.getText()).equals("Instructor")) { diff --git a/src/main/java/use_cases/login_registration/logout_usecase/LogoutInputBoundary.java b/src/main/java/use_cases/login_registration/logout_usecase/LogoutInputBoundary.java index e1f79c7..ef5b3d0 100644 --- a/src/main/java/use_cases/login_registration/logout_usecase/LogoutInputBoundary.java +++ b/src/main/java/use_cases/login_registration/logout_usecase/LogoutInputBoundary.java @@ -4,10 +4,8 @@ public interface LogoutInputBoundary { - /** - * @param request the request to logout - * @return the logout response - * @throws IOException + /** Save a User's information into the database before they log out + * @throws IOException if saving not successful */ - LogoutResponseModel create(LogoutRequestModel request) throws IOException; + void create() throws IOException; } diff --git a/src/main/java/use_cases/login_registration/logout_usecase/LogoutInteractor.java b/src/main/java/use_cases/login_registration/logout_usecase/LogoutInteractor.java index 4470846..327f8a1 100644 --- a/src/main/java/use_cases/login_registration/logout_usecase/LogoutInteractor.java +++ b/src/main/java/use_cases/login_registration/logout_usecase/LogoutInteractor.java @@ -1,5 +1,6 @@ package use_cases.login_registration.logout_usecase; +import entities.CurrentUser; import entities.InstructorUser; import entities.StudentUser; import entities.User; @@ -18,31 +19,24 @@ public class LogoutInteractor implements LogoutInputBoundary { */ final LogoutGateway userGateway; - final LogoutPresenter userPresenter; - - final User user; + private User user; /** * @param gateway the logout gateway (which interacts with the User database) - * @param logoutPresenter the logout presenter - * @param u the User that is logging out */ - public LogoutInteractor(LogoutGateway gateway, LogoutPresenter logoutPresenter, User u) { + public LogoutInteractor(LogoutGateway gateway) { this.userGateway = gateway; - this.userPresenter = logoutPresenter; - this.user = u; + this.user = CurrentUser.getCurrentUser(); } /** * Save a new UserRegSaveRequest which contains all of the information in the User that is trying to * log out into the User database. - * @param request the request to logout - * @return the logout response * @throws IOException if logout fails */ @Override - public LogoutResponseModel create(LogoutRequestModel request) throws IOException { - + public void create() throws IOException { + this.user = CurrentUser.getCurrentUser(); LocalDateTime now = LocalDateTime.now(); UserRegSaveRequest userModel; @@ -58,8 +52,5 @@ public LogoutResponseModel create(LogoutRequestModel request) throws IOException } userGateway.save(userModel); - - LogoutResponseModel accResponseModel = new LogoutResponseModel(user.getName(), now.toString()); - return userPresenter.prepareSuccessView(accResponseModel); } } diff --git a/src/main/java/use_cases/login_registration/logout_usecase/LogoutRequestModel.java b/src/main/java/use_cases/login_registration/logout_usecase/LogoutRequestModel.java deleted file mode 100644 index 7fbeeb2..0000000 --- a/src/main/java/use_cases/login_registration/logout_usecase/LogoutRequestModel.java +++ /dev/null @@ -1,4 +0,0 @@ -package use_cases.login_registration.logout_usecase; - -public class LogoutRequestModel { -} diff --git a/src/main/java/use_cases/login_registration/logout_usecase/LogoutResponseModel.java b/src/main/java/use_cases/login_registration/logout_usecase/LogoutResponseModel.java index 02129bc..2b9cb07 100644 --- a/src/main/java/use_cases/login_registration/logout_usecase/LogoutResponseModel.java +++ b/src/main/java/use_cases/login_registration/logout_usecase/LogoutResponseModel.java @@ -22,12 +22,4 @@ public LogoutResponseModel(String name, String timeOfLogout) { public void setName(String username) { this.name = username; } - public String getLogoutTime() { - return logoutTime; - } - - public void setLogoutTime(String TimeOfLogout) { - this.logoutTime = TimeOfLogout; - } - } 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 2050604..e730a2e 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 @@ -52,13 +52,10 @@ public UserRegResponse create(UserRegRequest request) throws IOException { if (request.getTypeOfUser().equals("Instructor")) { this.user = userFactory.createInstructor(request.getName(), request.getPassword()); - } else //if (request.getTypeOfUser().equals("Student")) + } else // initialize a StudentUser { this.user = userFactory.createStudent(request.getName(), request.getPassword()); } -// else { -// this.user = null; -// } if (!user.checkPassword()) { return userPresenter.prepareFailView("Password must be at least 9 characters long"); diff --git a/src/test/java/logout_usecase/LogoutInteractorTest.java b/src/test/java/logout_usecase/LogoutInteractorTest.java index be88ba4..c797386 100644 --- a/src/test/java/logout_usecase/LogoutInteractorTest.java +++ b/src/test/java/logout_usecase/LogoutInteractorTest.java @@ -1,58 +1,28 @@ package logout_usecase; -import entities.GeneralUserFactory; -import entities.InstructorUser; -import entities.User; -import entities.UserFactory; +import entities.*; import org.junit.jupiter.api.Test; import screens.login_registration.InMemoryUser; -import screens.login_registration.LogoutResponseFormatter; -import screens.login_registration.UserRegResponseFormatter; import use_cases.login_registration.logout_usecase.*; -import use_cases.login_registration.user_register_usecase.*; import java.io.IOException; -import static org.junit.jupiter.api.Assertions.*; -import static org.junit.jupiter.api.Assertions.fail; - public class LogoutInteractorTest { @Test void create() throws IOException { - LogoutGateway userRepository = new InMemoryUser(); - - // This creates an anonymous implementing class for the Output Boundary. - LogoutPresenter presenter = new LogoutResponseFormatter() { - - @Override - public LogoutResponseModel prepareSuccessView(LogoutResponseModel logout) { - // 4) Check that the Output Data and associated changes - // are correct - assertEquals("paul", logout.getName()); - assertNotNull(logout.getLogoutTime()); // any creation time is fine. - return null; - } - - public LogoutResponseModel prepareFailView(String error) { - fail("Use case failure is unexpected."); - return null; - } - - }; + InMemoryUser userRepository = new InMemoryUser(); - User u = new InstructorUser("paul", "123456789"); - LogoutInputBoundary interactor = new LogoutInteractor(userRepository, presenter, u); + User u = new InstructorUser("jallope", "123456789"); + CurrentUser.setCurrentUser(u); - // 2) Input data — we can make this up for the test. Normally it would - // be created by the Controller. - LogoutRequestModel request = new LogoutRequestModel(); + LogoutInputBoundary interactor = new LogoutInteractor(userRepository); // 3) Run the use case - interactor.create(request); + interactor.create(); // was the user saved after logout? - assert ((InMemoryUser) userRepository).existsByName("paul"); + assert userRepository.existsByName("jallope"); } } diff --git a/src/test/java/FileUserTest.java b/src/test/java/user_register_usecase/FileUserTest.java similarity index 98% rename from src/test/java/FileUserTest.java rename to src/test/java/user_register_usecase/FileUserTest.java index c2a019a..c5505f1 100644 --- a/src/test/java/FileUserTest.java +++ b/src/test/java/user_register_usecase/FileUserTest.java @@ -1,3 +1,5 @@ +package user_register_usecase; + import entities.InstructorUser; import entities.StudentUser; import org.junit.jupiter.api.Test;