Problem 20

Question

( Airline Reservations System) A small airline has just purchased a computer for its new automated reservations system. You have been asked to program the new system. You are to write a program to assign seats on each flight of the airline's only plane (capacity: 10 seats). Your program should display the following menu of alternativesplease type 1 for "First Class" and Please type 2 for "Econony". If the person types 1 , your program should assign a seat in the first class section (seats \(1-5\) ). If the person types \(2,\) your program should assign a seat in the economy section (seats \(6-10\) ). Your program should print a boarding pass indicating the person's seat number and whether it is in the first class or economy section of the plane. Use a one-dimensional array to represent the seating chart of the plane. Initialize all the elements of the array to 0 to indicate that all seats are empty. As each seat is assigned, set the corresponding elements of the array to 1 to indicate that the seat is no longer available. Your program should, of course, never assign a seat that has already been assigned. When the first class section is full, your program should ask the person if it is acceptable to be placed in the economy section (and vice versa). If yes, then make the appropriate seat assignment. If no, then print the message -Next flight leaves in 3 hours".

Step-by-Step Solution

Verified
Answer
Initialize seating array, handle user input, and iteratively assign the first available seat based on the class choice.
1Step 1: Initialize Seating Array
Create an array `seats` of size 10, initialized to 0. This indicates all seats are empty: `seats = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]`.
2Step 2: Display Menu Options
Display the menu options for the user: 'Please type 1 for First Class and Please type 2 for Economy.' This allows the user to choose the section in which they wish to be seated.
3Step 3: Process User Input
Accept the choice from the user. If the user selects 1, try assigning a seat in the first class (indices 0-4). If the user selects 2, attempt to assign a seat in the economy class (indices 5-9).
4Step 4: Assign First Class Seat
Check seats 0 through 4 in the array for an empty seat. Assign the first available seat (value 0) by setting it to 1, and print a boarding pass with the seat number. If no seat is available, proceed to Step 5.
5Step 5: Handle Full First Class
If all first class seats are full, prompt the user with 'First Class is full. Would you like a seat in Economy? (y/n)'. Based on the response, either assign an economy seat or display 'Next flight leaves in 3 hours' if the response is 'n'.
6Step 6: Assign Economy Seat
For a request in economy, check seats 5 through 9 for availability. Assign the first open seat by setting its value to 1, and print a boarding pass with the seat number. If no seat is available, proceed to Step 7.
7Step 7: Handle Full Economy
If all economy seats are occupied, ask 'Economy is full. Would you like a seat in First Class? (y/n)'. If the user agrees ('y'), assign a first class seat if available. Otherwise, show the message 'Next flight leaves in 3 hours'.

Key Concepts

Program DesignArray ManipulationFlight Booking LogicSeating Allocation
Program Design
Designing a program for an airline reservation system involves creating a structured plan that details how the reservation process will work. This exercise revolves around an airline with a single flight of 10 seats. By breaking down the program needs into sections, you ensure all scenarios are handled neatly.

The program must first initialize necessary data, such as a seating chart, and then present the options to the user. Introducing a simple menu with choices for seating class allows users to select their preference easily. The program should also include error handling, such as what happens when seats in a section are fully booked and how to communicate this to the users.

Flowcharts or pseudocode can be handy tools during this phase. Think of them as visual guides that show the steps your program needs to take. A user-friendly design keeps the booking process straightforward and minimizes confusion or errors during operation.
Array Manipulation
In the context of this airline reservation system, array manipulation is crucial for tracking which seats are available and which are taken. An array is a data structure that stores data in a list format. Here, we use an array to represent the seating chart of the airplane. Each index in the array corresponds to a specific seat.

You start by initializing an array called `seats` with 10 elements, all set to 0. The value 0 indicates an empty seat. As seats are booked, the corresponding index is set to 1, denoting that the seat is now occupied.

In programming, you must loop through the array to find available seats when a customer selects a seating class. It's vital to ensure you only offer seats that are marked as available (i.e., 0). By manipulating the array correctly, the system can offer accurate seating choices and maintain a real-time seating chart.
Flight Booking Logic
The flight booking logic serves as the backbone of the reservation system, ensuring that all bookings are processed accurately and efficiently. When users choose a class, the program must determine the first available seat within that class. This requires checks based on the assigned indices:
  • First Class: indices 0 to 4
  • Economy Class: indices 5 to 9

The program first attempts to find a seat in the user's selected class. If all such seats are filled, the program prompts the user to accept an alternative class, if available. This decision-making process needs careful logic to avoid double booking and to handle cases where neither class has available seats.

Another critical aspect is maintaining user satisfaction. If you cannot meet the user's request, the program should gracefully inform them of the next steps, like suggesting another flight. Handling such exceptions is part of ensuring a smooth booking experience.
Seating Allocation
Seating allocation in this system is managed by checking through each section to find the first available seat when a customer's request comes in. When a client indicates their preferred class, the program iterates over the specific array section to see if any spots marked "0" (unoccupied) exist.

If a seat is found, the booking system updates the array to mark this seat as taken by switching the index value from 0 to 1. It then prints a boarding pass for the customer with their assigned seat number and class. If the desired section is full, the system offers an alternative within the available seats of the other class, implementing a fallback mechanism to maximize seat occupation.

This system of allocation ensures that all seating decisions are made efficiently and fairly, preventing inefficiencies such as empty seats flying free on fully booked flights. Proper allocation optimizes aircraft usage and enhances customer satisfaction by providing as many seat options as possible.