mirozspace

cinema3

Oct 22nd, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. class Movie {
  2. movieName;
  3. ticketPrice;
  4. screenings;
  5. totalSoldTickets;
  6. totalProfit;
  7.  
  8. constructor(movieName, ticketPrice) {
  9. this.movieName = movieName;
  10. this.ticketPrice = Number(ticketPrice);
  11. this.screenings = [];
  12. this.totalSoldTickets = 0;
  13. this.totalProfit = 0;
  14. }
  15.  
  16. newScreening(date, hall, description) {
  17. if (this.screenings.some(s => s.date === date && s.hall === hall)) {
  18. throw new Error(`Sorry, ${hall} hall is not available on ${date}`)
  19. }
  20. this.screenings.push({date: date, hall: hall, description: description, profit: 0.00});
  21. return `New screening of ${this.movieName} is added.`;
  22. }
  23.  
  24. endScreening(date, hall, soldTickets) {
  25. if (this.screenings.length === 0) {
  26. throw new Error(`Sorry, there is no such screening for ${this.movieName} movie.`)
  27. }
  28. let findScreen = this.screenings.find(e => (e.date === date && e.hall === hall));
  29. if (findScreen) {
  30. findScreen.profit += Number(soldTickets) * Number(this.ticketPrice);
  31. this.totalSoldTickets += soldTickets;
  32. this.screenings = this.screenings.filter(scr => scr.date !== date || scr.hall !== hall);
  33. return `${this.movieName} movie screening on ${date} in ${hall} hall has ended. Screening profit: ${findScreen.profit}`;
  34. }
  35. }
  36.  
  37. toString() {
  38. let result = '';
  39. result += `${this.movieName} full information:\n`;
  40. result += `Total profit: ${this.totalSoldTickets * this.ticketPrice}$\n`
  41. result += `Sold Tickets: ${this.totalSoldTickets}\n`;
  42. if (this.screenings.length > 0) {
  43. result += 'Remaining film screenings:\n';
  44. let scrPrintSorted = this.screenings.sort((a, b) => a.hall.localeCompare(b.hall));
  45. for (let i = 0; i < scrPrintSorted.length; i++) {
  46. result += `${scrPrintSorted[i].hall} - ${scrPrintSorted[i].date} - ${scrPrintSorted[i].description}\n`;
  47. }
  48. } else {
  49. result += `No more screenings!`;
  50. }
  51. return result.trim();
  52. }
  53. }
Add Comment
Please, Sign In to add comment