Advertisement
teofarov13

Untitled

Oct 21st, 2023
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class FlightBookingSystem {
  2.     constructor(agencyName) {
  3.         this.agencyName = agencyName;
  4.         this.flights = [];
  5.         this.bookings = [];
  6.         this.bookingsCount = 0;
  7.     }
  8.  
  9.     addFlight(flightNumber, destination, departureTime, price) {
  10.         if (this.flights.some(f => f.flightNumber === flightNumber)) {
  11.             return `Flight ${flightNumber} to ${destination} is already available.`;
  12.         } else {
  13.             let flight = {
  14.                 flightNumber,
  15.                 destination,
  16.                 departureTime,
  17.                 price,
  18.                 bookings: 0
  19.             };
  20.             this.flights.push(flight);
  21.             return `Flight ${flightNumber} to ${destination} has been added to the system.`;
  22.         }
  23.     }
  24.  
  25.     bookFlight(passengerName, flightNumber) {
  26.         if (!this.flights.some(f => f.flightNumber === flightNumber)) {
  27.             return `Flight ${flightNumber} is not available for booking.`;
  28.         } else {
  29.             this.bookings.push({ flightNumber, passengerName });
  30.             this.bookingsCount++;
  31.             return `Booking for passenger ${passengerName} on flight ${flightNumber} is confirmed.`;
  32.         }
  33.     }
  34.  
  35.     cancelBooking(passengerName, flightNumber) {
  36.         const index = this.bookings.findIndex(b => b.passengerName === passengerName && b.flightNumber === flightNumber);
  37.         if (index === -1) {
  38.             throw new Error(`Booking for passenger ${passengerName} on flight ${flightNumber} not found.`);
  39.         }
  40.         this.bookings.splice(index, 1);
  41.         this.bookingsCount--;
  42.         return `Booking for passenger ${passengerName} on flight ${flightNumber} is cancelled.`;
  43.     }
  44.  
  45.  
  46.     showBookings(criteria) {
  47.         if (this.bookings.length === 0) {
  48.             throw new Error(`No bookings have been made yet.`);
  49.         }
  50.         if (criteria === "all") {
  51.             let result = `All bookings(${this.bookingsCount}):`;
  52.             for (let i = 0; i < this.bookings.length; i++) {
  53.                 const booking = this.bookings[i];
  54.                 result += `\n${booking.passengerName} booked for flight ${booking.flightNumber}.`;
  55.             }
  56.             return result;
  57.         } else if (criteria === "cheap") {
  58.             let cheapBookings = this.bookings.filter(b => this.flights[b.flightNumber].price <= 100);
  59.             if (cheapBookings.length === 0) {
  60.                 return "No cheap bookings found.";
  61.             }
  62.             let result = "Cheap bookings:";
  63.             for (let i = 0; i < cheapBookings.length; i++) {
  64.                 let booking = cheapBookings[i];
  65.                 result += `\n${booking.passengerName} booked for flight ${booking.flightNumber}.`;
  66.             }
  67.             return result;
  68.         } else if (criteria === "expensive") {
  69.             let expensiveBookings = this.bookings.filter(b => this.flights[b.flightNumber].price > 100);
  70.             if (expensiveBookings.length === 0) {
  71.                 return "No expensive bookings found.";
  72.             }
  73.             let result = "Expensive bookings:";
  74.             for (let booking of expensiveBookings) {
  75.                 result += `\n${booking.passengerName} booked for flight ${booking.flightNumber}.`;
  76.             }
  77.             return result;
  78.         } else {
  79.             throw new Error(`Invalid criteria: ${criteria}`);
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement