Introduction ZettaAir airlines had only one plane with only 3 seats for passenge
ID: 3748412 • Letter: I
Question
Introduction
ZettaAir airlines had only one plane with only 3 seats for passengers. The company needed a computer program to book passengers, or if the plane was fully booked, to add customers to a waiting list (also 3 long).
Program Description
Your program will display (see the sample output) the following menu at startup and will continue to do so, after it executes the user's choice to add, delete or show passengers, until the user chooses exit, which of course exits the program.
Menu
========
1. Add Passenger
2. Delete Passenger
3. Show Passengers
4. Exit
Enter choice:
The program maintains two lists, which as explained below are both queues. The first is for booked passengers. Since the plane has only 3 seats for passengers, only 3 passengers can be on this list. The second is the waiting list. ZettaAir's policy is not to have more than 3 customers on the waiting list; anyone else is told to try again later.
Adding
You add a customer to the list of booked passengers unless that list is full (3 booked passengers). If it is, then you add the customer to the waiting list unless that list also is full (maximum size of waiting list is 3). If so, you tell the customer sorry, try again later. Each passenger is added in order.
Deleting
If there are any booked passengers, then you delete the first passenger on that list. Next, if there are any customers on the waiting list, you add the first customer on the waiting list to the list of booked passengers, and of course take that customer off the waiting list.
If there are no booked passengers, then there is no one to delete (if there are no booked passengers there can't be any customers on the waiting list).
Showing a Passenger
See the sample output. If there are any booked passengers, then you list them in order. You then list in order any customers on the waiting list unless there are none, in which case you advise there is no one on the waiting list.
If there are no booked passengers, then there is no one to show (if there are no booked passengers there can't be any customers on the waiting list), so you advise there are no booked passengers.
Queue Class
Both the booked passenger list and the waiting list are queues. Since deletions are permitted, each queue will be implemented by a circular or ring queue. This concept is explained in the book and in Module #4 (Queue).
You will write the queue as a class. Module #4 (Queue) explains the member variables and member functions, including a constructor.
Files
The class will be written using header and implementation files. Your program also will include a driver file, so your multi-file project will have these three files and no others:
Your Job
The code/output handout provides all the code for the cqueue.h file (except the value of one constant) and the test.cpp file except the implementation of the add, delete and show functions. Your job is to complete those two files and write the cqueue.cpp file.
File Name Purpose cqueue.h Header file for queue cqueue.cpp Implementation file for queue test.cpp Driver fileExplanation / Answer
cqueue.cpp
#include "cqueue.h"
#include <iostream>
using namespace std;
CQueue::CQueue()
{
front = MAX - 1;
rear = MAX - 1;
}
bool CQueue::IsEmpty()
{
return (front == rear);
}
bool CQueue::IsFull()
{
return ((rear + 1) % MAX == front);
}
void CQueue::Enqueue(Passenger p)
{
rear = (rear + 1) % MAX;
strcpy_s(passengers[rear].name, 80, p.name);
}
Passenger CQueue::Front()
{
return passengers[(front + 1) % MAX];
}
void CQueue::Dequeue()
{
front = (front + 1) % MAX;
}
cqueue.h
const int MAX = 4; //To do: determine appropriate number
struct Passenger {
char name[80];
};
class CQueue {
private:
int front;
int rear;
Passenger passengers[MAX];
public:
CQueue();
bool IsEmpty(void);
bool IsFull(void);
void Enqueue(Passenger);
Passenger Front(void);
void Dequeue(void);
};
test.cpp
#include <iostream>
#include <string>
#include "cqueue.h"
using namespace std;
enum choice { BOOKED, WAITING };
const int LINES = 2;
int showMenu(void);
void addPassenger(CQueue*);
void deletePassenger(CQueue*);
void showPassengers(CQueue*);
int main(void)
{
CQueue qPassengers[LINES];
int x;
do {
x = showMenu();
switch (x)
{
case 1: addPassenger(qPassengers);
break;
case 2: deletePassenger(qPassengers);
break;
case 3: showPassengers(qPassengers);
break;
}
} while (x != 4);
return 0;
}
int showMenu(void)
{
int select;
cout << "Menu ";
cout << "======== ";
cout << "1. Add Passenger ";
cout << "2. Delete Passenger ";
cout << "3. Show Passengers ";
cout << "4. Exit ";
cout << "Enter choice: ";
cin >> select;
return select;
}
// To do: implement addPassenger, deletePassenger and showPassengers
void addPassenger(CQueue* qP)
{
Passenger pas;
if (!qP[BOOKED].IsFull())
{
cout << "Enter name: ";
cin >> pas.name;
cout << "Booking " << pas.name << " on flight." << endl;
qP[BOOKED].Enqueue(pas);
}
else
{
if (!qP[WAITING].IsFull())
{
cout << "Enter name: ";
cin >> pas.name;
cout << "Sorry. Plane fully booked. Adding " << pas.name << " to waiting list" << endl;
qP[WAITING].Enqueue(pas);
}
else
{
cout << "Sorry. Plane and waiting list fully booked. Try later" << endl;
}
}
}
void deletePassenger(CQueue* qP)
{
Passenger pas;
if (qP[BOOKED].IsEmpty())
{
cout << "No passengers to delete ";
}
else
{
if (!qP[BOOKED].IsEmpty())
{
// removing first person from booked list
pas = qP[BOOKED].Front();
cout << "Removing " << pas.name << " from booked passengers." << endl;
qP[BOOKED].Dequeue();
if (!qP[WAITING].IsEmpty())
{
//Adding first passenger from waiting list to booked list
pas = qP[WAITING].Front();
cout << "Adding " << pas.name << " from waiting list." << endl;
qP[BOOKED].Enqueue(pas);
//delete passenger from front of waiting list
qP[WAITING].Dequeue();
}
}
}
}
void showPassengers(CQueue* qP)
{
CQueue tempB = qP[BOOKED];
CQueue tempW = qP[WAITING];
Passenger pas;
//no passengers to display if booked empty
if (tempB.IsEmpty())
{
cout << "No passengers to display" << endl;
return;
}
//show passengers on BOOKED
//display, dequeue until empty
if (!tempB.IsEmpty())
{
cout << "Booked Passengers" << endl;
cout << "======== ";
while (!tempB.IsEmpty())
{
pas = tempB.Front();
cout << pas.name << endl;
tempB.Dequeue();
}
}
//show passengers on WAITING
//get front, display until empty
if (!tempW.IsEmpty())
{
cout << "Waiting List" << endl;
cout << "======== ";
while (!tempW.IsEmpty())
{
pas = tempW.Front();
cout << pas.name << endl;
tempW.Dequeue();
}
}
else
{
cout << "No passengers on waiting list" << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.