-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
302 lines (198 loc) · 9.2 KB
/
main.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"use strict"
console.log(`main linked`)
const moviesArr = [];
const cinemasArr = []
const resultDiv = document.getElementById("resultDiv");
const notFoundMessage = element => `Error, "${element}" not found.`;
const successMessage = procedure => `${procedure} successfully!`;
const normalizeString = stringInput => stringInput.toUpperCase().trim();
const hardcodedMovie1 = new Movie ("Memoirs of a geisha", "Rob Marshall", 2005);
const hardcodedMovie2 = new Movie ("Inception", "Cristopher Nolan", 2010);
const hardcodedMovie3 = new Movie ("Gravity", "Alfonso Cuarón", 2013);
const hardcodedMovie4 = new Movie ("Ex Machina", "Alex Garland", 2014);
const hardcodedMovie5 = new Movie ("The Fifth Element", "Luc Besson", 1997);
const hardcodedMovie6 = new Movie ("Dune", "Denis Villeneuve", 2021);
const hardcodedMovie7 = new Movie ("A Space Odyssey", "Stanley Kubrick", 1968);
const hardcodedCinema1 = new Cinema ("CinemaLand", "Cinesa", "Barcelona");
const hardcodedCinema2 = new Cinema ("Cinelalala", "Renoir Cinemas", "L'Hospi");
moviesArr.push(hardcodedMovie1, hardcodedMovie2, hardcodedMovie3, hardcodedMovie4, hardcodedMovie5, hardcodedMovie6, hardcodedMovie7);
cinemasArr.push(hardcodedCinema1, hardcodedCinema2);
hardcodedCinema1.pushMoviesToCinema(hardcodedMovie1);
hardcodedCinema1.pushMoviesToCinema(hardcodedMovie3);
hardcodedCinema1.pushMoviesToCinema(hardcodedMovie5);
hardcodedCinema1.pushMoviesToCinema(hardcodedMovie7);
hardcodedCinema2.pushMoviesToCinema(hardcodedMovie2);
hardcodedCinema2.pushMoviesToCinema(hardcodedMovie4);
hardcodedCinema2.pushMoviesToCinema(hardcodedMovie6);
console.table(moviesArr);
console.table(cinemasArr);
function movieIsFoundFun(rawInputString) {
resultDiv.innerHTML = "";
let normInputString = normalizeString(rawInputString);
let foundMovie = moviesArr.find( movie => movie.title.toUpperCase().trim() === normInputString );
if (foundMovie !== null && foundMovie !== undefined) {
return foundMovie;
}
}
function indexOfMovieIsFound(rawInputString) {
resultDiv.innerHTML = "";
let normInputString = normalizeString(rawInputString);
let foundIndexOfMovie = moviesArr.findIndex( movie => movie.title.toUpperCase().trim() === normInputString);
if (foundIndexOfMovie) {
return foundIndexOfMovie;
}
}
function cinemaIsFoundFun(rawInputString) {
resultDiv.innerHTML = "";
let normInputString = normalizeString(rawInputString);
console.log(`rawinputstring -> ${rawInputString} // normInputstring -> ${normInputString}`);
let foundCinema = cinemasArr.find( cinema => cinema.name.toUpperCase().trim() === normInputString );
console.log(`foundCinema en la funcion de busqueda despues del find -> ${foundCinema}`);
if (foundCinema) {
return foundCinema;
}
}
function indexOfCinemaIsFound(rawInputString) {
resultDiv.innerHTML = "";
let normInputString = normalizeString(rawInputString);
console.log(`rawinputstring -> ${rawInputString} // normInputstring -> ${normInputString}`);
// let foundIndexOfCinema = cinemasArr.findIndex( cinema => cinema.name.toUpperCase().trim() === normInputString );
let foundIndexOfCinema = cinemasArr.findIndex(cinema => {console.log(`Comparando: ${cinema.name.toUpperCase().trim()} === ${normInputString}`)});
console.log(`foundindexofCinema en la funcion de busqueda despues del find -> ${foundIndexOfCinema}`);
if (foundIndexOfCinema) {
return foundIndexOfCinema;
}
}
function createNewMovie() {
resultDiv.innerHTML = "";
let newMovieTitle = document.getElementById("newMovieNameInput").value;
let newMovieDirector = document.getElementById("newMovieDirectorInput").value;
let newMovieReleaseDate = parseInt(document.getElementById("newMovieReleaseDateInput").value);
let userConfirmation = true;
if (!newMovieTitle || !newMovieDirector|| isNaN(newMovieReleaseDate)) {
resultDiv.innerHTML = `All data is required, the release date must be a number.`
return
}
let movieIsFound = movieIsFoundFun(newMovieTitle);
if (movieIsFound) {
userConfirmation = confirm(`A movie with this title already exists, are you sure you want to add this title?`);
}
if (!userConfirmation) {
return;
} else {
let newInstanceOfMovie = new Movie (newMovieTitle, newMovieDirector, newMovieReleaseDate);
moviesArr.push(newInstanceOfMovie);
resultDiv.innerHTML = successMessage("Created") + "<br>" + newInstanceOfMovie.toString();
}
}
function createNewCinema() {
resultDiv.innerHTML = "";
let newCinemaName = document.getElementById("newCinemaNameInput").value;
let newCinemaCompany = document.getElementById("newCinemaCompanyInput").value;
let newCinemaCity = document.getElementById("newCinemaCityInput").value;
let userConfirmation = true;
let newCinemaInstance = {};
if (!newCinemaName || !newCinemaCompany || !newCinemaCity) {
resultDiv.innerHTML = `All data is required.`
return
}
let cinemaIsFound = cinemaIsFoundFun(newCinemaName);
if (cinemaIsFound) {
userConfirmation = confirm(`A movie with this title already exists, are you sure you want to add this title anyways?`);
}
if (!userConfirmation) {
return;
} else {
newCinemaInstance = new Cinema (newCinemaName, newCinemaCompany, newCinemaCity);
cinemasArr.push(newCinemaInstance);
resultDiv.innerHTML = successMessage("Cinema added") + "<br>" + newCinemaInstance.toString();
}
}
function pushMovieToCinemaFun() {
resultDiv.innerHTML = "";
let movieToPush = document.getElementById("movieToPush").value;
let targetedCinema = document.getElementById("targetedCinema").value;
let foundMovie = movieIsFoundFun(movieToPush);
let foundCinema = cinemaIsFoundFun(targetedCinema);
console.log(`this is foundCinema in line 163 -> ${foundCinema}`)
if (foundMovie && foundCinema) {
foundCinema.pushMoviesToCinema(foundMovie);
resultDiv.innerHTML = successMessage(`Process completed`);
} else {
resultDiv.innerHTML = notFoundMessage("movie/cinema");
}
}
function readData() {
let selectMenuShowData = document.getElementById("selectShowData").value; // "movie" // "cinema"
let rawInput = document.getElementById("instanceToShow").value;
let foundMovie = movieIsFoundFun(rawInput);
let foundCinema = cinemaIsFoundFun(rawInput);
switch (selectMenuShowData) {
case "movie" :
if (foundMovie) {
resultDiv.innerHTML = foundMovie.toString();
console.log(`estamos entrando al switch de readData()`);
} else {
return resultDiv.innerHTML = notFoundMessage(rawInput);
}
break;
case "cinema" :
if (foundCinema) {
resultDiv.innerHTML = foundCinema.toString();
console.log(`estamos entrando al switch de readData()`);
} else {
return resultDiv.innerHTML = notFoundMessage(rawInput);
}
break;
default : resultDiv.innerHTML = `Choose one option, please.`;
}
}
function deleteInstance() {
resultDiv.innerHTML = "";
let selectMenuDeleteData = document.getElementById("selectDeleteData").value;
let rawInput = document.getElementById("instanceToDelete").value;
let indexOfMovie = indexOfMovieIsFound(rawInput);
let indexOfCinema = indexOfCinemaIsFound(rawInput);
let foundMovie = movieIsFoundFun(rawInput);
let foundCinema = cinemaIsFoundFun(rawInput);
if (selectMenuDeleteData === "movie") {
if ( foundMovie && indexOfMovie) {
moviesArr.splice(indexOfMovie, 1);
resultDiv.innerHTML = successMessage(`${rawInput} deleted`);
} else {
resultDiv.innerHTML = notFoundMessage(rawInput);
}
} else if (selectMenuDeleteData === "cinema") {
if ( foundCinema && indexOfCinema) {
cinemasArr.splice(indexOfCinema, 1);
resultDiv.innerHTML = successMessage(`${rawInput} deleted`);
} else {
resultDiv.innerHTML = notFoundMessage(rawInput);
}
} else {
resultDiv.innerHTML = `Choose one option, please.`;
}
console.log(`rawInput: ${rawInput}`);
console.log(`indexOfMovie: ${indexOfMovie}`);
console.log(`indexOfCinema: ${indexOfCinema}`);
console.log(`selectMenuDeleteData: ${selectMenuDeleteData}`);
}
// switch (selectMenuDeleteData) {
// case "movie" :
// if (indexOfMovie !== -1) {
// moviesArr.splice(indexOfMovie, 1);
// resultDiv.innerHTML = successMessage(`${rawInput} deleted`);
// } else {
// resultDiv.innerHTML = notFoundMessage(rawInput);
// }
// break;
// case "cinema" :
// if (indexOfCinema !== -1) {
// cinemasArr.splice(indexOfCinema, 1);
// resultDiv.innerHTML = successMessage(`${rawInput} deleted`);
// } else {
// resultDiv.innerHTML = notFoundMessage(rawInput);
// }
// break;
// default : resultDiv.innerHTML = `Choose one option, please.`;
// }