Liliana797979

movie - js advanced exam

Oct 1st, 2021
170
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.totalProfit = 0;
  7.     this.ticketsCount = 0;
  8.   }
  9.  
  10.   newScreening(date, hall, description) {
  11.     let obj = { date: date, hall: hall, desc: description };
  12.     let screen = this.screenings.find(
  13.       (o) => o.date === date && o.hall === hall
  14.     );
  15.     if (screen === undefined) {
  16.       this.screenings.push(obj);
  17.       return `New screening of ${this.movieName} is added.`;
  18.     } else {
  19.       throw new Error(`Sorry, ${hall} hall is not available on ${date}`);
  20.     }
  21.   }
  22.  
  23.   endScreening(date, hall, soldTickets) {
  24.     let screen = this.screenings.find(
  25.       (o) => o.date === date && o.hall === hall
  26.     );
  27.     if (screen === undefined) {
  28.       throw new Error(
  29.         `Sorry, there is no such screening for ${this.movieName} movie.`
  30.       );
  31.     } else {
  32.       let currentProfit = Number(soldTickets) * Number(this.ticketPrice);
  33.       this.totalProfit += currentProfit;
  34.       this.ticketsCount += Number(soldTickets);
  35.       let idx = 0;
  36.       for (let i = 0; i < this.screenings.length; i++) {
  37.         if (
  38.           this.screenings[i].date === date &&
  39.           this.screenings[i].hall === hall
  40.         ) {
  41.           idx = i;
  42.           break;
  43.         }
  44.       }
  45.       this.screenings.splice(idx, 1);
  46.       return `${this.movieName} movie screening on ${date} in ${hall} hall has ended. Screening profit: ${currentProfit}`;
  47.     }
  48.   }
  49.  
  50.   toString() {
  51.     let result = [
  52.       `${this.movieName} full information:`,
  53.       `Total profit: ${this.totalProfit.toFixed(0)}$`,
  54.       `Sold Tickets: ${this.ticketsCount}`,
  55.     ];
  56.     if (this.screenings.length > 0) {
  57.       result.push(`Remaining film screenings:`);
  58.       let sorted = this.screenings.sort((a, b) => a.hall.localeCompare(b.hall));
  59.       for (const s of sorted) {
  60.         result.push(`${s.hall} - ${s.date} - ${s.desc}`);
  61.       }
  62.     } else {
  63.       result.push(`No more screenings!`);
  64.     }
  65.     return result.join("\n");
  66.   }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment