Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Movie {
- constructor(movieName, ticketPrice) {
- this.movieName = movieName;
- this.ticketPrice = Number(ticketPrice);
- this.screenings = [];
- this._findMovie = function (date, hall) {
- return this.screenings.find((o) => o.date === date && o.hall === hall);
- };
- this.totalProfit = 0;
- this.totalSoldTickets = 0;
- }
- newScreening(date, hall, description) {
- const movieObj = { date, hall, description };
- const isPresent = this._findMovie(date, hall);
- if (isPresent) {
- throw Error(`Sorry, ${hall} hall is not available on ${date}`);
- } else {
- this.screenings.push(movieObj);
- return `New screening of ${this.movieName} is added.`
- }
- }
- endScreening(date, hall, soldTickets) {
- const isPresent = this._findMovie(date, hall);
- if (isPresent) {
- const currentProfit = Number(soldTickets) * this.ticketPrice;
- this.totalProfit += currentProfit;
- this.totalSoldTickets += soldTickets;
- let index = 0;
- for (let i = 0; i < this.screenings.length; i++) {
- const screening = this.screenings[i];
- if (screening.date === date && screening.hall === hall) {
- index = i;
- break;
- }
- }
- this.screenings.splice(index, 1);
- return `${this.movieName} movie screening on ${date} in ${hall} hall has ended. Screening profit: ${currentProfit}`;
- } else {
- throw Error(`Sorry, there is no such screening for ${this.movieName} movie.`);
- }
- }
- toString() {
- let result = [
- `${this.movieName} full information:`,
- `Total profit: ${this.totalProfit.toFixed(0)}$`,
- `Sold tickets: ${this.totalSoldTickets.toFixed(0)}`
- ];
- if (this.screenings.length > 0) {
- result.push("Remaining film screenings:");
- let sorted = this.screenings
- .sort((a, b) => a.hall.localeCompare(b.hall))
- .forEach(s => result.push(`${s.hall} - ${s.date} - ${s.description}`));
- } else {
- result.push(`No more screenings!`);
- }
- return result.join('\n')
- }
- }
- let m = new Movie('Wonder Woman 1984', '10.00');
- console.log(m.newScreening('October 2, 2020', 'IMAX 3D', `3D`));
- console.log(m.newScreening('October 3, 2020', 'Main', `regular`));
- console.log(m.newScreening('October 4, 2020', 'IMAX 3D', `3D`));
- console.log(m.endScreening('October 2, 2020', 'IMAX 3D', 150));
- console.log(m.endScreening('October 3, 2020', 'Main', 78));
- console.log(m.toString());
- m.newScreening('October 4, 2020', '235', `regular`);
- m.newScreening('October 5, 2020', 'Main', `regular`);
- m.newScreening('October 3, 2020', '235', `regular`);
- m.newScreening('October 4, 2020', 'Main', `regular`);
- console.log(m.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement