35.7k views
5 votes
C create a class called Plane, to implement the functionality of the Airline Reservation System. Write an application that uses the Plane class and test its functionality. Write a method called Check In() as part of the Plane class to handle the check in process Prompts the user to enter 1 to select First Class Seat (Choice: 1) Prompts the user to enter 2 to select Economy Seat (Choice: 2) Assume there are only 5-seats for each First Class and Economy When all the seats are taken, display no more seats available for you selection Otherwise it displays the seat that was selected. Repeat until seats are filled in both sections Selections can be made from each class at any time.

User Terenoth
by
4.9k points

1 Answer

3 votes

Answer:

Here is the C++ program:

#include <iostream> //for using input output functions

using namespace std; //to identify objects like cin cout

class Plane{ //class Plane

private: // declare private data members i.e. first_class and eco_class

int first_class; //variable for first class

int eco_class; //variable declared for economy class

public: // public access modifier

Plane(){ //constructor to initialize values of first_class and eco_class

first_class=0; //initialized to 0

eco_class=0;} //initialized to 0

int getFirst(){ // class method to get data member first_class

return first_class;} //returns the no of reserved first class seats

int getEco(){ // class method to get data member eco_class

return eco_class;} //returns the no of reserved eco class seats

void CheckIn(){ //method to handle the check in process

int choice; //choice between first and economy class

cout<<"\\Enter 1 to select First Class Seat: "<<endl; //prompts user to enter 1 to reserve first class seat

cout<<"\\Enter 2 to select Economy Class Seat: "<<endl; //prompts user to enter 2 to reserve economy class seat

cin>>choice; //reads the choice from user

switch(choice){ // switch statement is used to handle the check in process

case 1: //to handle the first class seat reservation

if(getFirst()<5){ //if the seat is available and the seat limit has not exceed 5

first_class++; //add 1 to the first_class seat to count that a seat is reserved

cout<<"You reserved First class seat! ";} //display the message about successful seat reservation in first class

cout<<"\\Number of first class seats reserved: "<<getFirst()<<endl;} //displays the number of seats already reserved

else{ //if all first class seats are reserved then display the following message

cout<<"No more seats available for you selection!"<<endl;

if(getEco()>=5 && getFirst()>=5){ //if all seats from first class and economy class are reserved display the following message

cout<<"All seats are reserved!"<<endl;

exit(1);}} //program exits

break;

case 2: //to handle the economy seat reservation

if(getEco()<5){ //if the seat is available and the seat limit has not exceed 5

eco_class++; //add 1 to the eco_class seat to count that a seat is reserved

cout<<"You reserved Economy class seat! "; //display the message about successful seat reservation in economy class

cout<<"\\Number of Economy class seats reserved: "<<getEco()<<endl;} //displays the number of seats already reserved

else{ //if all economy class seats are reserved then display the following message

cout<<"No more seats available for you selection!"<<endl;

if(getEco()>=5 && getFirst()>=5){ //if all seats from first class and economy class are reserved display the following message

cout<<"All seats are reserved!"<<endl;

exit(1);}} //program exits

break;

default: cout<<"Enter a valid choice"<<endl; } } }; //if user enters anything other that 1 or 2 for choice then this message is displayed

int main(){ //start of main() function body

int select; // choice from first or economy class

Plane plane; //create an object of Plane class

cout<<"**************************"<<endl;

cout<<"Airline Reservation System"<<endl; //display this welcome message

cout<<"**************************"<<endl;

while(true){ // while loop executes the check in procedure

plane.CheckIn();} } //class CheckIn method of Plane classs using plane object to start with the check in process

Step-by-step explanation:

The program is elaborated in the comments with each statement of the above code. The program has a class Plane that has a method CheckIn to handle the check in process. The user is prompted to enter a choice i.e. enter 1 to select First Class Seat and enter 2 to select Economy Seat. A switch statement is used to handle this process. If user enters 1 then the case 1 of switch statement executes which reserves a seat for the user in first class. If user enters 2 then the case 2 of switch statement executes which reserves a seat for the user in economy class.There are only 5 seats for each First Class and Economy. So when the limit of the seats reservation exceeds 5 then the no more seats available for you selection is displayed. If all the seats are taken in both the first and economy class then it displays the message All seats are reserved. If the user enters anything other than 1 or 2 then display the message Enter a valid choice. Whenever the user reserves one of first or economy class seats the relevant seat is incremented to 1 in order to count the number of seats being reserved. The program and its output are attached.