Skip to content

Commit

Permalink
use case for writing to .csv, sample classes for testing
Browse files Browse the repository at this point in the history
- added a use case for writing schedule data to the .csv
- new sample "HRSystem", "Employee" and "Main" classes to facilitate testing
  • Loading branch information
JasmineChu committed Aug 6, 2023
1 parent 8bc2dcf commit 6d14af3
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 60 deletions.
42 changes: 5 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
# Project Template
# How to Run the Program

This is a template repository for CSC 207 projects.
This repository contains starter code for a gradle project.
It also contains workflow documents that give instructions on how to manage your Github repository and how to use Github Projects for efficient collaboration.
Since this feature is only the "Schedule" portion of the overall HR Management System, there are some dependencies on classes that are present throughout the entire system.
There have been stand-in classes ("Employee" and "HRSystem") made to test this portion somewhat independently, as well as a "Main" class that can be run to simulate an active HR System that would open the Schedule interface.

## Checklist For Your Project
- [ ] Verify the correct settings for your project repository
- [ ] Set up Github Projects
- [ ] Create the implementation plan using issues and Github Projects
- [ ] Create deveopment branches for your features
- [ ] Use pull requests to merge finished features into main branch
- [ ] Conduct code reviews

**If your team has trouble with any of these steps, please ask on Piazza. For example, with how GitHub Classroom works, your team *may* not have permissions to do some of the first few steps, in which case we'll post alternative instructions as needed.**

## Workflow Documents

* Github Workflow: Please refer to the workflow that was introduced in the first lab. You should follow this when working on your code. The following document provides additional details too.

* [Project Planning and Development Guide](project_plan_dev.md): This document helps you to understand how to create and maintain a project plan for your class project. **This document helps you to complete the Implementation Plan Milestone.**

## Gradle Project
Import this project into your Intellij editor. It should automatically recognise this as a gradle repository.
The starter code was built using SDK version 11.0.1. Ensure that you are using this version for this project. (You can, of course, change the SDK version as per your requirement if your team has all agreed to use a different version)

You have been provided with two starter files for demonstration: HelloWorld and HelloWorldTest.

You will find HelloWorld in `src/main/java/tutorial` directory. Right click on the HelloWorld file and click on `Run HelloWorld.main()`.
This should run the program and print on your console.

You will find HelloWorldTest in `src/test/java/tutorial` directory. Right click on the HelloWorldTest file and click on `Run HelloWorldTest`.
All tests should pass. Your team can remove this sample of how testing works once you start adding your project code to the repo.

Moving forward, we expect you to maintain this project structure. You *should* use Gradle as the build environment, but it is fine if your team prefers to use something else -- just remove the gradle files and push your preferred project setup. Assuming you stick with Gradle, your source code should go into `src/main/java` (you can keep creating more subdirectories as per your project requirement). Every source class can auto-generate a test file for you. For example, open HelloWorld.java file and click on the `HelloWorld` variable as shown in the image below. You should see an option `Generate` and on clicking this your should see an option `Test`. Clicking on this will generate a JUnit test file for `HelloWorld` class. This was used to generate the `HelloWorldTest`.

![image](https://user-images.githubusercontent.com/5333020/196066655-d3c97bf4-fdbd-46b0-b6ae-aeb8dbcf351d.png)

You can create another simple class and try generating a test for this class.
A sample set of Employees has been hard-coded into the "Main" class in the "drivers" package, as well as a pre-set .csv file that matches the data.
Run said class to test the scheduling feature.
40 changes: 40 additions & 0 deletions src/main/java/drivers/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package drivers;

import entities.Employee;
import entities.HRSystem;
import interfaceadapters.ScheduleController;
import usecases.ScheduleFileReader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {

public static void main(String[] args) {
try {
String fileName = "ScheduleData.csv";
String currentWorkingDirectory = System.getProperty("user.dir");
String filePath = currentWorkingDirectory + "\\src\\main\\java\\resources\\" + fileName;
List<String[]> data = ScheduleFileReader.readCSV(filePath);

// 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);

ScheduleUI scheduleUI = new ScheduleUI();

ScheduleController scheduleController = new ScheduleController(hrSystem, scheduleUI, data);
scheduleController.openUI();
} catch (IOException e) {
System.out.println("Failed to open and read the schedule .csv file.");
}

}

}
14 changes: 6 additions & 8 deletions src/main/java/drivers/SchedulePanel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package drivers;

import interfaceadapters.ScheduleController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
Expand All @@ -21,12 +23,10 @@ public SchedulePanel(String[][] data) {
title.setAlignmentX(Component.CENTER_ALIGNMENT);

//Buttons
JButton newShift = new JButton("New Shift");
JButton removeShift = new JButton("Remove Shift");
JButton saveButton = new JButton("Save Schedule to File");

JPanel buttons = new JPanel();
buttons.add(newShift);
buttons.add(removeShift);
buttons.add(saveButton);

//Table
this.data = data;
Expand Down Expand Up @@ -56,7 +56,6 @@ public void mousePressed(MouseEvent e) {
JTable target = (JTable) e.getSource();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
System.out.println(row + " and " + column);

if (row >= 0 && column > 0) {
ShiftChangeScreen shiftChangeScreen = new ShiftChangeScreen(row, column, data);
Expand All @@ -70,8 +69,7 @@ public void mousePressed(MouseEvent e) {
}
}
});
newShift.addActionListener(this);
removeShift.addActionListener(this);
saveButton.addActionListener(this);
}

