victoriaSD

02.Flight Booking System

Jun 16th, 2024
96
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.     const element = this.flights.find((x) => x.flightNumber === flightNumber);
  11.     price = Number(price);
  12.  
  13.     if (element) {
  14.       return `Flight ${flightNumber} to ${destination} is already available.`;
  15.     }
  16.  
  17.     this.flights.push({
  18.       flightNumber,
  19.       destination,
  20.       departureTime,
  21.       price,
  22.     });
  23.  
  24.     return `Flight ${flightNumber} to ${destination} has been added to the system.`;
  25.   }
  26.  
  27.   bookFlight(passengerName, flightNumber) {
  28.     const element = this.flights.find((x) => x.flightNumber === flightNumber);
  29.  
  30.     if (!element) {
  31.       return `Flight ${flightNumber} is not available for booking.`;
  32.     }
  33.  
  34.     this.bookings.push({
  35.       passengerName,
  36.       flightNumber,
  37.     });
  38.  
  39.     this.bookingsCount++;
  40.  
  41.     return `Booking for passenger ${passengerName} on flight ${flightNumber} is confirmed.`;
  42.   }
  43.  
  44.   cancelBooking(passengerName, flightNumber) {
  45.     const element = this.bookings.find(
  46.       (x) =>
  47.         x.flightNumber === flightNumber && x.passengerName === passengerName
  48.     );
  49.  
  50.     if (!element) {
  51.       throw new Error(
  52.         `Booking for passenger ${passengerName} on flight ${flightNumber} not found.`
  53.       );
  54.     }
  55.  
  56.     this.bookingsCount--;
  57.     const indexElement = this.bookings.findIndex(
  58.       (x) =>
  59.         x.flightNumber === flightNumber && x.passengerName === passengerName
  60.     );
  61.     this.bookings.splice(indexElement, 1)[0];
  62.  
  63.     return `Booking for passenger ${passengerName} on flight ${flightNumber} is cancelled.`;
  64.   }
  65.  
  66.   showBookings(criteria) {
  67.     if (this.bookings.length === 0) {
  68.       throw new Error(`No bookings have been made yet.`);
  69.     }
  70.  
  71.     if (criteria === "all") {
  72.  
  73.       let line = `All bookings(${this.bookingsCount}):\n`;
  74.  
  75.       this.bookings.forEach(
  76.         (x) =>
  77.           (line += `${x.passengerName} booked for flight ${x.flightNumber}.\n`)
  78.       );
  79.       return line;
  80.  
  81.     }
  82.     else if (criteria === "cheap") {
  83.  
  84.       let cheapBookings = this.bookings.filter((booking) => {
  85.         const flight = this.flights.find(
  86.           (x) => x.flightNumber === booking.flightNumber
  87.         );
  88.         return flight && flight.price <= 100;
  89.       });
  90.  
  91.       if (cheapBookings.length === 0) {
  92.         return `No cheap bookings found.`;
  93.       }
  94.  
  95.       let line = `Cheap bookings:`;
  96.       cheapBookings.forEach(
  97.         (x) =>
  98.           (line += `\n${x.passengerName} booked for flight ${x.flightNumber}.`)
  99.       );
  100.       return line;
  101.  
  102.     }
  103.     else if (criteria === "expensive") {
  104.        
  105.       let expensiveBookings = this.bookings.filter((booking) => {
  106.         const flight = this.flights.find(
  107.           (x) => x.flightNumber === booking.flightNumber
  108.         );
  109.         return flight && flight.price > 100;
  110.       });
  111.  
  112.       if (expensiveBookings.length === 0) {
  113.         return "No expensive bookings found.";
  114.       }
  115.  
  116.       let line = "Expensive bookings:";
  117.       expensiveBookings.forEach(
  118.         (x) =>
  119.           (line += `\n${x.passengerName} booked for flight ${x.flightNumber}.`)
  120.       );
  121.       return line;
  122.     }
  123.   }
  124. }
  125.  
  126. const system = new FlightBookingSystem("TravelWorld");
  127. console.log(system.addFlight("AA101", "Los Angeles", "09:00 AM", 250));
  128. console.log(system.addFlight("BB202", "New York", "10:30 AM", 180));
  129. console.log(system.bookFlight("Alice", "AA101"));
  130. console.log(system.bookFlight("Bob", "BB202"));
  131. console.log(system.showBookings("expensive"));
  132. console.log(system.showBookings("cheap"));
  133.  
Advertisement
Add Comment
Please, Sign In to add comment