/* C++ Programme for check the availability of flights and seats and book the seats
   Yogesh Yadav------IIT Kharagpur*/
                                     
#include <bits/stdc++.h>
using namespace std;

static int p = 0;
class a                                                             /*Calling the functions and defining variables*/
    {
            char flight_no[10], captain_name[30], arrival_time[30], departure_time[30], from[30], to[30], seat[30][6][30];
        public:
            void install();
            void allotment();
            void empty();
            void show();
            void avail();
            void position(int i);
    }flight[6];

void vline(char ch)                                                 /*Function for partition line*/
    {
        for (int i=135;i>0;i--)
            {
                cout<<ch;
            }
    }

void a::install()                                                   /*Function for installation of flights*/
    {
        cout<<"Enter Flight no: ";
        cin>>flight[p].flight_no;
        cout<<"\nEnter Captain's name: ";
        cin>>flight[p].captain_name;
        cout<<"\nArrival time: ";
        cin>>flight[p].arrival_time;
        cout<<"\nDeparture time: ";
        cin>>flight[p].departure_time;
        cout<<"\nFrom: \t\t\t";
        cin>>flight[p].from;
        cout<<"\nTo: \t\t\t";
        cin>>flight[p].to;
        flight[p].empty();
        p++;
    }

void a::allotment()                                                 /*Function for allotment of seats*/                            
    {
        int seat;
        char number[5];
        top:
        cout<<"Flight no: ";
        cin>>number;

        int n;

        for(n=0;n<=p;n++)
            {
                if(strcmp(flight[n].flight_no, number)==0)
                break;
            }

        while(n<=p)
            {
                cout<<"\nSeat Number: ";
                cin>>seat;

                if(seat>180)
                    {
                        cout<<"\nThere are only 180 seats available in this flight.";
                    }

                else
                {
                    if (strcmp(flight[n].seat[seat/6][(seat%6)-1], "Empty")==0)
                        {
                            cout<<"Enter Passanger's name: ";
                            cin>>flight[n].seat[seat/6][(seat%6)-1];
                            break;
                        }
                    else
                        cout<<"The Seat No. is already reserved.\n";
                }
            }

        if(n>p)
            {
                cout<<"Enter correct Flight no.\n";
                goto top;
            }
    }
void a::empty()                                                     /*Function for empty seats*/
    {
        for(int i=0; i<30;i++)
            {
                for(int j=0;j<6;j++)
                    {
                        strcpy(flight[p].seat[i][j], "Empty");
                    }
            }
    }
void a::show()                                                      /*Function for availability of seats*/
    {
        int n;
        char number[5];

        cout<<"Enter Flight no: ";
        cin>>number;

        for(n=0;n<=p;n++)
            {
                if(strcmp(flight[n].flight_no, number)==0)
                break;
            }

        while(n<=p)
            {
                vline('*');
                cout<<endl;
                cout<<"Flight no: \t"<<flight[n].flight_no
                    <<"\nCaptain: \t"<<flight[n].captain_name
                    <<"\t\tArrival time: \t"<<flight[n].arrival_time
                    <<"\tDeparture time:"<<flight[n].departure_time
                    <<"\nFrom: \t\t"<<flight[n].from
                    <<"\t\tTo: \t\t"<<flight[n].to<<"\n";
                vline('*');
                cout<<endl;

                flight[0].position(n);

                int a=1;

                for (int i=0; i<30; i++)
                    {
                        for(int j=0;j<6;j++)
                            {
                                a++;

                                if(strcmp(flight[n].seat[i][j],"Empty")!=0)
                                    {
                                        cout<<"\nThe Seat No. "<<(a-1)<<" is reserved for "<<flight[n].seat[i][j]<<".";
                                    }
                            }
                    }
                break;
            }
        if(n>p)
            {
                cout<<"Enter correct Flight no: ";
            }
    }

void a::position(int l)                                             /*Function for positioning of seats*/
    {
        int s=0;p=0;
        for (int i =0; i<30;i++)
            {
                cout<<"\n";

                for (int j = 0;j<6; j++)
                    {
                        s++;

                        if(strcmp(flight[l].seat[i][j], "Empty")==0)
                            {
                                cout.width(10);
                                cout.fill(' ');
                                cout<<s<<".";
                                cout.width(10);
                                cout.fill(' ');
                                cout<<flight[l].seat[i][j];

                                p++;
                            }
                        else
                            {
                                cout.width(10);
                                cout.fill(' ');
                                cout<<s<<".";
                                cout.width(10);
                                cout.fill(' ');
                                cout<<flight[l].seat[i][j];
                            }
                    }
            }         
        cout<<"\n\nThere are "<<p<<" Seats empty in Flight No: "<<flight[l].flight_no;
        cout<<"\n";
        vline('_');
    }

void a::avail()                                                     /*Function for availability of flights*/
    {
        for(int n=0;n<3;n++)
            {
                vline('_');
                cout<<"\n\n";
                vline('*');
                cout<<"\n\tFlight no: \t"<<flight[n].flight_no
                    <<"\n\tCaptain: \t"<<flight[n].captain_name
                    <<"\t\tArrival time: \t"<<flight[n].arrival_time
                    <<"\tDeparture Time: \t"<<flight[n].departure_time
                    <<"\n\tFrom: \t\t"<<flight[n].from
                    <<"\t\tTo: \t\t"<<flight[n].to<<"\n";
                vline('*');
                cout<<"\n";
                vline('_');
                cout<<"\n\n\n";
            }
    }

int main()      
    {
        system("cls");
        int w;

        while(1)
            {
                cout<<"\n\n\n\n\n";
                cout<<"\t\t1.Installation of Flights\n\t\t"
                    <<"2.Seat Booking\n\t\t"
                    <<"3.Show the availability of seats\n\t\t"
                    <<"4.Flights Available\n\t\t"
                    <<"5.Exit";
                cout<<"\n\t\tEnter your Choice:->";
                cin>>w;
                cout<<"\n\n";

                switch(w)
                    {
                        case 1:  flight[p].install();
                        break;
                        case 2:  flight[p].allotment();
                        break;
                        case 3:  flight[0].show();
                        break;
                        case 4:  flight[0].avail();
                        break;
                        case 5:  exit(0);
                    }
            }
        return 0;
    }