Skip to content

Commit 3b209cb

Browse files
youngjtyoungjt
authored andcommitted
working towwards multi schedule SO. just need to refactor add/drop
1 parent 986aeb3 commit 3b209cb

17 files changed

+231
-140
lines changed

bin/comp110/Controller.class

188 Bytes
Binary file not shown.

bin/comp110/Parser.class

1.05 KB
Binary file not shown.

bin/comp110/Schedule.class

209 Bytes
Binary file not shown.

bin/comp110/Storage.class

1 Byte
Binary file not shown.

bin/tests/ParserTest.class

-1.82 KB
Binary file not shown.

bin/tests/StorageTest.class

-2 Bytes
Binary file not shown.

src/comp110/Controller.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package comp110;
22

3+
import java.util.List;
4+
35
import javafx.application.Platform;
46
import ui.UI;
57

@@ -40,12 +42,12 @@ public void storage_get_files_complete(boolean success, String message) {
4042
});
4143
// go ahead and give them the schedule immediately for use
4244
try {
43-
Schedule schedule = this.m_parser.parseSchedule(this.m_storage.get_schedule_json_filename(),
45+
List<Schedule> schedules = this.m_parser.parseSchedule(this.m_storage.get_schedule_json_folder(),
4446
this.m_storage.get_path_to_onyen_csv_directory(), this.m_storage.get_schedule_leads_filename());
45-
this.m_ui.setSchedule(schedule);
47+
this.m_ui.setSchedules(schedules);
4648
} catch (Exception e) {
4749
// tell the ui things suck
48-
this.m_ui.setSchedule(null);
50+
this.m_ui.setSchedules(null);
4951
Platform.runLater(
5052
() -> this.m_ui.displayMessage("Controller::storage_get_files_complete(): " + e.toString()));
5153
}
@@ -61,10 +63,10 @@ public void uiRequestSchedule() {
6163
// an exception
6264
// ui needs to be ready to handle null schedule
6365
try {
64-
Schedule schedule = this.m_parser.parseSchedule(this.m_storage.get_schedule_json_filename(),
66+
List<Schedule> schedules = this.m_parser.parseSchedule(this.m_storage.get_schedule_json_folder(),
6567
this.m_storage.get_path_to_onyen_csv_directory(), this.m_storage.get_schedule_leads_filename());
66-
this.m_ui.setSchedule(schedule);
67-
Platform.runLater(() -> this.m_ui.displaySchedule(schedule));
68+
this.m_ui.setSchedules(schedules);
69+
Platform.runLater(() -> this.m_ui.displaySchedule(schedules));
6870
} catch (Exception e) {
6971
Platform.runLater(() -> this.m_ui.displayMessage("Controller::uiRequestSchedule(): " + e.toString()));
7072
}
@@ -106,9 +108,9 @@ public void uiRequestSaveAvailability(Employee employee, String commit_message)
106108
}
107109
}
108110

109-
public void uiRequestChangeSchedule(Schedule schedule, String commit_message) {
111+
public void uiRequestChangeSchedule(List<Schedule> schedules, String commit_message) {
110112
try {
111-
this.m_parser.writeScheduleToJson(schedule, this.m_storage.get_schedule_json_filename());
113+
this.m_parser.writeScheduleToJson(schedules, this.m_storage.get_schedule_json_folder());
112114
this.m_storage.save_files(commit_message);
113115
} catch (Exception e) {
114116
this.m_ui.displayMessage("Controller::uiRequestChangeSchedule(): " + e.toString());

src/comp110/Parser.java

Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.io.FileOutputStream;
88
import java.io.FileReader;
99
import java.io.IOException;
10+
import java.util.ArrayList;
11+
import java.util.List;
1012
import java.util.Scanner;
1113
import com.google.gson.Gson;
1214
import java.io.PrintWriter;
@@ -79,63 +81,75 @@ public Employee parseEmployee(String file) throws Exception {
7981
return new Employee(name, onyen, capacity, gender.equals("M") ? false : true, level, availability);
8082
}
8183

82-
public Schedule parseSchedule(String jsonFile, String staff_dir, String leadsFile) throws Exception {
84+
public List<Schedule> parseSchedule(String scheduleFolderPath, String staff_dir, String leadsFile) throws Exception {
85+
86+
List<Schedule> schedules = new ArrayList<Schedule>();
8387

8488
// read in the json file
8589
Gson gson = new Gson();
8690
String json = "";
8791

88-
Scanner scanner = null;
92+
File scheduleFile = null;
8993
try {
90-
File file = new File(jsonFile);
91-
scanner = new Scanner(file);
94+
scheduleFile = new File(scheduleFolderPath);
95+
96+
} catch (Exception e) {
97+
throw new Exception("Parser::parseSchedule(): Error reading schedule folder=" + scheduleFolderPath);
98+
}
99+
100+
for (File scheduleJson : scheduleFile.listFiles()) {
101+
Scanner scanner = new Scanner(scheduleFile);
92102
scanner.useDelimiter("\\Z");
93103
json = scanner.next();
94104
scanner.close();
95-
} catch (FileNotFoundException e) {
96-
throw new Exception("Parser::parseSchedule(): Error reading json file=" + jsonFile);
97-
}
98105

99-
// create the json week object
100-
JsonWeek jsonweek = gson.fromJson(json, JsonWeek.class);
101-
if (jsonweek == null) {
102-
throw new Exception("Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + jsonFile);
103-
}
104-
if (jsonweek.getShifts() == null){
105-
// some problem parsing json
106-
throw new Exception("Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + jsonFile);
107-
}
106+
// create the json week object
107+
JsonWeek jsonweek = gson.fromJson(json, JsonWeek.class);
108+
if (jsonweek == null) {
109+
throw new Exception("Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + scheduleFolderPath);
110+
}
111+
if (jsonweek.getShifts() == null){
112+
// some problem parsing json
113+
throw new Exception("Parser::parseSchedule(): Error parsing json file ito jsonweek. File=" + scheduleFolderPath);
114+
}
108115

109-
// now we have the json we need to reconstruct the schedule object
110-
// first we will build the staff object
111-
Staff staff = null;
112-
try {
113-
staff = parseStaff(staff_dir);
114-
} catch (Exception e) {
115-
throw new Exception("Parser::parseSchedule(): " + e.toString());
116-
}
116+
// now we have the json we need to reconstruct the schedule object
117+
// first we will build the staff object
118+
Staff staff = null;
119+
try {
120+
staff = parseStaff(staff_dir);
121+
} catch (Exception e) {
122+
throw new Exception("Parser::parseSchedule(): " + e.toString());
123+
}
117124

118-
// now build the week object
119-
Week week = new Week("Current Schedule");
120-
121-
// grab a reference to the shifts array
122-
Shift[][] shifts = week.getShifts();
123-
for (int day = 0; day < NUMBER_DAYS; day++) {
124-
for (int hour = 0; hour < NUMBER_HOURS; hour++) {
125-
for (int i = 0; i < jsonweek.getShifts()[day][hour].getScheduled().length; i++) {
126-
String onyen = jsonweek.getShifts()[day][hour].getScheduled()[i];
127-
Employee employee = this.getEmployeeByOnyen(onyen, staff);
128-
if (employee == null) {
129-
throw new Exception(
130-
"Parser::parseSchedule(): No Employee in Staff for Onyen in Schdeule. Onyen=" + onyen);
125+
// now build the week object
126+
Week week = new Week("Current Schedule");
127+
128+
// grab a reference to the shifts array
129+
Shift[][] shifts = week.getShifts();
130+
for (int day = 0; day < NUMBER_DAYS; day++) {
131+
for (int hour = 0; hour < NUMBER_HOURS; hour++) {
132+
for (int i = 0; i < jsonweek.getShifts()[day][hour].getScheduled().length; i++) {
133+
String onyen = jsonweek.getShifts()[day][hour].getScheduled()[i];
134+
Employee employee = this.getEmployeeByOnyen(onyen, staff);
135+
if (employee == null) {
136+
throw new Exception(
137+
"Parser::parseSchedule(): No Employee in Staff for Onyen in Schdeule. Onyen=" + onyen);
138+
}
139+
// add the employee to the shift
140+
shifts[day][hour].add(employee);
131141
}
132-
// add the employee to the shift
133-
shifts[day][hour].add(employee);
134142
}
135143
}
144+
Leads leads = this.parseLeads(leadsFile, staff);
145+
schedules.add(new Schedule(staff, week, leads, scheduleJson.getName().substring(9, scheduleJson.getName().length()))); // grab just the date portion of file name
146+
}
147+
148+
if (schedules.size() == 0) {
149+
throw new Exception ("Parser::parseSchedule(): No schedules found to load in: " + scheduleFolderPath);
150+
} else {
151+
return schedules;
136152
}
137-
Leads leads = this.parseLeads(leadsFile, staff);
138-
return new Schedule(staff, week, leads);
139153
}
140154

141155
public void writeFile(Employee employee, String filename) throws Exception{
@@ -258,25 +272,28 @@ private Leads parseLeads(String leadsFile, Staff staff) throws Exception {
258272
return leads;
259273
}
260274

261-
public void writeScheduleToJson(Schedule schedule, String path) throws Exception{
262-
if (schedule == null){
263-
throw new Exception("Parser::writeScheduleToJson(): Schedule is null");
275+
public void writeScheduleToJson(List<Schedule> schedules, String path) throws Exception {
276+
if (schedules.size() == 0){
277+
throw new Exception("Parser::writeScheduleToJson(): Schedule list is empty");
264278
}
265279
if (path.equals("") || path == null){
266280
throw new Exception("Parser::writeScheduleToJson(): Path is invalid");
267281
}
268282

269283
// create gson object
270-
try{
271-
Gson gson = new Gson();
272-
JsonWeek jsonWeek = schedule.getWeek().toJsonWeek();
273-
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(new File(path)), 65536);
274-
String json = gson.toJson(jsonWeek);
275-
writer.write(json.getBytes());
276-
writer.close();
277-
} catch (Exception e){
278-
throw new Exception("Parser::writeScheduleToJson(): Unable to open file=" + path);
284+
for (Schedule schedule : schedules) {
285+
try{
286+
Gson gson = new Gson();
287+
JsonWeek jsonWeek = schedule.getWeek().toJsonWeek();
288+
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(new File(path + File.pathSeparator + "schedule-" + schedule.getDatesValid() + ".json")), 65536);
289+
String json = gson.toJson(jsonWeek);
290+
writer.write(json.getBytes());
291+
writer.close();
292+
} catch (Exception e){
293+
throw new Exception("Parser::writeScheduleToJson(): Unable to open file=" + path);
294+
}
279295
}
296+
280297
}
281298

282299
public String parseCurrentVersion(String versionPath) {

src/comp110/Schedule.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ public class Schedule implements Serializable {
1212
private Staff m_staff;
1313
private Week m_week;
1414
private Leads m_leads;
15+
private String m_datesValid;
1516

1617
// functions
17-
public Schedule(Staff staff, Week week, Leads leads) {
18+
public Schedule(Staff staff, Week week, Leads leads, String datesValid) {
1819
this.m_staff = staff;
1920
this.m_week = week;
2021
this.m_leads = leads;
22+
this.m_datesValid = datesValid;
2123
this.computeShiftLeads();
2224
}
2325

@@ -32,13 +34,17 @@ public Week getWeek() {
3234
public Leads getLeads() {
3335
return this.m_leads;
3436
}
37+
38+
public String getDatesValid() {
39+
return this.m_datesValid;
40+
}
3541

3642
public boolean equals(Schedule other) {
3743
return this.m_staff.equals(other.m_staff) && this.m_week.equals(other.m_week);
3844
}
3945

4046
public Schedule copy() {
41-
return new Schedule(this.m_staff.copy(), this.m_week.copy(), this.m_leads.copy());
47+
return new Schedule(this.m_staff.copy(), this.m_week.copy(), this.m_leads.copy(), this.m_datesValid);
4248
}
4349

4450
private void computeShiftLeads() {

src/comp110/Storage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public String get_availability_csv_filename_from_onyen(String onyen){
6363
return this.m_application_directory + DEFAULT_LOCAL_REPO_FOLDER + "/data/fall-17/staff/" + onyen + ".csv";
6464
}
6565

66-
public String get_schedule_json_filename(){
67-
return this.m_application_directory + DEFAULT_LOCAL_REPO_FOLDER + "/data/schedule.json";
66+
public String get_schedule_json_folder(){
67+
return this.m_application_directory + DEFAULT_LOCAL_REPO_FOLDER + "/data/currentSchedules";
6868
}
6969

7070
public String get_schedule_leads_filename(){

0 commit comments

Comments
 (0)