/****************************************************** ** Name: ** Filename: WashingDrv.cpp ** Project #: Data Structures And Other Objects Using C++ 2nd Edition (Micheal Main & Walter Savich) ** Project Description: Develop a test driver that implements the class Car Wash. ** Output: The simulation of cars passing through a car wash based on user inputed criteria of the operating parameters of the car washing machine. ** Input: Time the car wash takes in minutes: Time between car arrivals in minutes: Total time to run car wash in minutes: ** Algorithm: 1. Instruct user to input time operating parameters for car wash sim. 2. Create function to implement washing class and pass user operating parameters into car wash function. 3. Display results of car wash simulation ******************************************************/ // Include files #include // Used For cin, cout #include // Used For getch(); #include #include #include "washing.h" using namespace std; using namespace main_savitch_8A; // Global Type Declarations // Function Prototypes void instruct (void); void pause (); bool toQuit(); void car_wash_simulate // Implements CarWash Using Queue and washing.h (unsigned int wash_time, double arrival_prob, unsigned int total_time); //Global Variables - should not be used without good reason. int main () { // Declaration section bool done = false; // User Decides To Quit int timeWash = 0; // Time Wash Takes int timeBetweenCars = 0; // Time Between Cars int timeTotal = 0; // Total Time To Run int secs = 60; // Convert Minutes Into Seconds // Executable section instruct (); while (!done) // While Not False Repeat { cout << "Please enter the time the wash takes in minutes: "; cin >> timeWash; timeWash = timeWash * secs; cout << "Please enter the time in minutes between car arrivals: "; cin >> timeBetweenCars; timeBetweenCars = timeBetweenCars * secs; cout << "Please enter the total time in minutes you want to run " << "the sim: "; cin >> timeTotal; timeTotal = timeTotal * secs; car_wash_simulate(timeWash, 1.0/timeBetweenCars, timeTotal); done = toQuit(); // Calls toQuit() Function To Continue Or End Prog. } pause (); return 0; } void car_wash_simulate (unsigned int wash_time, double arrival_prob, unsigned int total_time) // Library facilities used: iostream, queue, washing.h { queue arrival_times; // Waiting customer’s time stamps unsigned int next; // A value taken from the queue bool_source arrival(arrival_prob); washer machine(wash_time); averager wait_times; unsigned int current_second; // Write the parameters to cout. cout << "Seconds to wash one car: " << wash_time << endl; cout << "Probability of customer arrival during a second: "; cout << arrival_prob << endl; cout << "Total simulation seconds: " << total_time << endl; for (current_second = 1; current_second <= total_time; ++current_second) { // Simulate the passage of one second of time. // Check whether a new customer has arrived. if (arrival.query( )) arrival_times.push(current_second); // Check whether we can start washing another car. if ((!machine.is_busy( )) && (!arrival_times.empty( ))) { next = arrival_times.front( ); arrival_times.pop( ); wait_times.next_number(current_second - next); machine.start_washing( ); } // Tell the washer to simulate the passage of one second. machine.one_second( ); } // Write the summary information about the simulation. cout << "Customers served: " << wait_times.how_many_numbers( ) << endl; if (wait_times.how_many_numbers( ) > 0) cout << "Average wait: " << wait_times.average( ) << " sec" << endl; } bool toQuit () { bool cont = true; // While Will Repeat Unless cont = false bool toReturn = true; // Default Value Of True Unless Changed char response; // Users Decision To Continue Or End Prog. while (cont) { cout <<"\nWould you like to continue? (Y/N) "; cin >> response; cout <<"\n"; if ((response != 'Y') && (response != 'y') && (response != 'N') && (response != 'n')) { // Error Because Did Not Use Y or N cout <<"Invalid entry. Try again" << endl; } if (response == 'Y' || response == 'y') { cont = false; // User Continues toReturn = false; } else if (response == 'N' || response == 'n') { cont = false; // Stop Function toReturn = true; } }//End While return toReturn; // Returns True Or False To End } // End Quit void instruct (void) { // Declaration section cout << "This program will simulate a car wash where wash_time is the " << "number of seconds\nneeded to wash one car, arrival_prob is the " << "probability of a customer arriving\nin any second, and total_time " << "is the total number of seconds for the simulation." << "----------------------------------------------------" << "---------------------------" << endl; // Executable section } // End Instruct void pause () { // Declaration section // Executable section cout << "\nPress any key to continue..."; getch(); cout << "\r"; cout << " "; cout << "\r"; } // End Pause /* Program Output This program will simulate a car wash where wash_time is the number of seconds needed to wash one car, arrival_prob is the probability of a customer arriving in any second, and total_time is the total number of seconds for the simulation. ------------------------------------------------------------------------------- Please enter the time the wash takes in minutes: 3 Please enter the time in minutes between car arrivals: 5 Please enter the total time in minutes you want to run the sim: 25 Seconds to wash one car: 180 Probability of customer arrival during a second: 0.00333333 Total simulation seconds: 1500 Customers served: 6 Average wait: 29 sec Would you like to continue? (Y/N) */