The Brigade is up in arms over recent changes to the barber shop in Bancroft! Based on the results of a Midshipman capstone project in the Econ department, NABSD has determined that they can cut costs and still provide the necessary level of customer service by only employing one barber. Midshipmen are upset, and the Yak is exploding accordingly. The Commandant is concerned, so he wants you to do a computer simulation to determine if the Brigade's claim of poor expediency is well founded before he decides to take any action.
You will build a simulation modeling wait times in the haircut line (or the "queue", as the British exchange students call it). Recall from lecture that a queue has the property of first in, first out (FIFO). The abstract data type that supports the FIFO principle and provides useful functions is the "Queue" class. Its operations and structure is similar to the now familiar "Stack" abstract data type (ADT) except the LIFO paradigm of a stack is replaced by FIFO for the queue. We will build our simulation in pieces.
For this lab, I'm pushing the proverbial "I believe" button that you all thoroughly understand the concepts of seperating interface from implementation. So for the sake of simplicity, all code for this lab will be done in one single file instead of multiple .h
and .cpp
files.
For this first part of the lab, you'll write a program that simply reads from a file of Midshipman arrival times, stores those arrival times in a queue, and prints out the arrival times from the queue - just to verify that you've stored them properly. Use this starter code file to complete this and all follow-on parts of the lab: lab06_starter.cpp. You should make use of TradQueue.h provided in the last lecture. For the arrival times, the file input61.txt is a sample input file.
Write a program that:
./part1 < input61.txt
OR
cat input61.txt | ./part1
incoming
Printing the contents of the queue should give you the same order as they were put in - not reversed like a stack!
Let's use the system clock to drive our simulation - in other words let's have the customers show up in "real time". Well ... let's use seconds rather than minutes so that 30 minutes of simulation will only take 30 seconds. As these Midshipman arrive, we'll print out a message to say they've arrived and we'll place them in a queue line
that will model the actual line at the barber shop. For the moment this is the line from hell, because people will get into it but never get out! (kind of like waiting for a beverage at Armadillo's).
The function call time(0)
(you need to include ctime
) will read the current time from the system clock. The time read is the number of seconds since 1 Jan 1970, also know as the Epoch. You can track time in your program by putting
int startTime = time(0); //startTime will represent the barber shop opening
at the beginning of your simulation, so that the elapsed time (which is the "current time" in the simulation) can be determined at any point in the program with:
int currentTime = time(0) - startTime; // elapsed time since opening
So, for this second part, your program will:
incoming
queue as in Part 1.line
queue, which is where Midshipman go after they've arrived.startTime
as above.incoming
queue, each time through doing:
incoming
queue has a Midshipman arrival time at or before the current time, print the message "Midshipman arrived at [insert time here]"
and enqueue the Midshipman info (at this point just the arrival time) in line
When you run the program with input file input61.txt, you should be able to watch the Midshipmen show up in "real time". Try making up some different input files to see the difference.
Let's add some realism to our model. Let's change the input file so that in addition to a Midshipman arrival time, you will include the time it takes to complete that Midshipman's haircut. A normal trim is quick, but a quality Marine Corps regulation haircut takes more time. We'll need this information if we're going to add barbers that actually service the people waiting in line
. For simplicity, there are three types of haircuts and they take times of 2, 3, or 4 minutes respectively (these are very efficient barbers). Use the file input62.txt for simulation.
Now each Midshipman has two pieces of data associated with him, an arrival time and a haircut duration. So if we want to store a "Midshipman" in a queue, what data type should we use? Hopefully, you came to the conclusion that we should write a class Midshipman
to store the information, where Midshipman has private
data members for arrival time and haircut duration. You should use public
member functions as to modify those private
data members as is appropriate to support our object-oriented goals. Modify your program from Part 2 so that:
incoming
and line
store objects of type Midshipman
incoming
queue, if the next Midshipman on the incoming
queue has an arrival time at or before the current time, print the message: "Midshipman arrived at [insert time here] with duration [insert duration here]"
and enqueue the Midshipman in line
For 5 points extra credit: overload the >>
operator to allow reading in an entire line from the file!
We're now going to add a barber, who will pull the first customer out of line
and give them their desired haircut. Being good modular/object-oriented programmers, you should be thinking "a class called Barber
sounds like a good idea." What information does a barber need? He needs to know whether or not he's working on a Midshipman, and if he is working on a Midshipman he'll have to know who that Midshipman is. Less obviously, he'll have to know when he started working with the current Midshipman, so he can tell when he's done! Another gentle reminder that these tidbits of your new class should be private
. When you've created your classBarber
, your Part 4 solution should:
incoming
queue as in Part 3.line
queue, which is where Midshipmen go after they've arrived, as in Part 3.startTime
as in Part 3.Barber
to do the barber shop's business.incoming
queue or the line
queue, each time through doing:
incoming
queue is not empty and if the next Midshipman on the incoming
queue has an arrival time at or before the current time, enqueue the Midshipman in line
line
and, if so, take that Midshipman.In this case, every time the barber takes on a new Midshipman, print out the number of seconds the Midshipman waited in line, i.e. the current time minus the Midshipman's arrival time, using this format:
"Midshipman arrived at [insert time here] and waited [insert wait time here]"
You can use the mma
program from the previous labs to compute the average time a Midshipman spent waiting in your line. Run using input62.txt.
Note: You should make a member function called "work" for class Barber that takes the queue of Midshipmen who've arrived and the current time.
The following test script is provided for your testing purposes: lab06_test.sh
It is highly recommended that before you submit the lab for a grade, you run the test script to verify that your program compiles and operates properly. This is also a variant of the program I will use to test the code you submit, so it behooves you to ensure you pass all the tests in the script provided!
To run the test script:
chmod 700 lab06_test.sh
./lab06_test.sh
Note: This test script also checks for proper run time, so there will be time delays on parts 2-4.
Submit your final cpp file to the submission website: submit.cs.usna.edu. The file should be named part#.cpp, where # is the number of the Part you were able to complete successfully. The file name will be my indicator for how far you got in the lab, so it's important that you follow proper naming conventions.
If you completed all the way through Part 4, your submission command would look like this:
~/bin/submit -c=SI221 -p=Lab06 part4.cpp
Do NOT submit any .o or executable files.