Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FlightBookingSystem {
- constructor(agencyName) {
- this.agencyName = agencyName;
- this.flights = [];
- this.bookings = [];
- this.bookingsCount = 0;
- }
- addFlight(flightNumber, destination, departureTime, price) {
- const element = this.flights.find((x) => x.flightNumber === flightNumber);
- price = Number(price);
- if (element) {
- return `Flight ${flightNumber} to ${destination} is already available.`;
- }
- this.flights.push({
- flightNumber,
- destination,
- departureTime,
- price,
- });
- return `Flight ${flightNumber} to ${destination} has been added to the system.`;
- }
- bookFlight(passengerName, flightNumber) {
- const element = this.flights.find((x) => x.flightNumber === flightNumber);
- if (!element) {
- return `Flight ${flightNumber} is not available for booking.`;
- }
- this.bookings.push({
- passengerName,
- flightNumber,
- });
- this.bookingsCount++;
- return `Booking for passenger ${passengerName} on flight ${flightNumber} is confirmed.`;
- }
- cancelBooking(passengerName, flightNumber) {
- const element = this.bookings.find(
- (x) =>
- x.flightNumber === flightNumber && x.passengerName === passengerName
- );
- if (!element) {
- throw new Error(
- `Booking for passenger ${passengerName} on flight ${flightNumber} not found.`
- );
- }
- this.bookingsCount--;
- const indexElement = this.bookings.findIndex(
- (x) =>
- x.flightNumber === flightNumber && x.passengerName === passengerName
- );
- this.bookings.splice(indexElement, 1)[0];
- return `Booking for passenger ${passengerName} on flight ${flightNumber} is cancelled.`;
- }
- showBookings(criteria) {
- if (this.bookings.length === 0) {
- throw new Error(`No bookings have been made yet.`);
- }
- if (criteria === "all") {
- let line = `All bookings(${this.bookingsCount}):\n`;
- this.bookings.forEach(
- (x) =>
- (line += `${x.passengerName} booked for flight ${x.flightNumber}.\n`)
- );
- return line;
- }
- else if (criteria === "cheap") {
- let cheapBookings = this.bookings.filter((booking) => {
- const flight = this.flights.find(
- (x) => x.flightNumber === booking.flightNumber
- );
- return flight && flight.price <= 100;
- });
- if (cheapBookings.length === 0) {
- return `No cheap bookings found.`;
- }
- let line = `Cheap bookings:`;
- cheapBookings.forEach(
- (x) =>
- (line += `\n${x.passengerName} booked for flight ${x.flightNumber}.`)
- );
- return line;
- }
- else if (criteria === "expensive") {
- let expensiveBookings = this.bookings.filter((booking) => {
- const flight = this.flights.find(
- (x) => x.flightNumber === booking.flightNumber
- );
- return flight && flight.price > 100;
- });
- if (expensiveBookings.length === 0) {
- return "No expensive bookings found.";
- }
- let line = "Expensive bookings:";
- expensiveBookings.forEach(
- (x) =>
- (line += `\n${x.passengerName} booked for flight ${x.flightNumber}.`)
- );
- return line;
- }
- }
- }
- const system = new FlightBookingSystem("TravelWorld");
- console.log(system.addFlight("AA101", "Los Angeles", "09:00 AM", 250));
- console.log(system.addFlight("BB202", "New York", "10:30 AM", 180));
- console.log(system.bookFlight("Alice", "AA101"));
- console.log(system.bookFlight("Bob", "BB202"));
- console.log(system.showBookings("expensive"));
- console.log(system.showBookings("cheap"));
Advertisement
Add Comment
Please, Sign In to add comment