Air Plane Reservation System Problem

Air Plane Reservation System Problem in C++



Problem:
A small airline company (Virgin Airline) needs a program to carry out seat reservation for its flights. You are to write a demonstration seat reservation program for this company.
Description of Class and its functionality
The program is to be menu driven with the following options:

1 Display available flights
2 View flight
3 Seat availability
4 Seat booking
5 Exit

Option One (1)

The program should list all flight numbers that have at least one seat available. The screen display should show the flight number, departure date and time, departure airport code, arrival airport code and the number of seats available with their class.

Option two (2)

The program should prompt the user for a flight number. The program should then search the database for the requested flight. An appropriate error message should appear if the flight is
not present. If the flight is found the display should show all available seat numbers together with their class, the total number of available seats in each class, the departure and arrival airports, the departure date and time.

Option three (3)

The program should prompt the user for a flight number. It should then request the number of seats required by the customer. The program will then need to search the flight to see if that number of seats is available. If they are then a message giving the seat numbers available should appear on screen. If not an appropriate error message should be given.

Option four (4)

The program should prompt for a flight number and seat location in order to make a booking. Seats are numbered by rows 1..60 and A..F in each row. If the seat is available it should be allocated and therefore should not show as available any more. If a non-available seat is requested an error message should appear.

Option four (5)

Upon exit the modified data should be written to the same file (called flights.dat) in the same format as originally.

Data Structures

You are to use an object-oriented approach to this problem. There should be a flight class where all flights being contained in an array of records (structs) in the flight class. Within each flight there should be an array of char representing the seats.
A file of data called flights.txt will be provided which your program must read in to begin. The data will be the flight numbers, departure dates/times, airports and seating. Your program will be tested on a similar file of data. DO NOT create your own file of a different format. Use the format supplied in this file.

The format of the file will be:

XXXXX
XX/XX/XXXX XX:XX
XXXXXXXXXXXX
XXXXXXXXXXXX
XXXXXXXXX

blank line separates flights

Sample File:

VA301
20/02/2020 10:20
COLOMBO
SINGAPORE
10 E AB
15 E CDE
22 E ADF
31 E BCF
35 E ABCD
45 E AB
50 E DEF

VA532
25/02/2020 12:25
COLOMBO
NEW DELHI
5 B AE
9 B CDE
11 E AF
16 E ABC
23 E DE
29 E DEF
31 E ACE

VA391
30/02/2020 21:30
KUALA LUMPUR
COLOMBO
8 B B
13 E CD
19 E CDE
22 E CDE
27 E BCD
33 E DE
40 E DF

VA333
10/02/2020 21:20
COLOMBO
BANGKOK
5 B C
12 E CD
17 E ABC
25 E ACE
33 E CDF
39 E BCDE
48 E AB

VA205
15/02/2020 12:55
SINGAPORE
COLOMBO
20 E AB
22 E CDE
27 E ADF
34 E ABC
37 E CDEF
42 E EF
58 E CDE

Sample C++ Code:

#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include <bits/stdc++.h> 

using namespace std;

struct flight_details{//struct
    string row_number;
string seat_class;
string available_seats;
};

class flights{
public:
string flight_number;
string date_time;
string departure;
string arrival;
struct flight_details array[100];//making array of struct 
void display_available_flights(flights all_flights[],int number_of_flights,int number_of_seat_lines);//member funtions
        void view_flight(flights all_flights[],int number_of_flights,int number_of_seat_lines);
        void seat_availability(flights all_flights[],int number_of_flights,int number_of_seat_lines);
        void seat_booking(flights all_flights[],int number_of_flights,int number_of_seat_lines);
        void seat_booking_one_seat(flights all_flights[],int number_of_flights,int number_of_seat_lines);
        void seat_booking_more_than_one_seat(flights all_flights[],int number_of_flights,int number_of_seat_lines);
        void input_data_to_flights(flights all_flights[],int number_of_flights,int number_of_seat_lines);
        void exit_function(flights all_flights,int number_of_flights,int number_of_seat_lines);
        
};

