-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStudent.js
261 lines (232 loc) · 7.9 KB
/
Student.js
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*globals Workshop, Logger*/
/**
* Student Class.
*
* An object that contains all significant information about a single student.
*
* @param {string} firstName The first name of the student.
* @param {string} lastName The last name of the student.
* @param {array} preferenceArray The ordered array of the student's preferred workshops from most to least preferred.
* @param {int} sessionsPerWorkshop The number of sessions in a workshop.
*/
class Student {
constructor(
firstName,
lastName,
preferenceArray,
grade,
sessionsPerWorkshop
) {
this.firstName = firstName;
this.lastName = lastName;
this.grade = grade;
this.sessionsPerWorkshop = sessionsPerWorkshop;
this.preferences = preferenceArray;
this.assignedWorkshops = Array(sessionsPerWorkshop).fill(null);
this.givenFiller = false;
this.studentScore = 0;
// Points given for workshop assignments from most preferred to least preferred
this.scorerPoints = [1, 3, 5, 10, 11, 12];
// Points to be added for each workshop a student didn't want
this.unpreferredScore = 20;
// Points given for calculating workshop popularity based on what number preference the workshop is listed
this.popularityPoints = [1, 1, 1, 1, 1, 1];
}
/**
* Increments the popularities of all the workshops in the student's
* preference list based on `this.popularityPoints`.
*
* Ignores null preferences.
*/
updatePopularities() {
for (let i = 0; i < this.preferences.length; i++) {
this.preferences[i].incrementPopularity(this.popularityPoints[i]);
}
}
/**
* Assigns this student to a workshop in the student's first available slot.
*
* @param {workshop} workshop The workshop to add the student to.
*
* @throws an error if the student already has workshops in each slot.
*/
assignWorkshop(workshop) {
if (this.fullyAssigned()) {
throw new Error(
this.fullName() + " cannot be assigned any more workshops"
);
}
if (this.isAssigned(workshop)) {
throw new Error("duplicate match");
}
const sessionsByFill = workshop.leastFullSessions();
for (let i = 0; i < sessionsByFill.length; i++) {
//maybe this can be refactored into a "find available slot" method?
const currentSession = sessionsByFill[i];
const timeSlot = currentSession.timeSlot;
if (this.hasTimeSlotFree(timeSlot) && !currentSession.isFull()) {
workshop.addStudent(this);
currentSession.addStudent(this);
this.assignedWorkshops[timeSlot] = workshop;
return;
}
}
Logger.log("problem found");
throw new Error("what is wrong with you");
}
printAssigned() {
Logger.log(this.toString());
for (let i = 0; i < this.assignedWorkshops.length; i++) {
const workshop = this.assignedWorkshops[i];
if (workshop === null) {
Logger.log(" null");
} else {
Logger.log(" " + workshop.name);
}
}
}
assignFiller(workshop) {
if (this.givenFiller) {
throw new Error(
this.toString() + " has already been assigned a filler workshop"
);
}
this.givenFiller = true;
this.assignWorkshop(workshop);
}
/**
* Calculates and returns the number of workshops to which the student has been assigned.
*/
numberAssigned() {
let total = 0;
for (const workshop of this.assignedWorkshops) {
if (workshop != null) {
total += 1;
}
}
return total;
}
/**
* Calculates and returns whether or not the student has been assigned a workshop in all 3 sessions.
*/
fullyAssigned() {
return this.numberAssigned() === this.sessionsPerWorkshop;
}
/**
* Calculates and returns whether or not the student has a full list of preferences.
*/
hasAllPreferences() {
return this.preferences.length >= this.scorerPoints.length;
}
/**
* Returns true if the student has already been assigned the listed workshop, false if not.
*/
isAssigned(workshop) {
return this.assignedWorkshops.indexOf(workshop) !== -1;
}
topAssignedPreference() {
for (let i = 0; i < this.preferences.length; i++) {
const currentWorkshop = this.preferences[i];
if (this.isAssigned(currentWorkshop)) {
return i;
}
}
return -1;
}
hasTimeSlotFree(timeSlot) {
return this.assignedWorkshops[timeSlot] === null;
}
canBeAssigned(workshop) {
if (workshop === null) {
return false;
}
if (workshop.isFull()) {
return false;
}
if (this.isAssigned(workshop)) {
return false;
}
if (this.fullyAssigned(workshop)) {
return false;
}
for (let i = 0; i < this.assignedWorkshops.length; i++) {
if (this.hasTimeSlotFree(i) && !workshop.sessions[i].isFull()) {
return true;
}
}
return false;
}
compensate() {
if (this.fullyAssigned()) {
return;
}
for (let i = 0; i < this.preferences.length; i++) {
const currentPreference = this.preferences[i];
if (currentPreference === null) {
continue;
}
if (this.canBeAssigned(currentPreference)) {
this.assignWorkshop(currentPreference);
return;
}
}
throw new Error(
this.toString() + " could not be compensated for a filler workshop"
);
}
givenFirstPreference() {
const firstPreference = this.preferences[0];
return this.assignedWorkshops.indexOf(firstPreference) !== -1;
}
/**
* Calculates and returns the "score" of this student's assigned workshops based on their preferences.
*/
calculateScore() {
this.studentScore = 0;
for (const assignedWorkshop of this.assignedWorkshops) {
// for each assigned workshop i
let unpreferred = true;
for (let j = 0; j < this.preferences.length; j++) {
// for each preferred workshop j
const preference = this.preferences[j];
if (assignedWorkshop === preference) {
this.studentScore += this.scorerPoints[j];
unpreferred = false;
}
}
if (unpreferred) {
this.studentScore += this.unpreferredScore;
}
}
return this.studentScore;
}
/**
* Calculates and returns whether or not this student's score is equal to a given value.
*
* @param {int} scoreToCheck The value that is compared to this student's score.
*/
compareToScore(scoreToCheck) {
return scoreToCheck === this.studentScore;
}
/**
* Returns the full name of the student as a string.
*/
fullName() {
return this.firstName.concat(" ", this.lastName);
}
checkPreferenceNumbers() {
const preferencearray = [];
for (let i = 0; i < this.assignedWorkshops.length; i++) {
const assignedWorkshop = this.assignedWorkshops[i];
if (this.preferences.indexOf(assignedWorkshop) === -1) {
preferencearray[i] = this.preferences.length;
} else {
preferencearray[i] = this.preferences.indexOf(assignedWorkshop);
}
}
return preferencearray;
}
toString() {
return this.fullName();
}
}