Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will simulate the operation of a convenience store set in the world of The Simpsons television show (you don't need to know anything about

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
You will simulate the operation of a convenience store set in the world of The Simpsons television show (you don't need to know anything about this show to complete this project). During the simulation, customers will enter the store, collect some items, get in line for the next available checker, and then either buy the items, or steal them and rob the checker. There are two types of customers, those that buy items (shoppers), and those that steal items and money (robbers). Your program will: 1. Read in a set of customer records (customer name, \"shopper\" | \"robber", arrival time, number of items to buy/steal) from a provided input le, very similar to how you read the video records for projects 2 and 3. 2 Create a new Cust object for each customer records, and add it to an \"arrival\" priority queue. 3. Run a simulation based on the sequence of customers you read in step 1. 4. Generate output to a specied file while the simulation is running. The input is not ordered by arrival time, you will use priority queues to manage the ordering of events in the simulation. Customers will enter the store at a specified arrival time (simulation clock tick), spend time shopping for their items, get in line to pay/steal (these simulated robbers are very polite, they wait in line to rob the store), spend some time paying or stealing money, and then leave the store. When a customer performs an action (arrives in store, gets in checkout line, starts payingfstealing, nishes payingfstealing) your program will write a message to the specified output file documenting the customer's action and the current time. All items in the Kwik-E-Mart cost $3. All checkers start the simulation with $250 in their cash register. When a customer buys items from a checker, you will increase that checker's balance. When a checker is robbed, they lose all their money to the robber. Customers shop for exactly 2 clock ticks for each item they buy/steal. For example, if a customer arrives at clock-tick 10, and buys 15 items, the customer does not start the checkout I steal process until clock- tick = 40, that is, (10 + 15*2). Shopping time is dependent on the number of items (even for robbers). During the checkout process, shoppers must spend 1 clock-tick for each item they are buying. For example, if a shopper is removed from the checker queue and assigned a checker at clock-tick 28, and they are buying 7 items, they won't be done until clock-tick 35, that is (28 + (1 * 7)). Checkout time is dependent on the number of items. Robbers spend 7 clock-ticks for the checkout process. For example, if a robber is removed from the checker queue and assigned a checker at clock-tick 40, and they are stealing 17 items, they won't be done until clock-tick 47, that is, (40 + 7). Robbing time is not dependent on the number of items. Getting robbed is very stressful. Thus the checkers at the Kwick-E-Mart take a break after each time they are robbed. The length of the break (checker break duration) is a command line argument. Your PQueue class from Lab 8 is a priority queue of Cust pointers. It must only contain methods related to a priority queue, that is, the PQueue class mustn't know anything about this simulation. Implementation Requirements Command Line Arguments Your program must take four command line arguments: number of checkers (a positive integer), checker break duration (3 positive integer), name of input le (a C-string / char array), and name of output le (a C-string I char array). Note: You won't read from cin or write to cout for Project 5. Your program must validate that the number of checkers is 1 or greater, the checker's break duration is 0 or greater, and that the specied input file name and output le name can be opened. Details are given in the provided steps, below. Clock-ticks will be represented by a single integer starting at 1. This is just a relative time unit and not meant to represent any particular time of day or length of time. You Will use a loop and an integer clock variable to keep track of the simulation clock. Input File Content The customer records within the input file will be arbitrarily ordered with respect to arrival time and will have the formati Each set of fields will be on a separate line. For example: Homer shopper 3 9 Bart shopper 5 23 Lisa shopper 2 12 Maggie robber 27 1 Names will be a single string token Without any spaces. The customer type will always be either \"shopper\" or \"robber\". Arrival time will be an integer greater than 0. Number of items will be an integer greater than 0. Program Output A message must be written to the specified output file every time a customer: 1. enters the store 2. finishes shopping 3. starts checkout/stealing 4. finishes checkout/stealing An example of the expected output is shown below. The order of events must be ordered according to the clock. This is the output for the sample input given above, executed with 2 checkers and a break time of 0: 2: Lisa entered store 3: Homer entered store 5: Bart entered store 21: Homer done shopping 21: Homer started checkout with checker 0 26: Lisa done shopping 26: Lisa started checkout with checker 1 27: Maggie entered store 29: Maggie done shopping 30: Homer paid $27 for 9 items to checker 0 30: Maggie started checkout with checker 0 37: Maggie stole $277 and 1 item from checker 0 38: Lisa paid $36 for 12 items to checker 1 51: Bart done shopping 51: Bart started checkout with checker 0 74: Bart paid $69 for 23 items to checker 0 registers[0] = $69 registers [1] = $286 time = 75 Note that robbers "stole" "from" and shoppers "paid" "to." Also notice that item is sometimes plural (items) and sometimes singular (item) The entered/done shopping/started/done checkout messages must be printed in methods of class Cust. Create a print function for each type of message and pass the ostream and clock variables (and sometimes other information). For example, the following function outputs the "entered store" message to the given output stream: void Cust:outputEnteredStore(ostream &os, int clock) { assert(clock == m_arrival_time ); os #include #include #include using namespace std; #include "cust.h" #include "pqueue.h" int main(int argc, char* argyll) { 3) Define the following constants at the top of sim.cpp, just after the include statements: / Program constants const int COST PER ITEM = 3; const int SHOP_TIME_PER ITEM = 2; const int CHECKOUT_TIME_PER_ITEM = 1; const int ROB_TIME = 7; const int STARTING_CHECKER_CASH = 250; 4) Validate that the number of command line arguments passed in is correct. There must be exactly four user-provided command line arguments (review Command Line Arguments section above, if needed). Remember that argc will always be one larger than the number of user-specified command line argument strings (because argv[0] always contains the name of the program). So the expected value of argc is 5. If the number of command line arguments passed in is not correct, write the message "Error: invalid number of command line arguments." to cerr, followed by a newline character (endl), and exit the program with status 1 (call exit(1)). 5) Validate that the first user-specified command line argument (number of checkers) (argv[1]) is a legal integer, and if so, convert it to an int variable named checker_count using the atoi function. Refer back to your solution for Lab 7 - Ex 3 if you need a reminder on how to validate that a char array contains a legal integer, and using atoi to convert a char array to an integer. If the char array argyll] does not contain a valid integer, or if the integer value is less than 1, write the message "Error: invalid number of checkers specified.", to cerr, followed by a newline (endl), then exit the program with status 1. 6) Validate that the second user-specified command line argument (checker break length) (argv[2]) is a legal integer, and if so, convert it to an int variable named checker_break_length using the atoi function. If the char array argv(2] does not contain a valid integer, or if the integer value is less than 0, write the message "Error: invalid checker break duration specified.", to cerr, followed by a newline (endl), then exit the program with status 1. 7) Attempt to open the user-specified input file name (argv(3]), using the same techniques used in Lab 7. If you are unable to open the specified file name as an ifstream, write the message "Error: could not open input file .", to cerr, followed by a newline (endl), substituting the user specified file name for filename, then exit the program with status 1.8) Attempt to open the user-specified input file name (argv(4)), using the same techniques used in Lab 7. If you are unable to open the specified file name as an ofstream, write the message "Error: could not open output file .", to cerr, followed by a newline (endl), substituting the user specified file name for filename, then exit the program with status 1. 9) Declare a PQueue object named arrivalQueue on the runtime memory stack, like this: PQueue arrivalPQueue; 10) Create a function named read_input above your main function, declared as follows: void read_input(ifstream &ifile, PQueue &arrivalQueue) Note: We're using "pass-by reference" here not because ifile and arrivalQueue are "output parameters", but rather so that these two objects will be passed as pointers, but we can still treat them as objects (instead of pointers to objects) within this function. Implement this function to read the token-based input from the specified ifile, similar to how you read the video fields for project 2, except all of the input values for project 5 are tokens, so you don't need to use getline, and you will read from ifile instead of from cin. The input file contains a sequence of customer records, where each record consists of a name (string), an arrival time (int), a role specifier (string) "shopper" or "robber", and an item count (int). Consume each record using a while loop. For each customer record, create a new Cust object, and add that Cust object pointer on to the arrivalQueue using the enqueue method. Note: You will need to translate the "role specifier" string to true for "robber" and false for "shopper" when creating each new Cust object. When enqueing each Cust object pointer, use the specified arrival time as the priority value. 11) Call the read_input function from main, passing in the required parameters. 12) After read_input returns, arrivalPQueue is now populated with all the Cust object pointers needed to run the simulation. 13) Define a function named run_simulation above your main function, declared as follows: void run_simulation (int checker_count, PQueue &arrivalPQueue, int checker_break_duration, ostream &os) 14) Implement the body of function run_simulation as follows (note, this step has many sub-steps) a) Declare struct Checker as follows: struct Checker int m_cash; // current cash (whole dollars) in this checker's register int m_done_time; // clock tick checker will finish checking out current cust or will finish a break Cust *m_cust; // pointer to current customer being checked-out, or NULL if no cust being helped b) Allocate an array named checkers of checker_count Checker structs on the memory heap. c) Write a for loop to initialize each of the fields in each Checker struct in the checkers array. In each of the Checker structs in the checkers array, initialize m_done_time to 0, m_cash to STARTING_CHECKER_CASH and m_cust to NULL. d) Declare two additional priority queues on the memory stack, one for the shopping queue, and one for checker queue. e ) Create a local integer variable named customer_count to contain the number of customers to process. Initialize this variable to the number of elements currently in the arrivalPQueue.f) Declare an integer variable named clock to track the current simulation clock tick. g) Implement a for loop to run the simulation clock, as follows ('pseudo-code' steps are given inside of the for loop below). Use the provided constants from step 3 where needed! for (clock = 1; customer_count > 0; clock++) { While there is a customer on the arrival queue with a priority equal to the current clock tick dequeue them from the arrival queue I write the appropriate message to the specied output le 0 calculate what time this customer will be done shopping I place this customer on the shopping queue using the time they will be done shopping as the priority Note: Assuming that you implemented the PQueueIigetFirstPriority method has specied in lab 8, be mindful of the fact that this method returns the priority via it's output parameter, and returns true on success, or false if the queue is empty. While there is a customer on the shopping queue that is done shopping (has a priority equal to the current clock tick): o dequeue them from the shopping queue 0 write the appropriate message to the specied output le (see Program Output section). 0 place this customer on the checker queue (use 0 as the priority for all customers on the checker queue, this effectively makes the checker priority queue a simple queue, which is what we want) For each checker in the checker array that is done checking out a customer (m_cust != NULL && mgdone_time == clock) 1 adjust that checker's total cash according to the customer type (shopper or robber} write the appropriate message (\"paid' or \"stole" message) to the specified output file (see Program Output section). decrement customer_count if the customer was a robber, update this checker's m_done_time so they will end their break at the correct time delete this customer 0"... set the checker's customer pointer to NULL (indicates it has no customer) For each checker in the checkers array that is available (m_done_time

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions