Skip to content

Commit 15a266f

Browse files
committed
I think multi-schedule now works!!
1 parent 3c0ebfd commit 15a266f

File tree

12 files changed

+178
-98
lines changed

12 files changed

+178
-98
lines changed

bin/comp110/Parser.class

-23 Bytes
Binary file not shown.

bin/comp110/Schedule.class

213 Bytes
Binary file not shown.

bin/tests/ParserTest.class

2.03 KB
Binary file not shown.

src/comp110/Leads.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public Leads(Staff staff) {
1212
}
1313

1414
public void add(String onyen) {
15-
System.out.print(onyen);
1615
Employee e = _staff.getEmployeeByOnyen(onyen);
1716
if (e != null) {
1817
super.add(e);

src/comp110/Parser.java

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class Parser {
2525

2626
// functions
2727
public Parser() {
28-
28+
2929
}
3030

3131
public Employee parseEmployee(String file) throws Exception {
@@ -81,8 +81,9 @@ public Employee parseEmployee(String file) throws Exception {
8181
return new Employee(name, onyen, capacity, gender.equals("M") ? false : true, level, availability);
8282
}
8383

84-
public List<Schedule> parseSchedules(String scheduleFolderPath, String staff_dir, String leadsFile) throws Exception {
85-
84+
public List<Schedule> parseSchedules(String scheduleFolderPath, String staff_dir, String leadsFile)
85+
throws Exception {
86+
8687
List<Schedule> schedules = new ArrayList<Schedule>();
8788

8889
// read in the json file
@@ -105,18 +106,21 @@ public List<Schedule> parseSchedules(String scheduleFolderPath, String staff_dir
105106
json = scanner.nextLine();
106107
} catch (Exception e) {
107108
System.err.println(e.toString());
108-
throw new Exception("Parser::parseSchedule(): Error reading schedule file= "+ scheduleJson.getAbsolutePath());
109+
throw new Exception(
110+
"Parser::parseSchedule(): Error reading schedule file= " + scheduleJson.getAbsolutePath());
109111
}
110112
scanner.close();
111113

112114
// create the json week object
113115
JsonWeek jsonweek = gson.fromJson(json, JsonWeek.class);
114116
if (jsonweek == null) {
115-
throw new Exception("Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + scheduleFolderPath);
117+
throw new Exception(
118+
"Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + scheduleFolderPath);
116119
}
117-
if (jsonweek.getShifts() == null){
120+
if (jsonweek.getShifts() == null) {
118121
// some problem parsing json
119-
throw new Exception("Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + scheduleFolderPath);
122+
throw new Exception(
123+
"Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + scheduleFolderPath);
120124
}
121125

122126
// now we have the json we need to reconstruct the schedule object
@@ -140,25 +144,34 @@ public List<Schedule> parseSchedules(String scheduleFolderPath, String staff_dir
140144
Employee employee = this.getEmployeeByOnyen(onyen, staff);
141145
if (employee == null) {
142146
throw new Exception(
143-
"Parser::parseSchedule(): No Employee in Staff for Onyen in Schdeule. Onyen=" + onyen);
147+
"Parser::parseSchedule(): No Employee in Staff for Onyen in Schdeule. Onyen="
148+
+ onyen);
144149
}
145150
// add the employee to the shift
146151
shifts[day][hour].add(employee);
147152
}
148153
}
149154
}
150155
Leads leads = this.parseLeads(leadsFile, staff);
151-
schedules.add(new Schedule(staff, week, leads, scheduleJson.getName().substring(9, scheduleJson.getName().length()))); // grab just the date portion of file name
156+
schedules.add(new Schedule(staff, week, leads,
157+
scheduleJson.getName().substring(9, scheduleJson.getName().length()).split("\\.")[0])); // grab
158+
// just
159+
// the
160+
// date
161+
// portion
162+
// of
163+
// file
164+
// name
152165
}
153-
166+
154167
if (schedules.size() == 0) {
155-
throw new Exception ("Parser::parseSchedule(): No schedules found to load in: " + scheduleFolderPath);
168+
throw new Exception("Parser::parseSchedule(): No schedules found to load in: " + scheduleFolderPath);
156169
} else {
157170
return schedules;
158171
}
159172
}
160-
161-
public void writeFile(Employee employee, String filename) throws Exception{
173+
174+
public void writeFile(Employee employee, String filename) throws Exception {
162175

163176
// check if employee is null first
164177
if (employee == null) {
@@ -168,7 +181,7 @@ public void writeFile(Employee employee, String filename) throws Exception{
168181
// write to file
169182
PrintWriter fw = null;
170183
try {
171-
System.out.println("Writing Schedule to" + filename);
184+
// System.out.println("Writing Schedule to" + filename);
172185
fw = new PrintWriter(filename);
173186
StringBuilder sb = new StringBuilder();
174187

@@ -216,20 +229,20 @@ public void writeFile(Employee employee, String filename) throws Exception{
216229
fw.close();
217230
} catch (Exception e) {
218231
// unable to open file
219-
if (fw != null){
232+
if (fw != null) {
220233
fw.close();
221234
}
222235
throw new Exception("Parser::writeFile(): Unable to open file for writing. File=" + filename);
223236
}
224237
}
225238

226-
private Staff parseStaff(String dir) throws Exception{
239+
private Staff parseStaff(String dir) throws Exception {
227240
// create staff object
228241
Staff staff = new Staff();
229242

230243
// get a file object to the directory containing all the csv's
231244
File csvDirectory = new File(dir);
232-
245+
233246
// create an employee from each csv
234247
for (File csv : csvDirectory.listFiles()) {
235248
Employee employee = null;
@@ -256,7 +269,7 @@ private Employee getEmployeeByOnyen(String onyen, Staff staff) {
256269
// not able to find employee
257270
return null;
258271
}
259-
272+
260273
private Leads parseLeads(String leadsFile, Staff staff) throws Exception {
261274
BufferedReader reader = null;
262275
Leads leads = new Leads(staff);
@@ -279,41 +292,42 @@ private Leads parseLeads(String leadsFile, Staff staff) throws Exception {
279292
}
280293

281294
public void writeScheduleToJson(List<Schedule> schedules, String path) throws Exception {
282-
if (schedules.size() == 0){
295+
if (schedules.size() == 0) {
283296
throw new Exception("Parser::writeScheduleToJson(): Schedule list is empty");
284297
}
285-
if (path.equals("") || path == null){
298+
if (path.equals("") || path == null) {
286299
throw new Exception("Parser::writeScheduleToJson(): Path is invalid");
287300
}
288-
301+
289302
// create gson object
290303
for (Schedule schedule : schedules) {
291-
try{
304+
try {
292305
Gson gson = new Gson();
293-
JsonWeek jsonWeek = schedule.getWeek().toJsonWeek();
294-
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(new File(path + File.pathSeparator + "schedule-" + schedule.getDatesValid() + ".json")), 65536);
295-
String json = gson.toJson(jsonWeek);
296-
writer.write(json.getBytes());
297-
writer.close();
298-
} catch (Exception e){
299-
throw new Exception("Parser::writeScheduleToJson(): Unable to open file=" + path);
300-
}
306+
JsonWeek jsonWeek = schedule.getWeek().toJsonWeek();
307+
File f = new File(path + "schedule_" + schedule.getDatesValid() + ".json");
308+
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(f), 65536);
309+
String json = gson.toJson(jsonWeek);
310+
writer.write(json.getBytes());
311+
writer.close();
312+
} catch (Exception e) {
313+
throw new Exception("Parser::writeScheduleToJson(): Unable to open file=" + path);
314+
}
301315
}
302316

303317
}
304-
318+
305319
public String parseCurrentVersion(String versionPath) {
306320
BufferedReader reader = null;
307321
try {
308322
reader = new BufferedReader(new FileReader(versionPath));
309323
} catch (FileNotFoundException e) {
310-
return ""; //if we can't find the file just carry on
324+
return ""; // if we can't find the file just carry on
311325
}
312326
String currentVersion = null;
313327
try {
314328
currentVersion = reader.readLine();
315329
reader.close();
316-
} catch (IOException e) { //This should never happen
330+
} catch (IOException e) { // This should never happen
317331
System.err.println("lol this should never happen");
318332
e.printStackTrace();
319333
}

src/comp110/Schedule.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public Schedule copy() {
4747
return new Schedule(this.m_staff.copy(), this.m_week.copy(), this.m_leads.copy(), this.m_datesValid);
4848
}
4949

50-
private void computeShiftLeads() {
50+
public void computeShiftLeads() {
51+
// before computing we should invalidate any existing
52+
invalidateShiftLeads();
5153
for (int day = 0; day < m_week.getShifts().length; day++) {
5254
for (int hour = 0; hour < m_week.getShifts()[day].length; hour++) {
5355
Employee lead = null;
@@ -66,5 +68,13 @@ private void computeShiftLeads() {
6668
}
6769
}
6870
}
71+
72+
private void invalidateShiftLeads() {
73+
for (int day = 0; day < m_week.getShifts().length; day++) {
74+
for (int hour = 0; hour < m_week.getShifts()[day].length; hour++) {
75+
m_week.getShift(day, hour).setLead(null);
76+
}
77+
}
78+
}
6979

7080
}

src/tests/ParserTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package tests;
22

33
import static org.junit.Assert.*;
4+
5+
import java.util.List;
6+
47
import org.junit.*;
58

69
import comp110.Employee;
@@ -40,7 +43,7 @@ public void parseEmployee_test() {
4043
public void parseSchedule_test() {
4144
Parser p = new Parser();
4245
try {
43-
Schedule s = p.parseSchedules("testData/schedule.json", "data/spring-17/staff", "data/fall-17/leads.csv");
46+
List<Schedule> s = p.parseSchedules("testData/schedule.json", "data/spring-17/staff", "data/fall-17/leads.csv");
4447
Shift test1 = new Shift(0, 1, 0);
4548
Shift test2 = new Shift(0, 2, 0);
4649
Shift test3 = new Shift(1, 5, 0);
@@ -89,7 +92,7 @@ public void parseSchedule_test() {
8992
test7.add(cphamlet);
9093
test7.add(sunmiche);
9194

92-
Shift[][] shift = s.getWeek().getShifts();
95+
Shift[][] shift = s.get(0).getWeek().getShifts();
9396

9497
assertEquals(shift[0][1].equals(test1), true);
9598
assertEquals(shift[0][2].equals(test2), true);
@@ -131,9 +134,9 @@ public void writeFile_test() {
131134
public void writeScheduleToJson_Test() {
132135
Parser p = new Parser();
133136
try {
134-
Schedule s = p.parseSchedules("testData/schedule.json", "data/spring-17/staff", "data/fall-17/leads.csv");
137+
List<Schedule> s = p.parseSchedules("testData/schedule.json", "data/spring-17/staff", "data/fall-17/leads.csv");
135138
p.writeScheduleToJson(s, "testData/testSchedule.json");
136-
Schedule result = p.parseSchedules("testData/testSchedule.json", "data/spring-17/staff", "data/fall-17/leads.csv");
139+
List<Schedule> result = p.parseSchedules("testData/testSchedule.json", "data/spring-17/staff", "data/fall-17/leads.csv");
137140
assertEquals(s.equals(result), true);
138141
} catch (Exception e) {
139142
// TODO Auto-generated catch block
@@ -170,9 +173,9 @@ public void writeRepeatedFiles(){
170173
public void writeRepeatedSchedules(){
171174
Parser p = new Parser();
172175
try {
173-
Schedule s = p.parseSchedules("testData/testSchedule.json", "data/spring-17/staff", "data/fall-17/leads.csv");
176+
List<Schedule> s = p.parseSchedules("testData/testSchedule.json", "data/spring-17/staff", "data/fall-17/leads.csv");
174177
p.writeScheduleToJson(s, "testData/testSchedule.json");
175-
Schedule result = p.parseSchedules("testData/testSchedule.json", "data/spring-17/staff", "data/fall-17/leads.csv");
178+
List<Schedule> result = p.parseSchedules("testData/testSchedule.json", "data/spring-17/staff", "data/fall-17/leads.csv");
176179
for(int i = 0; i < 20; i++){
177180
p.writeScheduleToJson(result, "testData/testSchedule.json");
178181
result = p.parseSchedules("testData/testSchedule.json", "data/spring-17/staff", "data/fall-17/leads.csv");

src/ui/AvailabilityStage.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,6 @@ private void handleCheck(ActionEvent event) {
303303
// now that the schedule has changed we can let the person save
304304
_saveAvailabilityButton.setDisable(false);
305305
int[][] updatedAvailability = _ui.getCurrentEmployee().getAvailability();
306-
// System.out.println("Day: " + check.getDay() + " Hour: " +
307-
// check.getHour());
308306
HBox parent = (HBox) check.getParent();
309307
if (check.isSelected()) {
310308
parent.setBackground(new Background(new BackgroundFill(Color.GREEN, null, null)));

0 commit comments

Comments
 (0)