Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Movie {
- movieName;
- ticketPrice;
- screenings;
- totalSoldTickets;
- totalProfit;
- constructor(movieName, ticketPrice) {
- this.movieName = movieName;
- this.ticketPrice = Number(ticketPrice);
- this.screenings = [];
- this.totalSoldTickets = 0;
- this.totalProfit = 0;
- }
- newScreening(date, hall, description) {
- if (this.screenings.some(s => s.date === date && s.hall === hall)) {
- throw new Error(`Sorry, ${hall} hall is not available on ${date}`)
- }
- this.screenings.push({date: date, hall: hall, description: description, profit: 0.00});
- return `New screening of ${this.movieName} is added.`;
- }
- endScreening(date, hall, soldTickets) {
- if (this.screenings.length === 0) {
- throw new Error(`Sorry, there is no such screening for ${this.movieName} movie.`)
- }
- let findScreen = this.screenings.find(e => (e.date === date && e.hall === hall));
- if (findScreen) {
- findScreen.profit += Number(soldTickets) * Number(this.ticketPrice);
- this.totalSoldTickets += soldTickets;
- this.screenings = this.screenings.filter(scr => scr.date !== date || scr.hall !== hall);
- return `${this.movieName} movie screening on ${date} in ${hall} hall has ended. Screening profit: ${findScreen.profit}`;
- }
- }
- toString() {
- let result = '';
- result += `${this.movieName} full information:\n`;
- result += `Total profit: ${this.totalSoldTickets * this.ticketPrice}$\n`
- result += `Sold Tickets: ${this.totalSoldTickets}\n`;
- if (this.screenings.length > 0) {
- result += 'Remaining film screenings:\n';
- let scrPrintSorted = this.screenings.sort((a, b) => a.hall.localeCompare(b.hall));
- for (let i = 0; i < scrPrintSorted.length; i++) {
- result += `${scrPrintSorted[i].hall} - ${scrPrintSorted[i].date} - ${scrPrintSorted[i].description}\n`;
- }
- } else {
- result += `No more screenings!`;
- }
- return result.trim();
- }
- }
Add Comment
Please, Sign In to add comment