Advertisement
divanov94

Untitled

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