-
Notifications
You must be signed in to change notification settings - Fork 0
/
Schedule.cpp
224 lines (173 loc) · 6.59 KB
/
Schedule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//
// Schedule.cpp
//
//
// Created by Ethan Blagg on 11/6/19.
//
#include "Schedule.hpp"
// for convenience
using json = nlohmann::json;
void Schedule::createSectionsFromFile(fstream& file) {
Section sectionBuf; // read sections from json file into buffer
json jsonSections; // json file object
if (!file.fail()) {
file >> jsonSections; // get json object from file
}
else { // failed to open file
cout << "ERROR: Failed to open file";
exit(0);
}
// create Section objects from json file
for (int i = 0; i < jsonSections["courses"].size(); i++) {
//cout << i << "......\n"; // Debug
sectionBuf.setCourse(jsonSections["courses"][i]["course"]);
sectionBuf.setDays(jsonSections["courses"][i]["days"]);
sectionBuf.setStartTime(jsonSections["courses"][i]["start"]);
sectionBuf.setEndTime(jsonSections["courses"][i]["end"]);
// Add section to Sections list if not a repeat
if (allowRepeatSections) { // dont care if repeated
allSections.push_back(sectionBuf);
}
else if (!isRepeatSection(sectionBuf)) {// care if repeat, and not repeat
allSections.push_back(sectionBuf);
} // otherwise don't push
} // for
} // getSectionsFromFile
bool Schedule::isRepeatSection(Section& sec) const {
bool isrepeat = false;
for (Section s : allSections) {
if (sec == s) {
isrepeat = true;
} // if
} // for
return isrepeat;
}
void Schedule::printSections() {
for (Section sec: allSections) {
cout << sec.getCourse() << " "
<< sec.getDays() << " "
<< sec.getStartTime() << " "
<< sec.getEndTime() << " "
<< endl;
}
}
void Schedule::setCourses() {
// Create a vector of distinct courses (use Courses vector)
for (Section sec: allSections) {
bool match = false;
for (int i = 0; i < courses.size(); i++) {
if ( !sec.getCourse().compare(courses[i].getCourse()) ) {
// course already exists (strings equal)
int numSecs = courses[i].addSection(sec);
match = true;
}
}
if (!match) { // Need to add the course!
addCourse(sec);
}
} // for sec
} // setCourses
void Schedule::addCourse(string cID) {
Course temp(cID);
courses.push_back(temp);
}
void Schedule::addCourse(Section sec) {
Course temp(sec);
courses.push_back(temp);
}
void Schedule::printCourses() {
for (Course c: courses) {
cout << c.getCourse() << endl;
}
}
void Schedule::printCoursesDetailed() {
for (Course c: courses) {
cout << c.getCourse() << endl;
for (Section s: c.getSections()) {
cout << "\t" << s.getDays()
<< " " << s.getStartTime()
<< " - " << s.getEndTime() << endl;
} // for s
} // for c
}
void Schedule::findNonconflictingSchedules() {
//cout << "\nIn Schedule::findNonconflictingSchedules()\n";
int courseNum = 0; // initial course
vector<Section> initSec; // empty section to get started
checkNextCourse(courseNum, initSec);
}
// NOTE: pretty sure the schedule candidate stuff isn't correct
// ...when/where should a new one be created?
void Schedule::checkNextCourse(int courseNum, vector<Section> &scheduleCandidate) {
//cout << "\n\n\n**** checkNextCourse()\tcourseNum="<< courseNum << "/" << courses.size()-1 << "\n"; // Debug
// go through each section in the course
for (int i = 0; i < courses[courseNum].getSections().size(); i++) {
// Create a new Schedule Candidate (copy of current) and continue
// This is likely not the proper way to do this...fix later
vector<Section> newSchedCand = scheduleCandidate;
// vector<Section> newSchedCand;
// for (int i=0; i < scheduleCandidate.size(); i++) {
// newSchedCand.push_back(scheduleCandidate[i]);
// }
//cout << "Printing schedule candidate size=" << newSchedCand.size()
// << " courseNum=" << courseNum << " section=" << i
// << " id=" << courses[courseNum].getSections()[i].getCourse()<< "\n";
//Section::printVector(newSchedCand);
// check if times conflict
if (!isConflicting(newSchedCand, courses[courseNum].getSections()[i]) ) {
// DONT CONFLICT - Add the section to the candidate
newSchedCand.push_back(courses[courseNum].getSections()[i]);
// Add the candidate to the schedule if in last course
if (courseNum == courses.size() - 1 ) {
//cout << "\tPushing schedule candidate\n"; Debug
schedules.push_back(newSchedCand);
//Section::printVector(newSchedCand); // debug
}
else { // otherwise go to next course (courseNum) if it exists
// cout << "\tGoing to next course\n"; // Debug
checkNextCourse(courseNum+1, newSchedCand);
} // if else
} // if conflicting
} // for
} //
bool Schedule::isConflicting(vector<Section> &sVec, Section &s) {
bool conflict = false;
// Set conflict flag if a start time is during another section
for(Section i: sVec) {
if (s.hasConflictWith(i) ) {
conflict = true;
}
} // for
return conflict;
}
void Schedule::writeToFile(fstream &file) {
json j; // json file object to write to
json jbuf;
for (int i = 0; i < schedules.size(); i++) {
for (int s = 0; s < schedules[i].size(); s++) {
//cout << " " << schedules[i][s].getCourse() << endl; // debug
jbuf[s]["course"] = schedules[i][s].getCourse();
jbuf[s]["days"] = schedules[i][s].getDays();
jbuf[s]["start"] = schedules[i][s].getStartTime();
jbuf[s]["end"] = schedules[i][s].getEndTime();
}
j[i] = jbuf;
}
if (!file.fail()) {
file << j; // get json object from file
}
else { // failed to open file
cout << "ERROR: Failed to open file";
exit(0);
}
}
void Schedule::printScheduleToConsole() {
int perm = 1;
for (vector<Section> s : schedules) {
//for (int i = 0; i < schedules.size(); i++) {
cout << "Schedule # " << perm << endl;
Section::printVector(s);
cout << endl;
perm++;
}
}