Code:
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
struct SEAT {
bool occupied; // initially, false
string first,last; // passenger name
int numBags; // max of 4
char mealType; // (r)egular, (v)egetaria, (o)ther
};
const int ROWS= 20;
const int COLS= 10;
int menuchoice();
void clearPlane(SEAT p[][COLS]);
void showPlane(SEAT p[][COLS]);
bool getSeat(SEAT p[][COLS], int &row, int &col);
void getSeatFromUser(SEAT p[][COLS], int &row, int &col);
void getInfo(SEAT s[][COLS], int, int);
void showInfo(SEAT s[][COLS], int, int);
void makeReservation(SEAT p[][COLS]);
void cancelReservation(SEAT p[][COLS]);
void viewReservations(SEAT p[][COLS]);
int main()
{
int choice; // user's menu choice
SEAT plane[ROWS][COLS]; // reservation info for entire plane
clearPlane(plane);
showPlane(plane);
do
{
choice= menuchoice();
switch (choice) {
case 1: makeReservation(plane); break;
case 2: cancelReservation(plane); break;
case 3: viewReservations(plane); break;
case 4: break;
}
}while(choice !=4);
return 0;
}
/* menuchoice
**-----------------
** displays menu and get's user's choice
**--------------------------------------------------------------------*/
int menuchoice()
{
int choice;
cout << endl << endl;
cout << "+------------------------+\n";
cout << "| P.Metal Airlines |\n";
cout << "+------------------------+\n";
cout << "| [1] Reserve a Seat |\n";
cout << "| [2] Cancel Reservation |\n";
cout << "| [3] List Reservations |\n";
cout << "| [4] Exit Program |\n";
cout << "+------------------------+\n";
do {
cout << "Choice: ";
cin >> choice;
if(!cin)
cin.clear();
} while (choice < 1 || choice > 4);
return choice;
}
//Tabulated Seats
void showPlane(SEAT p[][COLS])
{
int i,j;
cout << " 1 2\n";
cout << " 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0\n";
for (j=0; j<COLS; j++) {
cout << (char) (j+65);
for (i=0; i<ROWS; i++)
if (p[i][j].occupied)
cout << " x";
else
cout << " ";
cout << endl;
}
}
/* clearPlane
**-----------------
** empties all reservations in airplane
**--------------------------------------------------------------------*/
void clearPlane(SEAT p[][COLS])
{
int i,j;
for (i=0; i<ROWS; i++)
for (j=0; j<COLS; j++)
p[i][j].occupied= false;
}
/* getSeat
**-----------------
** obtains desired row and column number for a seat; the seat can be
** selected by the user or can be selected automatically by the
** computer; the function return true if an open seat was found;
** false otherwise.
**--------------------------------------------------------------------*/
bool getSeat(SEAT p[][COLS], int &row, int &col)
{
bool allTaken; // true if all seats on plane are occupied
char method; // (a)utomatic or (m)anual seat selection
allTaken= true; // make sure at least one
for (row=0; row<ROWS && allTaken; row++) // seat is available
for (col=0; col<COLS && allTaken; col++)
if (!p[row][col].occupied)
allTaken= false;
if (allTaken) return false; // if not return false
do {
cout << "Would you like to select a seat (a)utomatically or (m)anually: ";
cin >> method;
} while (tolower(method)!='a' && tolower(method)!='m');
switch (tolower(method)) {
case 'a': for (row=0; row<ROWS; row++) // (a)utomatic seat selection
for (col=0; col<COLS; col++)
if (!p[row][col].occupied)
return true;
break; // this break is redundant, we hope
case 'm': getSeatFromUser(p,row,col); // (m)anual seat selection
return !p[row][col].occupied;
}
return false; // we should never get here
}
/* getSeatFromUser
**-----------------
** obtains desired row and column number for a seat; this function
** will be called from getSeat when requesting a seat for making
** reservations and will be called directly when selecting a
** seat for cancellation.
**--------------------------------------------------------------------*/
void getSeatFromUser(SEAT p[][COLS], int &row, int &col)
{
char let;
showPlane(p);
do {
cout << "Enter row (1.." << ROWS << "): ";
cin >> row;
} while (row < 1 || row > ROWS);
do {
cout << "Enter col (A.." << (char) (COLS+64) << "): ";
cin >> let;
let= toupper(let);
} while (let < 'A' || let > (COLS+64));
col= let-65;
row--;
}
/*--------------------------------------------------------
* Accepts the SEAT struct then the user enters in their
* information.
* ------------------------------------------------------*/
void getInfo(SEAT s[][COLS], int row, int col)
{
cin.ignore(100,'\n');
char answer;
do{
cout<<"Enter in this information:\n\n";
// getSeat(p[][COLS], &row, &col);
cout<<"Enter your first name: ";
cin>>s[row][col].first;
cout<<"Enter your last name: ";
cin>>s[row][col].last;
cout<<"\n";
do{
cin.clear();
cout<<"How many bags will you be bringing?(limit of 4) ";
cin>>s[row][col].numBags;
cout<<"\n";
}while(s[row][col].numBags<0||s[row][col].numBags>4);
do{
cin.clear();
cout<<"What type of meal would you like?((c)hicken, (h)am, or (o)ther) ";
cin>>s[row][col].mealType;
s[row][col].mealType=tolower(s[row][col].mealType);
cout<<"\n";
}while(s[row][col].mealType!='c' && s[row][col].mealType!='h' && s[row][col].mealType!= 'o');
/* cout<<"Seat: "<<row<<", "<<(char)col+65<<"\n";
cout<<"Name: "<<SEAT.first<<" "<<SEAT.last<<"\n";
cout<<"Number of bags: " <<SEAT.numBags<<"\n";
cout<<"Meal Type: "<<SEAT.mealType<<"\n";*/
showInfo(s, row, col);
cin.clear();
cout<<"Is this information correct? ";
cin>>answer;
}while(answer=='n');
}
/*--------------------------------------------------------
* Displays the information they just entered.
* ------------------------------------------------------*/
void showInfo(SEAT s[][COLS], int row, int col)
{
string chicken="Chicken";
string ham="Ham";
string other="Other";
cout<<"Seat: "<<row+1<<", "<<(char)(col+65)<<"\n";
cout<<"Name: "<<s[row][col].first<<" "<<s[row][col].last<<"\n";
cout<<"Number of bags: " <<s[row][col].numBags<<"\n";
if(s[row][col].mealType=='c')
cout<<"Meal Type: "<<chicken<<"\n";
if(s[row][col].mealType=='h')
cout<<"Meal Type: "<<ham<<"\n";
if(s[row][col].mealType=='o')
cout<<"Meal Type: "<<other<<"\n";
}
/*--------------------------------------------------------
* Calls showPlane and then will ask what seat they want
* and will call getInfo
* ------------------------------------------------------*/
void makeReservation(SEAT p[][COLS])
{
int row;
int col;
//SEAT person;
do{
getSeat(p,row, col);
}while(p[row][col].occupied==true);
getInfo(p,row,col);
}
/*--------------------------------------------------------
* User enters in a seat row and column and if it is empty
* it will display that. If it is occupied then it will
* make the array empty.
* ------------------------------------------------------*/
void cancelReservation(SEAT p[][COLS])
{
int row;
int col;
getSeat(p, row, col);
if(p[row][col].occupied==false)
cout<<"The reservation is empty.";
else
p[row][col].occupied=false;
}
/*--------------------------------------------------------
* For loop that will go through each seat and if it is
* occupied it will display the SEAT struct for that one
* then move on.
* ------------------------------------------------------*/
void viewReservations(SEAT p[][COLS])
{
int i;
int n;
for(i=0; i<10; i++)
for(n=0; n<20; n++)
if(p[i][n].occupied==true)
{
cout<<"showInfo(p, i, n)";
}
}
Comment