Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Movie {
  2.     constructor(movieName, ticketPrice) {
  3.         this.movieName = movieName;
  4.         this.ticketPrice = Number(ticketPrice);
  5.         this.screenings = [];
  6.         this._findMovie = function (date, hall) {
  7.             return this.screenings.find((o) => o.date === date && o.hall === hall);
  8.         };
  9.         this.totalProfit = 0;
  10.         this.totalSoldTickets = 0;
  11.     }
  12.  
  13.     newScreening(date, hall, description) {
  14.         const movieObj = { date, hall, description };
  15.         const isPresent = this._findMovie(date, hall);
  16.  
  17.         if (isPresent) {
  18.             throw Error(`Sorry, ${hall} hall is not available on ${date}`);
  19.         } else {
  20.             this.screenings.push(movieObj);
  21.             return `New screening of ${this.movieName} is added.`
  22.         }
  23.     }
  24.  
  25.     endScreening(date, hall, soldTickets) {
  26.         const isPresent = this._findMovie(date, hall);
  27.  
  28.         if (isPresent) {
  29.             const currentProfit = Number(soldTickets) * this.ticketPrice;
  30.             this.totalProfit += currentProfit;
  31.             this.totalSoldTickets += soldTickets;
  32.  
  33.             let index = 0;
  34.  
  35.             for (let i = 0; i < this.screenings.length; i++) {
  36.                 const screening = this.screenings[i];
  37.                 if (screening.date === date && screening.hall === hall) {
  38.                     index = i;
  39.                     break;
  40.                 }
  41.             }
  42.  
  43.             this.screenings.splice(index, 1);
  44.  
  45.             return `${this.movieName} movie screening on ${date} in ${hall} hall has ended. Screening profit: ${currentProfit}`;
  46.  
  47.         } else {
  48.             throw Error(`Sorry, there is no such screening for ${this.movieName} movie.`);
  49.         }
  50.     }
  51.  
  52.     toString() {
  53.         let result = [
  54.             `${this.movieName} full information:`,
  55.             `Total profit: ${this.totalProfit.toFixed(0)}$`,
  56.             `Sold tickets: ${this.totalSoldTickets.toFixed(0)}`
  57.         ];
  58.  
  59.         if (this.screenings.length > 0) {
  60.             result.push("Remaining film screenings:");
  61.             let sorted = this.screenings
  62.                 .sort((a, b) => a.hall.localeCompare(b.hall))
  63.                 .forEach(s => result.push(`${s.hall} - ${s.date} - ${s.description}`));
  64.         } else {
  65.             result.push(`No more screenings!`);
  66.         }
  67.  
  68.         return result.join('\n')
  69.     }
  70. }
  71.  
  72. let m = new Movie('Wonder Woman 1984', '10.00');
  73. console.log(m.newScreening('October 2, 2020', 'IMAX 3D', `3D`));
  74. console.log(m.newScreening('October 3, 2020', 'Main', `regular`));
  75. console.log(m.newScreening('October 4, 2020', 'IMAX 3D', `3D`));
  76. console.log(m.endScreening('October 2, 2020', 'IMAX 3D', 150));
  77. console.log(m.endScreening('October 3, 2020', 'Main', 78));
  78. console.log(m.toString());
  79.  
  80. m.newScreening('October 4, 2020', '235', `regular`);
  81. m.newScreening('October 5, 2020', 'Main', `regular`);
  82. m.newScreening('October 3, 2020', '235', `regular`);
  83. m.newScreening('October 4, 2020', 'Main', `regular`);
  84. console.log(m.toString());
  85.  
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement