Skip to content

Commit

Permalink
refactor drivers packages, javadoc, test cases
Browse files Browse the repository at this point in the history
- added sub packages hrsystemui and scheduleui
- specificity in javadocs
- new test cases class for ScheduleController
  • Loading branch information
JasmineChu committed Aug 11, 2023
1 parent 52608a3 commit 5e5b403
Show file tree
Hide file tree
Showing 15 changed files with 193 additions and 30 deletions.
22 changes: 12 additions & 10 deletions src/main/java/drivers/Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package drivers;

import entities.Employee;
import drivers.scheduleui.ScheduleUI;
import entities.HRSystem;
import interfaceadapters.HRSystemController;
import interfaceadapters.ScheduleController;

import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -14,18 +14,20 @@
public class Main {

public static void main(String[] args) {
// Sample HRSystem
ArrayList<Employee> employees = new ArrayList<>();
employees.add(new Employee("001", "name"));
employees.add(new Employee("002", "name"));
employees.add(new Employee("003", "name"));
employees.add(new Employee("004", "name"));
employees.add(new Employee("005", "name"));
HRSystem hrSystem = new HRSystem(employees);
HRSystemController hrSystemController = new HRSystemController();
HRSystem hrSystem = hrSystemController.createHRSystem();

ScheduleUI scheduleUI = new ScheduleUI();
List<String[]> data = ScheduleController.callFileReader();
ScheduleController scheduleController = new ScheduleController(hrSystem, scheduleUI, data);

// Sample HRSystem
scheduleController.createEmployee("001", "name");
scheduleController.createEmployee("002", "name");
scheduleController.createEmployee("003", "name");
scheduleController.createEmployee("004", "name");
scheduleController.createEmployee("005", "name");

scheduleController.openUI();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package drivers;
package drivers.hrsystemui;

import drivers.scheduleui.SchedulePanel;
import interfaceadapters.ScheduleController;

import javax.swing.*;
Expand Down Expand Up @@ -68,6 +69,7 @@ public void actionPerformed(ActionEvent e) {
if (!scheduleController.createEmployee(id, name)) {
JOptionPane.showMessageDialog(this,
"Invalid input. Employee with ID already exists.");
return;
}
JOptionPane.showMessageDialog(this, "New employee created.");
SchedulePanel.deleteNewFrame();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package drivers;
package drivers.hrsystemui;

import drivers.scheduleui.SchedulePanel;
import interfaceadapters.ScheduleController;

import javax.swing.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package drivers;
package drivers.scheduleui;

import drivers.hrsystemui.CreateEmployeePanel;
import drivers.hrsystemui.DeleteEmployeePanel;
import interfaceadapters.ScheduleController;

import javax.swing.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package drivers;
package drivers.scheduleui;

import interfaceadapters.ScheduleController;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package drivers;
package drivers.scheduleui;

import interfaceadapters.ScheduleController;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package drivers;
package drivers.scheduleui;

import interfaceadapters.ScheduleController;

Expand Down
33 changes: 27 additions & 6 deletions src/main/java/entities/Shift.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/**
* Schedule.java
* Class for a Shift.
* Employees in the shift are stored in ArrayList<String> employees. Each index of employees represents a day in the
* week, from Sunday (index = 0) to Saturday (index = 6). Each String employee is separated by ", ".
*/
public class Shift<Employee> {

Expand All @@ -18,12 +20,32 @@ public Shift(String duration, ArrayList<String> employees) {

/**
* containsEmployee
* Returns whether this Shift contains and Employee with String id.
* Returns whether this Shift contains an Employee with String id.
* @param id, String for id of employee to be compared.
*/
public boolean containsEmployee(String id) {
for (String compareId : employees) {
if (id.equals(compareId)) {
String [] compareIds = compareId.split(", ");
for (String str : compareIds) {
if (id.equals(str)) {
return true;
}
}
}
return false;
}

/**
* containsEmployee
* Returns whether this Shift at the given dayIndex contains an Employee with String id.
* @param id, String for id of employee to be compared.
* @param dayIndex, int for specific day to be checked.
*/
public boolean containsEmployee(String id, int dayIndex) {
String compareId = employees.get(dayIndex);
String [] compareIds = compareId.split(", ");
for (String str : compareIds) {
if (id.equals(str)) {
return true;
}
}
Expand All @@ -49,14 +71,13 @@ public void addEmployee(int dayIndex, String employee) {
* @param employee, String for employee to be removed.
*/
public void removeEmployee(int dayIndex, String employee) {
System.out.println(this.employees);
String day = this.employees.get(dayIndex);
if (day.contains(employee + ", ")) {
day.replace(employee + ", ", "");
this.employees.set(dayIndex, day.replace(employee + ", ", ""));
} else if (day.contains(", " + employee)) {
day.replace(", " + employee, "");
this.employees.set(dayIndex, day.replace(", " + employee, ""));
} else {
day.replace(employee, "");
this.employees.set(dayIndex, day.replace(employee, ""));
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/interfaceadapters/HRSystemController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package interfaceadapters;

import entities.Employee;
import entities.HRSystem;
import usecases.modifyhrsystem.CreateHRSystem;

import java.util.ArrayList;

public class HRSystemController implements CreateHRSystem {

/**
* createHRSystem
* Creates and returns a new HRSystem.
* @return HRSystem.
*/
public HRSystem createHRSystem() {
HRSystem hrSystem = new HRSystem(new ArrayList<Employee>());
return hrSystem;
}
}
22 changes: 17 additions & 5 deletions src/main/java/interfaceadapters/ScheduleController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package interfaceadapters;

import drivers.ScheduleUI;
import drivers.scheduleui.ScheduleUI;
import entities.Employee;
import entities.HRSystem;
import entities.Schedule;
import entities.Shift;
import usecases.modifyemployees.CreateEmployee;
import usecases.modifyemployees.DeleteEmployee;
import usecases.modifyhrsystem.CreateEmployee;
import usecases.modifyhrsystem.DeleteEmployee;
import usecases.modifyfiles.ScheduleDataParser;
import usecases.modifyfiles.ScheduleFileReader;
import usecases.modifyfiles.ScheduleFileWriter;
Expand Down Expand Up @@ -74,6 +74,15 @@ public ArrayList<String> getAllEmployeesID() {
return allEmployeesID;
}

/**
* getSchedule
* Gets this ScheduleController's schedule.
* @return Schedule, this ScheduleController's schedule.
*/
public Schedule getSchedule() {
return this.schedule;
}

/**
* callFileWriter
* Calls readCSV from the ScheduleFileReader.
Expand Down Expand Up @@ -157,7 +166,8 @@ public Schedule createSchedule(String[][] data) {

/**
* createEmployee
* Attempts to create an employee in the HRSystem given an ID and name.
* Create an employee in the HRSystem given an ID and name. If there already exists an employee with the inputted
* ID, does not create said employee and returns false.
* @param newId, String ID of employee to be created.
* @param name, String name of employee to be created.
* @return boolean, true if Employee is successfully created, false otherwise.
Expand All @@ -177,7 +187,8 @@ public boolean createEmployee(String newId, String name) {

/**
* deleteEmployee
* Deletes an employee in the HRSystem.
* Deletes an employee in the HRSystem. If the employee is still scheduled in shifts, does not delete said employee
* and returns false.
* @param id, String ID of employee to be deleted.
* @return boolean, true if Employee is successfully deleted, false otherwise.
*/
Expand All @@ -189,4 +200,5 @@ public boolean deleteEmployee(String id) {
hrSystem.removeEmployee(id);
return true;
}

}
1 change: 0 additions & 1 deletion src/main/java/usecases/modifyfiles/ScheduleFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class ScheduleFileReader {
public static List<String[]> readCSV(String filePath) throws IOException {
List<String[]> data = new ArrayList<>();

System.out.println("Attempting to open file...");
try (BufferedReader csvReader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = csvReader.readLine()) != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package usecases.modifyemployees;
package usecases.modifyhrsystem;

public interface CreateEmployee {

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/usecases/modifyhrsystem/CreateHRSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package usecases.modifyhrsystem;

import entities.HRSystem;

/**
* CreateHRSystem.java
* Interface for use case CreateHRSystem.
*/
public interface CreateHRSystem {

/**
* createHRSystem
* Creates a new HRSystem.
*/
HRSystem createHRSystem();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package usecases.modifyemployees;
package usecases.modifyhrsystem;

public interface DeleteEmployee {

Expand Down
88 changes: 88 additions & 0 deletions src/test/java/interfaceadapters/ScheduleControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package interfaceadapters;

import drivers.scheduleui.ScheduleUI;
import entities.HRSystem;
import entities.Schedule;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

class ScheduleControllerTest {

HRSystemController hrSystemController = new HRSystemController();
HRSystem hrSystem = hrSystemController.createHRSystem();

ScheduleUI scheduleUI = new ScheduleUI();
List<String[]> data = ScheduleController.callFileReader();
ScheduleController scheduleController = new ScheduleController(hrSystem, scheduleUI, data);
Schedule schedule = scheduleController.getSchedule();

@Test
public void GetAllEmployeesID() {
ArrayList<String> lst = new ArrayList<>();
lst.add("001");
lst.add("002");
lst.add("003");
scheduleController.createEmployee("001", "name");
scheduleController.createEmployee("002", "name");
scheduleController.createEmployee("003", "name");
Assertions.assertEquals(lst, scheduleController.getAllEmployeesID());
}

@Test
public void AddEmployeeFirstDay() {
scheduleController.addEmployee(schedule, 0, 0, "001");
Assertions.assertEquals(true, schedule.getShift(0).containsEmployee("001", 0));
}

@Test
public void AddEmployeeMiddleDay() {
scheduleController.addEmployee(schedule, 3, 0, "002");
Assertions.assertEquals(true, schedule.getShift(0).containsEmployee("002", 3));
}

@Test
public void AddEmployeeLastDay() {
scheduleController.addEmployee(schedule, 6, 0, "002");
Assertions.assertEquals(true, schedule.getShift(0).containsEmployee("002", 6));
}

@Test
public void RemoveEmployeeStartOfShiftList() {
scheduleController.removeEmployee(schedule, 0, 0, "004");
Assertions.assertEquals(false, schedule.getShift(0).containsEmployee("004", 0));
}

@Test
public void RemoveEmployeeMiddleOfShiftList() {
scheduleController.addEmployee(schedule, 0, 0, "005");
scheduleController.addEmployee(schedule, 0, 0, "006");
scheduleController.removeEmployee(schedule, 0, 0, "005");
Assertions.assertEquals(false, schedule.getShift(0).containsEmployee("005", 0));
}

@Test
public void RemoveEmployeeEndOfShiftList() {
scheduleController.addEmployee(schedule, 0, 0, "005");
scheduleController.addEmployee(schedule, 0, 0, "006");
scheduleController.removeEmployee(schedule, 0, 0, "006");
Assertions.assertEquals(false, schedule.getShift(0).containsEmployee("006", 0));
}

@Test
public void CreateEmployee() {
scheduleController.createEmployee("001", "name");
Assertions.assertEquals(true, scheduleController.getAllEmployeesID().contains("001"));
}

@Test
public void DeleteEmployee() {
scheduleController.createEmployee("006", "name");
scheduleController.deleteEmployee("006");
Assertions.assertEquals(false, scheduleController.getAllEmployeesID().contains("006"));
}
}

0 comments on commit 5e5b403

Please sign in to comment.