-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchedule.java
More file actions
180 lines (140 loc) · 4.39 KB
/
Schedule.java
File metadata and controls
180 lines (140 loc) · 4.39 KB
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
/***************************************************\
| |
| Copyright ©2005, 2006 |
| Michael Cook |
| |
\***************************************************/
import java.lang.String;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Iterator;
public class Schedule {
private ArrayList<CourseSection> sections;
public Schedule() {
// Set some basic stuff
sections = new ArrayList<CourseSection>(4);
}
public CourseSection getACourse(int i) {
return sections.get(i);
}
public int getCourseCount() {
return sections.size();
}
public ListIterator<CourseSection> getListIterator() {
return sections.listIterator();
}
public Iterator<CourseSection> getIterator() {
return sections.iterator();
}
public ArrayList<String> getMissingCoReqs() {
if (sections.size() == 0)
return null; // Can't be missing co-reqs if we don't have any courses
// First we build a list of courses we need
ArrayList<String> theList = new ArrayList<String>();
ListIterator<CourseSection> cs = sections.listIterator();
CourseSection tempSection;
String temp;
while (cs.hasNext()) {
tempSection = cs.next();
temp = tempSection.getCoReq();
if (temp != null) {
theList.add(temp);
}
}
// System.out.println("Found " + sections.size() + " sections in this schedule.");
// Now we remove the ones we find
cs = sections.listIterator(); // Get a new iterator
while (cs.hasNext()) {
tempSection = cs.next();
temp = tempSection.getCourseName();
theList.remove(temp); // Fullfilled this co-req
}
// System.out.println("This section has " + theList.size() + " outstanding requirements.");
if (theList.size() == 0) {
return null; // All OK.
} else {
return theList;
}
}
public void addSection(CourseSection theSec, Scheduler master) throws SchedulerException {
String theName = theSec.getCourseName();
String oldID = containsCourse(theName);
if (oldID != null) {
removeSection(oldID);
}
// Now we make sure it doesn't conflict
if (sections.size() > 0) {
ListIterator<CourseSection> cs = sections.listIterator();
while (cs.hasNext()) {
CourseSection otherSec = cs.next();
if (master.sectionsConflict(theSec, otherSec)) {
// System.out.println("Not adding " + theSec.getIDCode() + ": conflicts with " +
// otherSec.getIDCode());
throw new SchedulerException("Conflict", "Unable to add section " +
theSec.getIDCode() + ", it conflicts with " +
otherSec.getIDCode());
}
}
}
// If we're here, we add it
sections.add(theSec);
}
public String containsCourse(String theName) {
if (sections.size() > 0) {
ListIterator<CourseSection> cs = sections.listIterator();
while (cs.hasNext()) {
CourseSection theSec = cs.next();
if (theSec.getCourseName() == theName) {
return theSec.getIDCode();
}
}
}
return null;
}
public String getCourseThatNeeds(String theName) {
if (sections.size() > 0) {
ListIterator<CourseSection> cs = sections.listIterator();
while (cs.hasNext()) {
CourseSection theSec = cs.next();
if (theSec.getCoReq() == theName) {
return theSec.getCourseName();
}
}
return null;
} else {
return null;
}
}
public void removeSection(CourseSection sectionToDrop) throws SchedulerException {
removeSection(sectionToDrop.getIDCode());
}
public void removeAll() {
sections = new ArrayList<CourseSection>(4);
}
public void removeSection(String sectionID) throws SchedulerException {
boolean removedSomething = false;
String theName = null;
if (sections.size() > 0) {
ListIterator<CourseSection> cs = sections.listIterator();
while (cs.hasNext()) {
CourseSection theSec = cs.next();
if (theSec.getIDCode() == sectionID) {
removedSomething = true;
theName = theSec.getCourseName();
cs.remove();
break;
}
}
}
// Did we get rid of something?
if (removedSomething) {
String coreqID = null;
coreqID = getCourseThatNeeds(sectionID);
if (coreqID != null)
removeSection(coreqID);
} else {
throw new SchedulerException("CourseSection", "Section " + sectionID +
" wasn't removed because it wasn't found.");
}
}
}