-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduler.js
90 lines (78 loc) · 2.47 KB
/
Scheduler.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
class Scheduler {
constructor() {
this.theaterHours = {
Monday: { open: 8 * 60, close: 23 * 60 },
Tuesday: { open: 8 * 60, close: 23 * 60 },
Wednesday: { open: 8 * 60, close: 23 * 60 },
Thursday: { open: 8 * 60, close: 23 * 60 },
Friday: { open: 10.5 * 60, close: 23.5 * 60 },
Saturday: { open: 10.5 * 60, close: 23.5 * 60 },
Sunday: { open: 10.5 * 60, close: 23.5 * 60 },
};
this.setupTime = 60;
this.cleanupTime = 35;
this.movies = [];
this.schedule = {};
}
setMovies(movies) {
this.movies = movies;
}
calculateMovieEndTimes(startTime, runTime) {
return startTime + runTime;
}
adjustToNearestFive(minutes) {
return minutes - (minutes % 5);
}
makeDailySchedule(day) {
this.schedule[day] = [];
const { open, close } = this.theaterHours[day];
this.movies.forEach((movie) => {
let totalTimePerShowing = movie.runTime + this.cleanupTime;
let latestStart = close - movie.runTime;
latestStart = this.adjustToNearestFive(latestStart);
while (latestStart - totalTimePerShowing >= open - this.setupTime) {
if (latestStart >= open + this.setupTime) {
let startTime = latestStart;
let endTime = this.calculateMovieEndTimes(startTime, movie.runTime);
this.schedule[day].unshift({
movie: movie.title,
startTime: startTime,
endTime: endTime,
});
latestStart = this.adjustToNearestFive(
latestStart - totalTimePerShowing
);
} else {
break;
}
}
});
}
printSchedule() {
for (const day in this.schedule) {
console.log(`${day}:`);
const showings = this.schedule[day];
this.movies.forEach((movie) => {
const movieShowings = showings.filter(
(showing) => showing.movie === movie.title
);
if (movieShowings.length > 0) {
console.log(`${movie.title} - ${movie.runTime} minutes`);
movieShowings.forEach((showing) => {
const startTime = this.formatTime(showing.startTime);
const endTime = this.formatTime(showing.endTime);
console.log(` ${startTime} - ${endTime}`);
});
}
});
}
}
formatTime(minutes) {
const hours = Math.floor(minutes / 60);
const mins = minutes % 60;
return `${hours.toString().padStart(2, "0")}:${mins
.toString()
.padStart(2, "0")}`;
}
}
module.exports = Scheduler;