-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcinemaTickets.js
More file actions
219 lines (195 loc) · 5.93 KB
/
Copy pathcinemaTickets.js
File metadata and controls
219 lines (195 loc) · 5.93 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
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
function createLocation(x, y) {
'use strict';
return { x: x, y: y };
}
function getDistance(location1, location2) {
'use strict';
return Math.sqrt((location1.x - location2.x) * (location1.x - location2.x) +
(location1.y - location2.y) * (location1.y - location2.y));
}
function createUser(location, options) {
'use strict';
options = options || {};
return {
location: location,
name: options.name || "Username",
age: options.age || 0
};
}
//name is string
function createFilm(name, options) {
'use strict';
options = options || {};
return {
name: name,
minimumAge: options.minimumAge || 0
};
}
//time in format hh:mm
//dayOfWeek in ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
function getDate(dayOfWeek, time) {
'use strict';
var dayOfMonth = {
'Mon': 5,
'Tue': 6,
'Wed': 7,
'Thu': 1,
'Fri': 2,
'Sat': 3,
'Sun': 4
};
return new Date(dayOfWeek + ', 0' + dayOfMonth[dayOfWeek] + ' Jan 1970 ' + time + ':00');
}
//time is a Datetime object (only hours and minutes and have a meaning)
function createSession(dayOfWeek, time, ticketCount, ticketPrice) {
'use strict';
return {
date: getDate(dayOfWeek, time),
ticketCount: ticketCount,
ticketPrice: ticketPrice
};
}
//sessions is an array of sessions
function createTimetable(film, sessions) {
'use strict';
return {
film: film,
sessions: sessions
};
}
//timetables is an object array of timetables
function createCinema(name, location, timetables) {
'use strict';
return {
name: name,
location: location,
timetables: timetables
};
}
function laterToday(date1, date2) {
'use strict';
if (date1.getDay() !== date2.getDay()) {
return false;
}
if (date1.getHours() < date2.getHours()) {
return false;
}
if (date1.getHours() === date2.getHours() && date1.getMinutes() <= date2.getMinutes()) {
return false;
}
return true;
}
function filterTodaySessions(cinemas, date) {
'use strict';
var i, j, newCinemas = [], cinema, newTimetables, timetable, newSessions,
sessionFilter = function (session) {
return laterToday(session.date, date);
};
for (i = 0; i < cinemas.length; i += 1) {
cinema = cinemas[i];
newTimetables = [];
for (j = 0; j < cinema.timetables.length; j += 1) {
timetable = cinema.timetables[j];
newSessions = timetable.sessions.filter(sessionFilter);
if (newSessions.length !== 0) {
newTimetables.push(createTimetable(timetable.film, newSessions));
}
}
if (newTimetables.length !== 0) {
newCinemas.push(createCinema(cinema.name, cinema.location, newTimetables));
}
}
return newCinemas;
}
function filterByFilmName(cinemas, name) {
'use strict';
var newCinemas = [], i, cinema, newTimetables,
filmFilter = function (timetable) {
return timetable.film.name === name;
};
for (i = 0; i < cinemas.length; i += 1) {
cinema = cinemas[i];
newTimetables = cinema.timetables.filter(filmFilter);
if (newTimetables.length !== 0) {
newCinemas.push(createCinema(cinema.name, cinema.location, newTimetables));
}
}
return newCinemas;
}
function sortByUserLocation(cinemas, user) {
'use strict';
return cinemas.sort(function (cinema1, cinema2) {
return getDistance(cinema1.location, user.location) -
getDistance(cinema2.location, user.location);
});
}
function getTop(cinemas, count) {
'use strict';
return cinemas.slice(0, count);
}
Array.prototype.filterTodaySessions = function (date) {
'use strict';
return filterTodaySessions(this, date);
}
Array.prototype.filterByFilmName = function (name) {
return filterByFilmName(this, name);
}
Array.prototype.sortByUserLocation = function (user) {
return sortByUserLocation(this, user);
}
Array.prototype.getTop = function (count) {
return getTop(this, count);
}
function testSetUp() {
var film1, film2, timetable1, timetable2, timetable3, timetable4, cinema1, cinema2;
film1 = createFilm('Godzilla');
film2 = createFilm('King-Kong');
timetable1 = createTimetable(film1, [
createSession('Mon', '12:00', 100, 100),
createSession('Tue', '13:00', 100, 100)
]);
timetable2 = createTimetable(film2, [
createSession('Wed', '14:00', 100, 100),
createSession('Thu', '15:00', 100, 100)
]);
timetable3 = createTimetable(film1, [
createSession('Fri', '16:00', 100, 100),
createSession('Sat', '17:00', 100, 100)
]);
timetable4 = createTimetable(film2, [
createSession('Sun', '10:00', 100, 100),
createSession('Sun', '11:00', 100, 100)
]);
cinema1 = createCinema('BigCinema', createLocation(0, 0), [timetable1, timetable2]);
cinema2 = createCinema('SmallCinema', createLocation(100, 100), [timetable3, timetable4]);
return [cinema1, cinema2];
}
function assert(testName, statement) {
if (statement)
console.log(testName + ' passed.');
else
console.log(testName + ' failed.');
}
function test1() {
var cinemas = testSetUp(), user;
user = createUser(createLocation(10, 10));
cinemas = cinemas
.sortByUserLocation(user);
assert('test1', cinemas[0].name === 'BigCinema');
}
function test2() {
var cinemas = testSetUp(), cinema;
cinemas = cinemas.filterTodaySessions(getDate('Sun', '10:30'));
cinema = cinemas[0];
assert('test2', cinema.timetables[0].sessions[0].date.getHours() === 11);
}
function test3() {
var cinemas = testSetUp().filterByFilmName('Godzilla');
assert('test3', cinemas[0].timetables.length == 1 && cinemas[1].timetables.length == 1);
}
window.onload = function() {
'use strict';
test1();
test2();
test3();
}