Queues - Brigade Barbershop Simulation

Intro

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.

Part 1 (20 points possible): Queue Basics

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:

  1. Reads Midshipman arrival times from the file input61.txt by using file redirection on the command line, e.g.
    ./part1 < input61.txt
    OR
    cat input61.txt | ./part1
  2. Stores Midshipman arrival times in a queue incoming
  3. Prints Midshipman arrival times from the queue - just to verify that you've read and stored them correctly

Printing the contents of the queue should give you the same order as they were put in - not reversed like a stack!

Part 2 (20 points possible): "Does anyone really know what time it is?" (Chicago, 1970)

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:

  1. Enqueue Midshipman arrival times in the incoming queue as in Part 1.
  2. Create the line queue, which is where Midshipman go after they've arrived.
  3. Open the barber shop by setting startTime as above.
  4. Set up a loop that loops for as long as there is something in the incoming queue, each time through doing:
    • compute the current time
    • if the next element on the 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.

Part 3 (30 points possible): Getting motivated, chapter 1

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:

For 5 points extra credit: overload the >> operator to allow reading in an entire line from the file!

Part 4 (30 points possible): Getting motivated, chapter 2

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 Barbersounds 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:

  1. Enqueue Midshipman arrival times in the incoming queue as in Part 3.
  2. Create the line queue, which is where Midshipmen go after they've arrived, as in Part 3.
  3. Open the barber shop by setting startTime as in Part 3.
  4. Create a Barber to do the barber shop's business.
  5. Set up a loop that loops for as long as there is something in the incoming queue or the line queue, each time through doing:
    1. compute the current time
    2. if the 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
    3. have the Barber do his work, which means
      • if the barber is busy he'll need to check whether he's worked with this Midshipman long enough to be finished.
      • if the barber is not busy he'll have to see if there's a Midshipman he can take out of 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.

Testing

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:

  1. Download the file in your VM, and place it in the same directory as your lab files (including TradQueue.h).
  2. Open the terminal and cd to your lab directory
  3. Ensure that the test script is executable: chmod 700 lab06_test.sh
  4. Run the script: ./lab06_test.sh

Note: This test script also checks for proper run time, so there will be time delays on parts 2-4.

Deliverables

Due: 2359, Monday 15 Oct 2018

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.