C++
#include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat = true) { menu(); cout << "-----------------------" << endl; cout << "Please enter Your Choice (1-3): " << endl; cin >> choice; cout << "----------------------" << endl; switch (choice) { case 1: cout << "Seating chart looks like this:" << endl; displaySeats(seatChar); break; case 2: reserveSeat(seatChar); break; case 3: exit(0); default: cout << "You need to enter a number 1-7" << endl; }//end of switch } return 0; } void menu() { cout << " AIR TICKETS SYSTEM " << endl; cout << "click [ 1 ] to Display Seat Chart" << endl; cout << "click [ 2 ]Reserve Seat" << endl; cout << "click [ 3 ] Quit" << endl; } void displaySeats(char seats[ROWS][COLS] ) { char letter; cout<<" 1 2 3 4"<<endl; for (int i = 0; i < ROWS; i++) { cout <<i+1<<" "; for (int j = 0; j < COLS; j++) { cout << seats[i][j] << " "; } cout << endl; } cout << endl; } void reserveSeat(char seat[ROWS][COLS]) { int row; int col; cout << "Please enter a seat in the form ROW 1-8 and 1-4 for the column: [example 2 2]: " << endl; cin >> row >> col; row--; col--; while (((row > ROWS) || (row <= 0)) &&(col > COLS || col<=0)) { cout << "You entered a seat that does not exist!" << endl; cout << "Please try again" << endl; cout << "Please enter a number 1-8 for the row and 1-4 for the column: " << endl; cin >> row >> col; row--; col--; } while (seat[row][col] == 'X') { cout << "Seat is already taken." << endl; cout << "Please try again!" << endl; cout << "Please enter a number 1-8 for the row and 1-4 for the column: " << endl; cin >> row >> col; row--; col--; } seat[row][col] = 'X'; cout << "Your seat was successful reserved!" << endl; }
//////////////////
Use OO paradigm to redo this code. Create a class with at least a data member for storing seats availability. The data member must be a twodimensional array. Your class must also contain a constructor and at least two member functions: one for seat booking and the other for displaying seat availability status.
#include<iostream>
using namespace std;
class Seats{
public:
const static int ROWS = 8; //for rows in airplane
const static int COLS = 4;
char seatChar[ROWS][COLS];
Seats(){
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < this->COLS; j++) {
this->seatChar[i][j] = '-';
}
}
}
// ------------------------------------
void getAccess(){
int choice; //input from menu
bool repeat = true; //needed for switch loop
while (repeat = true) {
this->menu();
cout << "-----------------------" << endl;
cout << "Please enter Your Choice (1-3): " <<
endl;
cin >> choice;
cout << "----------------------" << endl;
switch (choice) {
case 1:
cout << "Seating chart looks like this:" << endl;
this->displaySeats(seatChar);
break;
case 2:
this->reserveSeat(seatChar);
break;
case 3:
exit(0);
default:
cout << "You need to enter a number 1-7" << endl;
}//end of switch
}
}
// ----------------------------------------------
void menu() {
cout << " AIR TICKETS SYSTEM " << endl;
cout << "click [ 1 ] to Display Seat Chart" <<
endl;
cout << "click [ 2 ]Reserve Seat" << endl;
cout << "click [ 3 ] Quit" << endl;
}
// -----------------------------------------
void displaySeats(char seats[ROWS][COLS] )
{
char letter;
cout<<" 1 2 3 4"<<endl;
for (int i = 0; i < ROWS; i++) {
cout <<i+1<<" ";
for (int j = 0; j < COLS; j++) {
cout << seats[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
// ----------------------------------------------------
void reserveSeat(char seat[ROWS][COLS]) {
int row;
int col;
cout << "Please enter a seat in the form ROW 1-8 and 1-4 for
the column: [example 2 2]: " << endl;
cin >> row >> col;
row--; col--;
while (((row > ROWS) || (row <= 0)) &&(col >
COLS || col<=0)) {
cout << "You entered a seat that does not exist!" <<
endl;
cout << "Please try again" << endl;
cout << "Please enter a number 1-8 for the row and 1-4 for
the column: " << endl;
cin >> row >> col;
row--; col--;
}
while (seat[row][col] == 'X') {
cout << "Seat is already taken." << endl;
cout << "Please try again!" << endl;
cout << "Please enter a number 1-8 for the row and 1-4 for
the column: " << endl;
cin >> row >> col;
row--; col--;
}
seat[row][col] = 'X';
cout << "Your seat was successful reserved!" <<
endl;
}
// ----------------------------------
};
int main(){
// create object seats and get access
Seats seats;
seats.getAccess();
return 0;
}
------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,
IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~
Get Answers For Free
Most questions answered within 1 hours.