1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
#include <iostream>
using namespace std;
void bookSeat(int seatFare[], bool isBooked[]);
void printAll(int seatFare[]);
void printBooked(int seatFare[], bool isBooked[], bool val);
int printLeast(char c);
void InitializeSeats(int seatFare[], bool isBooked[]);
void printSeat(int seatFare[], bool isBooked[]);
void displayMenu()
{ cout << "\n 1. Book Seat" << endl;
cout << " 2. All Seat Status" << endl;
cout << " 3. Empty Seats and Fair" << endl;
cout << " 4. Booked Seats and Fair" << endl;
cout << " 5. Print A Seat" << endl;
cout << " 6. Quit the program" << endl;
cout << "Enter your choice:";
}
int main()
{ int seatFare[30]={-1};
bool isBooked[30]={false};
int choice=0;
InitializeSeats(seatFare, isBooked);
while (choice!=6)
{ displayMenu();
cin >> choice;
switch(choice)
{
case 1: bookSeat(seatFare, isBooked);
break;
case 2: printAll(seatFare);
break;
case 3: printBooked(seatFare, isBooked, false);
break;
case 4: printBooked(seatFare, isBooked, true);
break;
case 5: printSeat(seatFare, isBooked);
break;
case 6: break;
}
}
return 0;
}
bool printAvailable(int seatFare[], bool isBooked[], int x,int y)
{ int index ;
bool found=false;
for (index= x; index < y; ++index)
{ if(isBooked[30]==false)
{ cout <<"#"<<index+1<<"\t"<<seatFare[index]<<endl;
found=true;
}
}
return found;
}
void printAll(int seatFare[])
{ int index=0;
for (index= 0; index < 30; index++)
cout <<"#"<<index+1<<"\t"<<seatFare[index]<<endl;
}
void printBooked(int seatFare[], bool isBooked[], bool val)
{ int index ;
for (index= 0; index < 30; index++)
{ if(val==isBooked[index])
cout <<"#"<<index+1<<"\t"<<seatFare[index]<<endl;
}
}
int printLeast(int seatFare[], bool isBooked[], char c)
{ int x,y,i,least=-1;
if (c=='F')
{ x=0;y=10;
}
else
{ x=10;y=30;
}
int min=-1;
for (i= x; i < y; ++i)
{ if(false==isBooked[i] && seatFare[x]<min)
least=i;
}
return least;
}
void InitializeSeats(int seatFare[], bool isBooked[])
{ int i=0,diff=200;
seatFare[0] = 2000;
for(i=1;i<30;i++)
{ isBooked[i]=false;
if (i==10)
{ seatFare[i]=1400;
diff = -50;
continue;
}
seatFare[i]=seatFare[i-1]+diff;
}
}
void bookSeat(int seatFare[], bool isBooked[])
{ int choice,seatno=-1;
char inp;
cout << "Welcome to Airline Passenger seat assignment" << endl;
cout <<"1.First Class"<<endl<<"2.Economy Class"<< endl;
cin >> choice;
if(choice==1)
{ if(printAvailable(seatFare, isBooked, 0,10))
{ cout << "Please enter the seat number :";
cin >> seatno;
if(isBooked[seatno-1])
cout << "Seat already booked";
else
{ isBooked[seatno-1]=true;
cout << "Booked Seat : "<<seatno<<endl;
cout << "class : First Class"<<endl;
cout << "Fare is : " << seatFare[seatno-1]<<endl;
}
}
else
{ seatno=printLeast('E');
if( seatno!= -1)
{ cout << "Would you like to Book This Instead (y/n)::";
cin >> inp;
if(inp=='y')
{ isBooked[seatno-1]=true;
cout << "Booked Seat :: "<<seatno<<endl;
cout << "class :: First Class"<<endl;
cout << "Fare :: " << seatFare[seatno-1]<<endl;
}
}
}
}
else if(choice==2)
{ if (printAvailable(seatFare, isBooked, 10,30))
{ cout << "Please enter the seat number :";
cin >> seatno;
if(isBooked[seatno-1])
cout << "Seat already booked";
else
{ isBooked[seatno-1]=true;
cout << "Booked Seat :: "<<seatno<<endl;
cout << "class :: First Class"<<endl;
cout << "Fare :: " << seatFare[seatno-1]<<endl;
}
}
else
{ seatno=printLeast('F');
if( seatno!= -1)
{ cout << "Would you like to Book This Instead (y/n)::";
cin >> inp;
if(inp=='y')
{ isBooked[seatno-1]=true;
cout << "Booked Seat :: "<<seatno<<endl;
cout << "class :: Economy Class"<<endl;
cout << "Fare :: " << seatFare[seatno-1]<<endl;
}
}
}
}
}
void printSeat(int seatFare[], bool isBooked[])
{ int num;
cout << " Enter Seat Number" << endl;
cin>>num;
cout<<"#"<<num<<"\t"<<"Status :"<<isBooked[num-1]<<"\t Fare :"<<seatFare[num-1]<<endl;
}
|