public static void deleteNewFrame(){
Expand All @@ -81,7 +79,7 @@ public static void deleteNewFrame(){

@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Click " + e.getActionCommand());
ScheduleController.callFileWriter(data);
}


Expand Down
7 changes: 5 additions & 2 deletions src/main/java/drivers/ShiftChangePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public ShiftChangePanel(boolean add, int row, int column, String[][] data) {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == confirmButton) {
System.out.println(employeeListBox.getSelectedItem());

String employee = (String) employeeListBox.getSelectedItem();
String command = confirmButton.getText();
Expand Down Expand Up @@ -94,7 +93,11 @@ public String updateData(String command, String employee, String shift) {
shift = shift.replace(deletionTarget, "");
return shift;
case "Add Employee":
return shift + ", " + employee;
if (shift.equals("")) {
return employee;
} else {
return shift + ", " + employee;
}
default:
return "";
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/drivers/ShiftChangeScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public ShiftChangeScreen(int row, int column, String[][] data) {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(title);
this.add(tabbedPane);
System.out.println("ShiftChangeScreen running.");
}

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

public class Employee {

private String id;
private String name;

public Employee(String id, String name) {
this.id = id;
this.name = name;
}

public String getId() {
return this.id;
}
}
30 changes: 30 additions & 0 deletions src/main/java/entities/HRSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package entities;

import java.util.ArrayList;

public class HRSystem {

private ArrayList<Employee> employees;

public HRSystem(ArrayList<Employee> employees) {
this.employees = employees;
}

public ArrayList<Employee> getEmployees() {
return this.employees;
}

private void addEmployee(Employee employee) {
this.employees.add(employee);
}

private void removeEmployee(String id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
employees.remove(employee);
return;
}
}
}

}
2 changes: 0 additions & 2 deletions src/main/java/entities/Shift.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public class Shift<Employee> {
public Shift(String duration, ArrayList<String> employees) {
this.duration = duration;
this.employees = employees;

System.out.println(employees);
}

public String getDuration() {
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/interfaceadapters/ScheduleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import entities.Employee;
import entities.HRSystem;
import entities.Schedule;
import usecases.AddEmployee;
import usecases.CreateSchedule;
import usecases.RemoveEmployee;
import usecases.ScheduleDataParser;
import usecases.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -32,7 +30,6 @@ public ScheduleController(HRSystem hrSystem, ScheduleUI scheduleUI, List<String[

public void openUI() {
this.scheduleUI.createUI(this.parsedData);
System.out.println("Opened Schedule UI.");
}

public static void modifyShift(String str, int dayIndex, int shiftIndex, String employee) {
Expand All @@ -56,4 +53,15 @@ public static ArrayList<String> getAllEmployeesID() {
return allEmployeesID;
}

public static void callFileWriter(String[][] data) {
try {
String fileName = "ScheduleData.csv";
String currentWorkingDirectory = System.getProperty("user.dir");
String filePath = currentWorkingDirectory + "\\src\\main\\java\\resources\\" + fileName;
ScheduleFileWriter.writeCSV(data, filePath);
} catch (IOException e) {
System.out.println("Failed to open and write to the schedule .csv file.");
}
}

}
4 changes: 2 additions & 2 deletions src/main/java/resources/ScheduleData.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"SHIFT: 9:00AM - 5:00PM"
"001 002","001","001","001","001","001","001"
"004","001 002","001","001","001","001","001"
"SHIFT: 5:00PM - 11:00PM"
"001 002","002","002","002","002","002","002"
"001 002","002","002","002","002","002","002"
1 change: 0 additions & 1 deletion src/main/java/usecases/AddEmployee.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

public interface AddEmployee {
static void addEmployee(Schedule schedule, int dayIndex, int shiftIndex, String employee) {
System.out.println("Attempting to add employee.");

Shift shift = schedule.getShift(shiftIndex);
shift.addEmployee(dayIndex, employee);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/usecases/RemoveEmployee.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

public interface RemoveEmployee {
static void removeEmployee(Schedule schedule, int dayIndex, int shiftIndex, String employee) {
System.out.println("Attempting to remove employee.");

Shift shift = schedule.getShift(shiftIndex);
shift.removeEmployee(dayIndex, employee);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/usecases/ScheduleFileReader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package usecases;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/usecases/ScheduleFileWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package usecases;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class ScheduleFileWriter {

public static void writeCSV(String[][] data, String filePath) throws IOException {
FileWriter writer = new FileWriter(filePath);
ArrayList<String[]> parsedData = parseForCSV(data);
for (String[] row : parsedData) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < row.length; i++) {
sb.append(row[i]);
if (i < row.length - 1) {
sb.append(",");
}
}
sb.append(System.lineSeparator());
writer.write(sb.toString());
}
writer.close();
}

public static ArrayList<String[]> parseForCSV(String[][] data) {
ArrayList<String[]> parsedData = new ArrayList<>();
for (String[] lst : data) {
String shiftText = "\"SHIFT: " + lst[0] + "\"";
parsedData.add(new String[]{shiftText});
String[] newLst = new String[lst.length - 1];
for (int i = 1; i < lst.length; i++) {
newLst[i - 1] = "\"" + lst[i].replaceAll(",", "") + "\"";
}
parsedData.add(newLst);
}
return parsedData;
}

}

0 comments on commit 6d14af3

Please sign in to comment.