svephoto

Halls [JavaScript]

Dec 26th, 2020 (edited)
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solveClasses() {
  2.     class Hall {
  3.         constructor(capacity, name) {
  4.             this.capacity = Number(capacity);
  5.             this.name = name;
  6.             this.events = [];
  7.             this.allPerformers = [];
  8.         }
  9.  
  10.         hallEvent(title) {
  11.             if (this.events.includes(title)) {
  12.                 throw new Error("This event is already added!");
  13.             }
  14.  
  15.             this.events.push(title);
  16.  
  17.             return "Event is added.";
  18.         }
  19.  
  20.         close() {
  21.             this.events = [];
  22.  
  23.             return `${this.name} hall is closed.`;
  24.         }
  25.  
  26.         toString() {
  27.             let result = `${this.name} hall - ${this.capacity}`;
  28.  
  29.             if (this.events.length > 0) {
  30.                 result += '\n';
  31.                 result += `Events: ${this.events.join(', ')}`;
  32.             }
  33.  
  34.             return result;
  35.         }
  36.     }
  37.  
  38.     class MovieTheater extends Hall {
  39.         constructor(capacity, name, screenSize) {
  40.             super(capacity, name);
  41.  
  42.             this.screenSize = screenSize;
  43.         }
  44.  
  45.         close() {
  46.             return super.close() + "Аll screenings are over.";
  47.         }
  48.  
  49.         toString() {
  50.             let result = super.toString();
  51.  
  52.             result += '\n';
  53.             result += `${this.name} is a movie theater with ${this.screenSize} screensize and ${this.capacity} seats capacity.`;
  54.  
  55.             return result;
  56.         }
  57.     }
  58.    
  59.     class ConcertHall extends Hall {
  60.         constructor(capacity, name) {
  61.             super(capacity, name);
  62.  
  63.             this.events = [];
  64.         }
  65.  
  66.         hallEvent(title, performers) {
  67.             if (this.events.includes(title)) {
  68.                 throw new Error("This event is already added!");
  69.             } else {
  70.                 super.hallEvent(title);
  71.  
  72.                 this.allPerformers.push(performers);
  73.  
  74.                 return "Event is added.";
  75.             }
  76.         }
  77.  
  78.         close() {
  79.             return super.close() + "Аll performances are over.";
  80.         }
  81.  
  82.         toString() {
  83.             let result = super.toString();
  84.            
  85.             if (this.events.length > 0) {
  86.                 result += '\n';
  87.                 result += `Performers: ${this.allPerformers.join(', ')}.`;
  88.             }
  89.  
  90.             return result;
  91.         }
  92.     }
  93.  
  94.     return {
  95.         Hall,
  96.         MovieTheater,
  97.         ConcertHall
  98.     }
  99. }
  100.  
  101. let classes = solveClasses();
  102. let hall = new classes.Hall(20, 'Main');
  103. console.log(hall.hallEvent('Breakfast Ideas'));
  104. console.log(hall.hallEvent('Annual Charity Ball'));
  105. console.log(hall.toString());
  106. console.log(hall.close()); 
  107.  
  108. let movieHall = new classes.MovieTheater(10, 'Europe', '10m');
  109. console.log(movieHall.hallEvent('Top Gun: Maverick')); 
  110. console.log(movieHall.toString());
  111.  
  112. let concert = new classes.ConcertHall(5000, 'Diamond');        
  113. console.log(concert.hallEvent('The Chromatica Ball', ['LADY GAGA']));  
  114. console.log(concert.toString());
  115. console.log(concert.close());
  116. console.log(concert.toString());
  117.  
  118. // Event is added.
  119. // Event is added.
  120. // Main hall - 20
  121. // Events: Breakfast Ideas, Annual Charity Ball
  122. // Main hall is closed.
  123.  
  124. // Event is added.
  125. // Europe hall - 10
  126. // Events: Top Gun: Maverick
  127. // Europe is a movie theater with 10m screensize and 10 seats capacity.
  128.  
  129. // Event is added.
  130. // Diamond hall - 5000
  131. // Events: The Chromatica Ball
  132. // Performers: LADY GAGA.
  133. // Diamond hall is closed.Аll performances are over.
  134. // Diamond hall - 5000
  135.  
Add Comment
Please, Sign In to add comment