(C++)Programming Assignment. Please follow all instructions! It’s blockbuster mo
ID: 3839876 • Letter: #
Question
(C++)Programming Assignment. Please follow all instructions!
It’s blockbuster movie season! Your local theater has contracted you to design a simulator for the lines of people waiting to see the various movies. The theater is trying out the following new policy:
Patrons may begin lining up 2 hours before the movie starts,
Once a patron is in line, there is no way out except through the front of the line (i.e., into the
theater). Patrons are advised to use the restroom before getting in line.
The doors to the theater are opened 1 hour before the movie starts, and patrons are let out of
the line one at a time at a rate of one per minute to give each time to find a seat without others
rushing in behind them.
Patrons may enter the line up until the start of the movie – e.g., if the movie starts at 17:30,
patrons can get in line at 17:29 but the line closes as soon as the clock reads 17:30.
If the line fills up, or the doors close with patrons left in the line, those patrons will complain to
management, leave, and write a bad review on Yelp. This is where you come in.
Your goal is to create a class to model this new policy for the theater. You will be given sample data representing the typical flow of patrons into the line and help the theater managers decide if their new policy makes sense, or if the policy will cause lines to “overflow.”
The theaterLine Class
You will create a class with the following properties:
1) A private member: an STL queue to model a single line of patrons waiting to enter a theater. The queue will hold the names of the people in line (a string).
2) A private member: the maximum number of people allowed in the queue (an int).
a. Add an accessor for this member, but not a mutator.
3) A default constructor and parameterized constructor to initialize the queue appropriately.
4) A method to return the number of patrons currently in the queue.
5) A method to add a single patron to the end of the queue if there is space available.
6) A method that both removes a single patron from the front of the queue and returns this
person’s name (a string).
Testing (below testOne is a .txt)
You will be given test files representing the flow of patrons into the line for a single movie showing. The first line of the file will list the start hour and minute, followed by size of the theater line, and the title of the movie. Each subsequent line in the file has a time (hour, followed by minute) and a list of patrons (represented by single letters for simplicity). All times are in military (24-hour) time.
Here is an example for a showing of The Empire Strikes Back which starts at 17:30, with a line size of 5. (which is testOne)
15 31 A X Y Z
15 32 B 15 33 C
15 34 D Q M P
17 29 R S T U
Your Client
Most of the code you write will be in your client. Your client should include a function to simulate the behavior of the line. It will take the start hour/min, line size, movie title, and an input file stream. In other words, your main() should open the file, read the first line, and pass all this information to the client function. Here is the header for my client function:
void simulateMovie(int startHour, int startMin, int lineSize, string title, ifstream &fs)
The client should have a loop that simulates every minute from the opening of the line to the closing of the line. Each “tick” of the clock in your loop should call appropriate functions on a theaterLine object to add and remove patrons from the line as needed. If the theater line fills up print a message to the screen to report this. When the movie begins (e.g., 17:30 in the example above), end the simulation and report how many people are still left waiting to get into the theater. Here is an example run with my program for theThe Empire Strikes Back showing above. ( which is the output of testOne)
Explanation / Answer
#include <bits/stdc++.h>
#include <time.h>
using namespace std;
class order {
private:
string orderNumber;
string comboDescription;
string firstName;
string lastName;
string address;
int zipCode;
int milesFromRestaurant;
double orderCost;
set<string> orderNumbersTillNow;
public:
order() {
string orderNumber;
int seed = 131313312;
srand(time(NULL));
bool isUnique = false;
while (!isUnique) {
// generate random 4 chars
for (int i = 0; i < 4; ++i) {
orderNumber += "" + char(rand() % 26 + 'A');
}
// generate random 4 digit
for (int i = 0; i < 4; ++i) {
orderNumber += "" + char(rand() % 10 + '0');
}
if (orderNumbersTillNow.find(orderNumber) != orderNumbersTillNow.end()) {
orderNumber = "";
} else {
orderNumbersTillNow.insert(orderNumber);
this->orderNumber = orderNumber;
isUnique = true;
}
}
}
string getOrderNumber() {
return this->orderNumber;
}
string getComboDescription() {
return this->comboDescription;
}
void setComboDescription(string comboDescription) {
this->comboDescription = comboDescription;
}
string getFirstName() {
return this->firstName;
}
void setFirstName(string firstName) {
this->firstName = firstName;
}
string getLastName() {
return this->lastName;
}
void setLastName(string lastName) {
this->lastName = lastName;
}
string getAddress() {
return this->address;
}
void setAddress(string address) {
this->address = address;
}
int getZipCode() {
return this->zipCode;
}
void setZipCode(int zipCode) {
this->zipCode = zipCode;
}
int getMilesFromRestaurant() {
return this->milesFromRestaurant;
}
void setMilesFromRestaurant(int milesFromRestaurant) {
this->milesFromRestaurant = milesFromRestaurant;
}
double getOrderCost() {
return this->orderCost;
}
void setOrderCost(double orderCost) {
this->orderCost = orderCost;
}
};
// in memory queue for handling orders
queue<order> q;
// helper function to print given order
void printOrder(order o) {
cout << "Order Number: " << o.getOrderNumber() << endl;
cout << "Combo Description: " << o.getComboDescription() << endl;
cout << "First Name: " << o.getFirstName() << endl;
cout << "Last Name: " << o.getLastName() << endl;
cout << "Address: " << o.getAddress() << endl;
cout << "Zipcode: " << o.getZipCode() << endl;
cout << "Miles away from restaurant: " << o.getMilesFromRestaurant() << endl;
printf("Order Cost: %ld ", o.getOrderCost());
}
// helper function to check if given string
// is number
bool isNumber(string num) {
if (num.size() == 0) return false;
for (int i = 0; i < num.size(); ++i) {
if (num[i] >= '0' && num[i] <= '9') continue;
else return false;
}
return true;
}
// helper function to covert given string to int
int getIntFromString(string num) {
int result = 0;
for (int i = 0; i < num.size(); ++i) {
result = result * 10 + (num[i] - '0');
}
return result;
}
// helper function to prompt user for order details input
// for given order
void takeOrder(order &o) {
string userInput;
cout << "Enter combo description: ";
getline(cin, userInput);
o.setComboDescription(userInput);
cout << "Enter first name: ";
getline(cin, userInput);
o.setFirstName(userInput);
cout << "Enter last name: ";
getline(cin, userInput);
o.setLastName(userInput);
cout << "Enter address: ";
getline(cin, userInput);
o.setAddress(userInput);
bool correct = false;
while (!correct) {
cout << "Enter zipCode: ";
getline(cin, userInput);
if (userInput.size() != 5) {
cout << "Error: Please enter 5 length number!" << endl;
} else if (isNumber(userInput)) {
int result = getIntFromString(userInput);
if (result >= 10000 && result <= 99999) {
o.setZipCode(result);
correct = true;
} else {
cout << "Error: Please enter 5 length number!" << endl;
}
} else {
cout << "Error: Please enter 5 length number!" << endl;
}
}
correct = false;
while (!correct) {
cout << "Enter miles from restaurant: ";
getline(cin, userInput);
if (!isNumber(userInput)) {
cout << "Error: Please enter miles from restaurant as a number!" << endl;
} else if (getIntFromString(userInput) >= 0 && getIntFromString(userInput) <= 15) {
o.setMilesFromRestaurant(getIntFromString(userInput));
correct = true;
} else {
cout << "Error: Please enter miles from restaurant in between 1 to 15!" << endl;
}
}
double orderCost;
cout << "Enter order cost: ";
cin >> orderCost;
o.setOrderCost(orderCost);
getline(cin, userInput);
}
// transfer elements from src queue to dest queue
void transfer(queue<order> &src, queue<order> &dest) {
while (!src.empty()) {
dest.push(src.front());
src.pop();
}
}
// display all orders
void displayOrders() {
queue<order> tmp;
while (!q.empty()) {
order o = q.front(); q.pop();
tmp.push(o);
printOrder(o);
}
transfer(tmp, q);
}
// push an order into queue
void enterOrder(order o) {
q.push(o);
}
// delete an order from queue
void deleteOrder(string orderNumber) {
bool deleted = false;
queue<order> tmp;
while (!q.empty()) {
order o = q.front(); q.pop();
if (o.getOrderNumber() == orderNumber) {
deleted = true;
continue;
}
tmp.push(o);
}
transfer(tmp, q);
if (!deleted) {
cout << "Order not found!" << endl;
}
}
// search order if it is found print it details
void searchOrder(string orderNumber) {
bool isSearchSuccessful = false;
queue<order> tmp;
while (!q.empty()) {
order o = q.front(); q.pop();
if (o.getOrderNumber() == orderNumber) {
isSearchSuccessful = true;
printOrder(o);
}
tmp.push(o);
}
transfer(tmp, q);
if (!isSearchSuccessful) {
cout << "Order not found!" << endl;
}
}
// edit order if found
void editOrder(string orderNumber) {
bool isEditSuccessful = false;
queue<order> tmp;
while (!q.empty()) {
order o = q.front(); q.pop();
if (o.getOrderNumber() == orderNumber) {
takeOrder(o);
isEditSuccessful = true;
}
tmp.push(o);
}
transfer(tmp, q);
if (!isEditSuccessful) {
cout << "Order not found!" << endl;
}
}
// move given orderNumber order to front
void moveToFront(string orderNumber) {
bool isMoveSuccessful = false;
order orderToMove;
queue<order> tmp;
while (!q.empty()) {
order o = q.front(); q.pop();
if (o.getOrderNumber() == orderNumber) {
orderToMove = o;
isMoveSuccessful = true;
continue;
}
tmp.push(o);
}
if (!isMoveSuccessful) {
cout << "Order not found!" << endl;
} else {
q.push(orderToMove);
}
transfer(tmp, q);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.