void flights::display_available_flights(flights all_flights[],int number_of_flights,int number_of_seat_lines){//display available flights function
flight_details f;
int i;
    for(i=0; i<number_of_flights; i++){
cout<< "\n";
cout<< "Flight Number : " << all_flights[i].flight_number << endl;//print details of flights
cout<< "Departure Time and Date : " << all_flights[i].date_time << endl;
cout<< "Departure Airport : " << all_flights[i].departure << endl;
cout<< "Arrival Airport : " << all_flights[i].arrival << endl;
cout<< "\n";

}

}
void flights::view_flight(flights all_flights[],int number_of_flights,int number_of_seat_lines){//view flights function
    string flight_no;
int i,j;
cout<<"Enter The Flight Number"<<endl;//get flights number
cin>>flight_no;

for(i=0;i<number_of_flights;i++){
if(flight_no==all_flights[i].flight_number){//condition checking
cout<< "\n";
cout<< "Flight Number : " << all_flights[i].flight_number << endl;//printing details
cout<< "Departure Time and Date : " << all_flights[i].date_time << endl;
cout<< "Departure Airport : " << all_flights[i].departure << endl;
cout<< "Arrival Airport : " << all_flights[i].arrival << endl;
for(j=0;j<number_of_seat_lines;j++){
cout<< "Row Number : " <<all_flights[i].array[j].row_number << endl;//printing details of seats
cout<< "Seat Class : " <<all_flights[i].array[j].seat_class << endl;
cout<< "Available Seats : " <<all_flights[i].array[j].available_seats << endl;
cout<< "\n";
}

}
    }

}

void flights::seat_availability(flights all_flights[],int number_of_flights,int number_of_seat_lines){//flights avaibility function
string flight_no; 
int number_of_seats;
int i,j;
    cout<<"Enter The Flight Number : "<<endl;//get flight no
cin>>flight_no;
cout<<"Enter The Number of Seats You Want : "<<endl;//get needed number of seats
cin>>number_of_seats;
for(i=0;i<number_of_flights;i++){
if(flight_no==all_flights[i].flight_number){//condition checking
for(j=0;j<number_of_seat_lines;j++){
string str (all_flights[i].array[j].available_seats);
if(number_of_seats<=str.length()){//get the length of the string
cout<< "Row Number : " <<all_flights[i].array[j].row_number << endl;//printing details
cout<< "Seat Class : " <<all_flights[i].array[j].seat_class << endl;
cout<< "Available Seats : " <<all_flights[i].array[j].available_seats << endl;
cout<< "\n";
}
}
}

}
}



