In: Computer Science
Write a C++ program to simulate a service desk. This service desk should be able to service customers that can have one of three different priorities (high, medium, and low). The duration for any customer is a random number (between 5 minutes and 8 minutes). You need to write a program that will do the following:
Tips: Use a loop that will process 100 times, se each service request as an index for the queue, declare random numbers to generate priorities and service times, 5-8 for service, 1-3 for priority, generate 2 deques: one for service, 1 for priority, use push_front(high) and push_front(low) to add values to the deque, for medium priority use size()/2 to find the missle position of the deque using insert(name.insert(position, value).
main.cpp
------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <string>
#include <array>
#include <vector>
#include "Priority.h"
#include "Service.h"
#include "Customer.h"
using namespace std;
const string APP_NAME = "Service Desk Simulation";
const array<string, 3> MENU_OPTIONS = {
"Simulate 1 Service Station",
"Simulate 2 Services Station",
"End Simulation"
};
const int CUSTOMERS = 100;
const int SERVICE_MIN = 5;
const int SERVICE_MAX = 8;
//Print main menu options
void printMenuOptions() {
cout << endl << "---------------------------" << endl;
cout << APP_NAME << endl;
cout << "---------------------------" << endl;
for (int i=0; i<MENU_OPTIONS.size(); i++) {
cout << i+1 << ". " << MENU_OPTIONS[i] << endl;
}
cout << endl << "Select an option: ";
}
//Returns random service duration between 5 and 8
double getRandomServiceDuration() {
return 5 + (static_cast<double>(rand() % (8 - 5 + 1)));
}
//Returns random service priority in {high, medium, low}
Priority getRandomServicePriority() {
Priority p;
int r = 1 + (static_cast<int>(rand() % (3 - 1 + 1)));
switch (r) {
case 1:
p = Priority::high;
break;
case 2:
p = Priority::medium;
break;
case 3:
default:
p = Priority::low;
break;
}
return p;
}
void simulate(int numStations, vector<Service*> services)
{
Service* station1;
Service* station2;
int total = 0;
int waiting1 = 0;
int waiting2 = 0;
int totCustHigh = 0;
int totCustMedium = 0;
int totCustLow = 0;
double avgTimeHigh = 0;
double avgTimeMedium = 0;
double avgTimeLow = 0;
double totTimeHigh = 0;
double totTimeMedium = 0;
double totTimeLow = 0;
for (std::vector<Service*>::iterator it = services.begin();
it != services.end(); ++it) {
station1 = *(it);
total++;
cout << "Customer #" << total << " Waiting Time @
Station #1:" << waiting1 << " minutes" <<
endl;
waiting1 += station1->getDuration();
switch (station1->getPriority()) {
case Priority::high:
totCustHigh++;
totTimeHigh += station1->getDuration();
avgTimeHigh = totTimeHigh / totCustHigh;
break;
case Priority::medium:
totCustMedium++;
totTimeMedium += station1->getDuration();
avgTimeMedium = totTimeMedium / totCustMedium;
break;
case Priority::low:
totCustLow++;
totTimeLow += station1->getDuration();
avgTimeLow = totTimeLow / totCustLow;
break;
}
if (numStations == 2) {
++it;
station2 = *(it);
total++;
cout << "Customer #" << total << " Waiting Time @
Station #2:" << waiting2 << " minutes" <<
endl;
waiting2 += station2->getDuration();
switch (station2->getPriority()) {
case Priority::high:
totCustHigh++;
totTimeHigh += station2->getDuration();
avgTimeHigh = totTimeHigh / totCustHigh;
break;
case Priority::medium:
totCustMedium++;
totTimeMedium += station2->getDuration();
avgTimeMedium = totTimeMedium / totCustMedium;
break;
case Priority::low:
totCustLow++;
totTimeLow += station2->getDuration();
avgTimeLow = totTimeLow / totCustLow;
break;
}
}
}
cout << endl;
cout << "Customers served:" << total << " of 100" << endl;
cout << "[Customers by Priority] High:" << totCustHigh;
cout << " Medium:" << totCustMedium;
cout << " Low:" << totCustLow << endl;
cout << "[Average Waiting Time] High:" << avgTimeHigh
<< " minutes";
cout << " Medium:" << avgTimeMedium << " minutes";
cout << " Low:" << avgTimeLow << " minutes"
<< endl;
}
int main(int argc, char** argv) {
vector<Service*> services;
bool exitApp = false;
int choice = 0;
for (int i=0; i<CUSTOMERS; i++) {
Service * randService = new Service(getRandomServicePriority(), getRandomServiceDuration());
randService->setCustomer(new Customer());
services.push_back(randService);
}
while (!exitApp) {
printMenuOptions();
cin >> choice;
switch (choice) {
case 1:
simulate(1, services);
break;
case 2:
simulate(2, services);
break;
case 3:
exitApp = true;
break;
default:
cout << endl << "Incorrect choice. Please try again." << endl;
}
}
return 0;
}
------------------------------------------------------------------------------------------------------
Customer.cpp
-------------------------------------------
#include "Customer.h"
Customer::Customer() {
name = "";
}
Customer::Customer(const Customer& orig) {
}
Customer::~Customer() {
}
---------------------------------------------------------------------------------------------------------
Customer.h
--------------------------------------------------
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <string>
class Customer {
public:
Customer();
Customer(const Customer& orig);
virtual ~Customer();
private:
std::string name;
};
#endif
-------------------------------------------------------------------------------------------------
Service.cpp
------------------------------------------------------------
#include "Service.h"
#include "Priority.h"
Service::Service(Priority _priority, double _duration) {
priority = _priority;
duration = _duration;
}
Service::Service(const Service& orig) {
}
Service::~Service() {
}
Priority Service::getPriority() {
return priority;
}
double Service::getDuration() {
return duration;
}
void Service::setCustomer(Customer* _customer) {
customer = _customer;
}
std::string Service::getPriorityDescription() {
std::string description;
switch (priority) {
case Priority::high:
description = "High";
break;
case Priority::medium:
description = "Medium";
break;
case Priority::low:
description = "Low";
break;
default:
description = "";
break;
}
return description;
}
-------------------------------------------------------------------------------------------
Service.h
-------------------------------------------------
#ifndef SERVICE_H
#define SERVICE_H
#include <string>
#include "Priority.h"
#include "Customer.h"
class Service {
public:
Service(Priority _priority, double _duration);
Service(const Service& orig);
virtual ~Service();
Priority getPriority();
std::string getPriorityDescription();
double getDuration();
Customer* getCustomer();
void setCustomer(Customer* _customer);
private:
Priority priority;
double duration;
Customer* customer;
};
#endif
--------------------------------------------------------------------------------
Priority.h
--------------------------------------------
#ifndef PRIORITY_H
#define PRIORITY_H
enum class Priority { high, medium, low };
#endif