Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++)Programming Assignment It’s blockbuster movie season! Your local theater ha

ID: 3839195 • Letter: #

Question

(C++)Programming Assignment

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

Here is the code for the question. Please don't forget to rate the answer if it helped. Thank you very much.

theatre.h

#include <iostream>
#include <queue>
using namespace std;
class TheatreLine
{
private:
queue<string> line; //the queue for patrons to be lined
string movie_name;
int start_hour,start_min;
int max;
public:
TheatreLine()
{
start_hour = start_min = max = 0;
}
TheatreLine(int hour, int min, int maxno, string movie)
{
start_hour = hour;
start_min = min;
max = maxno;
movie_name = movie;
}

int get_max()
{
return max;
}

int get_no_patrons()
{
return line.size();
}

bool enqueue(string patron)
{
if(line.size() == max)
return false;
else
{
line.push(patron);
return true;
}
}

string dequeue()
{
string next;
if(line.size() != 0)
{
next = line.front();
line.pop();
}
return next;
}

string get_movie()
{
return movie_name;
}

int get_hour()
{
return start_hour;
}

int get_min()
{
return start_min;
}
};

client.cpp

#include "theatre.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{

ifstream infile;
string filename;
int hour, minute, max;
string movie;


cout<<"Enter input filename: ";
cin>>filename;

infile.open(filename.c_str());
if(!infile.is_open())
{
cout<<"Could not open input file : "<<filename;
return 1;
}

infile>> hour >> minute >> max ;
getline(infile, movie);

TheatreLine tline(hour, minute, max, movie);
int curr_hour = hour - 2, curr_min = minute; //line start 2 hours before movie start time

cout<<"Starting simulation for "<<tline.get_movie()<<endl;
cout<<"The line can hold a maximum of "<<tline.get_max() <<" patrons "<<endl;
cout<<"The time currently is "<<curr_hour<<":"<<curr_min<<endl;

int file_hour, file_min;
string patrons ,p ;
int idx;
int total_mins, theatre_mins = tline.get_hour()*60 + tline.get_min();

//always read the next line time details in advance from the file, so that we can match
// in next iteration

infile >> file_hour >> file_min; //read the next time in the file

while(!(curr_hour== tline.get_hour() && curr_min == tline.get_min()))
{
total_mins = curr_hour * 60 + curr_min; // convert to total minutes so comparison for 1 hour is easy

if(curr_hour == file_hour && curr_min == file_min) //if the line time is same as current simulation time
{
getline(infile, patrons); //get all patrons
int prev =1 ;
cout<<curr_hour<<":"<<curr_min<<endl;

//get individual patron names using space separator
do {
idx = patrons.find(' ',prev);
if(idx != -1)
p = patrons.substr(prev, idx - prev);
else
p = patrons. substr(prev);

if(tline.enqueue(p)) //did this patron get queued ?
{
cout<<" "<<p << " is entering the line"<<endl;
}
else
{
cout<<" Line has overflowed! Patron "<<p << " was turned away!"<<endl;
}
if(idx == -1 ) break;
prev = idx+1;
}while (true);

if(infile >> file_hour) // get the next patron time
{
infile >> file_min;
}
else //reached end of file .. no more data
file_hour = -1;
}

//check if we need to start dequeuing the line
if( theatre_mins - total_mins <= 60) //start sending patrons inside theatre 1 hour prior to moive start
{
if(tline.get_no_patrons() != 0)
{
cout<<curr_hour<<":"<<curr_min<<endl;
cout<<" "<<tline.dequeue() << " has entered the theatre"<<endl;
}
}

//increase the simulation time by 1 min
curr_min ++;
if(curr_min == 60)
{
curr_min = 0;
curr_hour ++;
if(curr_hour == 24)
curr_hour = 0;
}
}

cout<<"Doors closing, ending simulation!"<<endl;
cout<<"There are "<<tline.get_no_patrons() << " patrons left in line!"<<endl;

}

input file theatre.txt

17 30 5 The Empire Strikes Back
15 31 A X Y Z
15 32 B
15 33 C
15 34 D Q M P
15 35 E
15 36 F
15 37 G
15 38 H
15 39 I
16 40 J
16 50 K
17 29 R S T U

sample run

Enter input filename: theatre.txt
Starting simulation for The Empire Strikes Back
The line can hold a maximum of 5 patrons
The time currently is 15:30
15:31
   A is entering the line
   X is entering the line
   Y is entering the line
   Z is entering the line
15:32
   B is entering the line
15:33
   Line has overflowed! Patron C was turned away!
15:34
   Line has overflowed! Patron D was turned away!
   Line has overflowed! Patron Q was turned away!
   Line has overflowed! Patron M was turned away!
   Line has overflowed! Patron P was turned away!
15:35
   Line has overflowed! Patron E was turned away!
15:36
   Line has overflowed! Patron F was turned away!
15:37
   Line has overflowed! Patron G was turned away!
15:38
   Line has overflowed! Patron H was turned away!
15:39
   Line has overflowed! Patron I was turned away!
16:30
   A has entered the theatre
16:31
   X has entered the theatre
16:32
   Y has entered the theatre
16:33
   Z has entered the theatre
16:34
   B has entered the theatre
16:40
   J is entering the line
16:40
   J has entered the theatre
16:50
   K is entering the line
16:50
   K has entered the theatre
17:29
   R is entering the line
   S is entering the line
   T is entering the line
   U is entering the line
17:29
   R has entered the theatre
Doors closing, ending simulation!
There are 3 patrons left in line!