void flights::seat_booking(flights all_flights[],int number_of_flights,int number_of_seat_lines){//seat booking function
    int ch;
    cout<<"Do You Want to Book One or More Than One Seats?"<<endl;
    cout<<"*) If One, Enter Number One (1) : "<<endl;
    cout<<"*) If More Than One, Enter Number Two (2) : "<<endl;
    cin>>ch;
    switch (ch){
    case 1: seat_booking_one_seat(all_flights,number_of_flights,number_of_seat_lines);//call book one seat function
    break;
   
    case 2: seat_booking_more_than_one_seat(all_flights,number_of_flights,number_of_seat_lines);//call book more than one seat function
    break;
   
    default: cout<<"Wrong Choice"<<endl;
    break;
}


}
void flights::seat_booking_one_seat(flights all_flights[],int number_of_flights,int number_of_seat_lines){//book one seat function
string flight_no; 
string seat_location_row;
char seat_location_name;
int i,j;

    cout<<"Enter The Flight Number : "<<endl;
cin>>flight_no;
cout<<"Enter The Location of Your Seat(Row & Seat Name) : "<<endl;//get the location of seat
cin>>seat_location_row>>seat_location_name;
for(i=0;i<number_of_flights;i++){
if(flight_no==all_flights[i].flight_number){//condition checking
for(j=0;j<number_of_seat_lines;j++){
string s (all_flights[i].array[j].available_seats);// assigning value to string s 
int n=s.length();
char char_array[n+1];// declaring character array
strcpy(char_array, s.c_str());// copying the contents of the string to char array 
for(int k=0;k<n;k++){
if((seat_location_row==all_flights[i].array[j].row_number)&&(seat_location_name==char_array[k])){//condition checking

cout<<"Your Seat is in Row : "<<all_flights[i].array[j].row_number<<endl;//printing details
cout<<"Your Seat Class is : "<<all_flights[i].array[j].seat_class<<endl;
cout<<"Your Seat Name is : "<<char_array[k]<<endl;
cout<< "\n";
cout<<"Reservation is Successful"<<endl;
cout<< "\n";
}
}

}

}

}
}
void flights::seat_booking_more_than_one_seat(flights all_flights[],int number_of_flights,int number_of_seat_lines){//book more than one seat function
string flight_no; 
string seat_location_row;
string seat_location_name;
int i,j;
    cout<<"Enter The Flight Number : "<<endl;
cin>>flight_no;
cout<<"Enter The Location of Your Seat(Row & Seat Name) : "<<endl;//get the location of seat
cin>>seat_location_row>>seat_location_name;
for(i=0;i<number_of_flights;i++){
if(flight_no==all_flights[i].flight_number){//condition checking
for(j=0;j<number_of_seat_lines;j++){

if((seat_location_row==all_flights[i].array[j].row_number)&&(seat_location_name==all_flights[i].array[j].available_seats)){//condition checking
    string str (all_flights[i].array[j].available_seats);
    int n=str.length();
str.erase (n);//delete the element from file             
                    
cout<<"Your Seat is in Row : "<<all_flights[i].array[j].row_number<<endl;//printing details
cout<<"Your Seat Class is : "<<all_flights[i].array[j].seat_class<<endl;
cout<<"Your Seat Name is : "<<all_flights[i].array[j].available_seats<<endl;
cout<< "\n";
cout<<"Reservation is Successful"<<endl;
cout<< "\n";

}

}

}

}
}
void input_data_to_flights(flights all_flights[],int number_of_flights,int number_of_seat_lines){
string line;//initialize a string named line
char file_details[100];//cerate a char array

ifstream myfile("flights.txt");//open the flights file
if(myfile.is_open()){
for(int i=0 ; i<number_of_flights ; i++){
    for(int j=0; j<4; j++){
getline(myfile , line);//read line by line
switch(j){
case 0:
all_flights[i].flight_number = line;//read first line of the file
break;
case 1:
all_flights[i].date_time = line;//read second line of the file
break;
case 2:
all_flights[i].departure = line;//read third line of the file
break;
case 3:
all_flights[i].arrival = line;//read fourth line of the file
break;
}
}
int count = 0;
while(getline(myfile , line)){
if(line.size() == 0){
break;
}
    stringstream ss(line);//convert string in to struct char array

ss>>file_details;
all_flights[i].array[count].row_number = file_details;//convert row numbers


ss>>file_details;
all_flights[i].array[count].seat_class = file_details;//convert seat class


ss>>file_details;
all_flights[i].array[count].available_seats = file_details;//convert available seats

count++;

    }
    }
}
    else cout << "Unable to open file"; //if the file is not open output

    myfile.close(); //closing the file
}
int find_number_of_flights(){//find number of flights function
string line;
int num_of_flights=0;
ifstream myfile;
myfile.open("flights.txt");

while(getline(myfile,line)){
if(line.size()==0){
num_of_flights++;
}
}

myfile.close();
return num_of_flights;
}
int get_number_of_lines(){//get number of seat lines function
int number_of_lines;
string line;
    ifstream myfile("flights.txt");

    if(myfile.is_open()){
   
  while(getline(myfile,line)){

if(line.size()==0){
break;
}
else{
number_of_lines++;
}
}
    }

myfile.close();
return number_of_lines-4;

}
void exit_function(flights all_flights[],int number_of_flights,int number_of_seat_lines){//write data to the file and exit function

ofstream myfile;
myfile.open("flights.txt");

int i;
for(i=0;i<number_of_flights;i++){
myfile<<all_flights[i].flight_number<<endl;//write data to the file
myfile<<all_flights[i].date_time << endl;
myfile<<all_flights[i].departure << endl;
myfile<<all_flights[i].arrival << endl;
int j;
for(j=0;j<number_of_seat_lines;j++){
myfile<<all_flights[i].array[j].row_number<<" ";
myfile<<all_flights[i].array[j].seat_class<<" ";
myfile<<all_flights[i].array[j].available_seats<<endl;
}
myfile<<endl;

}
myfile.close();
exit(1);

}
int main(){
int number_of_flights=find_number_of_flights();//call find number of flights function
flights all_flights[number_of_flights+1];
int number_of_seat_lines=get_number_of_lines();//call get number of seat lines function

input_data_to_flights(all_flights,number_of_flights,number_of_seat_lines);//call the input data to flights function and parameter is all flights array

int ch;
while(1){
cout<<"***WELCOME TO THE VIRGIN AIRLINE***"<<endl;//getting user input in begining
cout<<"\n"<<endl;
cout<<"1) Display Available Flights"<<endl;//printing details
cout<<"2) View Flight"<<endl;
cout<<"3) Seat Availability"<<endl;
cout<<"4) Seat Booking"<<endl;
cout<<"5) Exit"<<endl;
cout<<"\n"<<endl;
cout<<"Please Enter Your Choice : "<<endl;
cin>>ch;
cout<<"\n"<<endl;

flights obj;

switch(ch){
case 1: obj.display_available_flights(all_flights,number_of_flights,number_of_seat_lines);//call the display flights function and parameter is all flights array
break;

case 2: obj.view_flight(all_flights,number_of_flights,number_of_seat_lines);//call the view flights function and parameter is all flights array
break;

case 3: obj.seat_availability(all_flights,number_of_flights,number_of_seat_lines);//call the seat avaibility function and parameter is all flights array
break;

case 4: obj.seat_booking(all_flights,number_of_flights,number_of_seat_lines);//call the seat booking function and parameter is all flights array
break;

case 5: exit_function(all_flights,number_of_flights,number_of_seat_lines);//exit from the switch case
break;

default: cout<<"Invalid Choice"<<endl;
break;
}
    }

return 0;

}

Comments

Popular posts from this blog

Programming Using GNU Octave

Library Problem

What Is A Gamma Ray Burst?