Print Friendly and PDF
(Airline Reservations System) A small airline has just purchased a computer for its new automated reservations system. You’ve 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).
(Airline Reservations System) A small airline has just purchased a computer for its new automated reservations system. You’ve 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 alternatives—Please type 1 for "First Class" and Please type 2 for "Economy". 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’s 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 false to indicate that all seats are empty. As each seat is assigned, set the corresponding elements of the array to true 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’s 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."


Solution:

 
#include <iostream>
using namespace std;
 
int option_select();
bool availability_checking(int);
void reserve_boarding(int);
void next_flight();
 
int capacity[10]  = {0};
 
int main()
{
   int option;
   bool availability = false;
   bool availability_other = false;
 
for(int x = 0; x < 12; x++)
{
   // option select
   option = option_select();
 
   // check seats availibility in selected section
   availability = availability_checking(option);
 
   if(availability == 1)
   {
      // if there is available place print boarding pass
      reserve_boarding(option);
   }
   else
   // if there is no available place, check seats availability in other section
   {
      if(option == 1)
         availability_other = availability_checking(2);
      else
         availability_other = availability_checking(1);
   }
 
   // ask client is it acceptable to place in other section
   if(availability == 0 && availability_other == 1)
   {
      cout << "There is available place in other section "
         << "\nDo you want to buy it? ([Y]es, [N]o)" << endl;
 
      char other_section;
 
      cin >> other_section;
 
      while(other_section != 'y' && other_section != 'n')
      {
         cout << "Please enter correct answer option" << endl;
         cin >> other_section;
      }
      if(other_section == 'n')
         next_flight();
      else
         if(option == 1)
            reserve_boarding(2);
         else
            reserve_boarding(1);
 
 
   }
   // if there is no available place in other section display message
   else if(availability == 0 && availability_other == 0)
      next_flight();
 
 
   }
}
 
// display message and option select
int option_select()
{
   int class_option;
 
   cout << "Please type 1 for \"First Class\""
      << "\nPlease type 2 for \"Economy\"" << endl;
 
   cin >> class_option;
 
   while(class_option != 1 && class_option != 2)
   {
      cout << "Please enter correct option!" << endl;
      cout << "Please type 1 for \"First Class\""
      << "\nPlease type 2 for \"Economy\"" << endl;
      cin >> class_option;
   }
 
   return class_option;
}
 
 
 
// seats availability
bool availability_checking(int opt)
{
   int counter_a = 0;
   int counter_b = 0;
 
   if(opt == 1)
   {
      for(int i = 0; i < 5; i++)
      {
         if(capacity[i] == 0)
            counter_a++;
      }
   }
   else
   {
      for(int j = 5; j < 10; j++)
      {
         if(capacity[j] == 0)
            counter_b++;
      }
   }
   if((opt == 1 && counter_a > 0) || (opt == 2 && counter_b > 0))
   {
      cout << "There is available place in section " << endl;
      return 1;
   }
   else
   {
      cout << "There is no available place in section " << endl;
      return 0;
   }
}
 
 
// reserve seat and print boarding pass
void reserve_boarding(int section)
{
   int seat = 0;
   if(section == 1)
   {
      for(int i = 0; i < 5; i++)
      {
         if(capacity[i] == 0)
         {
            capacity[i] = 1;
            seat = i + 1;
         }
         if(seat != 0)
            break;
      }
      if(seat != 0)
      {
         cout << "**** 1. Boarding Pass ****\nYour section:\t" << section
            << "\nYour seat:\t" << seat << endl;
      }
   }
   else
   {
      for(int j = 5; j < 10; j++)
      {
         if(capacity[j] == 0)
         {
            capacity[j] = 1;
            seat = j + 1;
         }
         if(seat != 0)
            break;
      }
      if(seat != 0)
      {
         cout << "**** 2. Boarding Pass ****\nYour section:\t" << section
            << "\nYour seat:\t" << seat << endl;
      }
   }
}
 
// display message about next flight
void next_flight(void)
{
   cout << "Next flight leaves in 3 hours." << endl;
}
 
 
 
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

1 comments: