kstoyanov

03. Movie - JS Еxam 12 Aug 2020

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