In: Computer Science
**New code needed! Please do not reference code that has already been answered for this question as that code contains errors***
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:
You should submit your C++ source code along with screen shots of sample runs that showed successful runs for the above steps.
code in c++ with comments for better understanding.
#include<bits/stdc++.h>
using namespace std;
class Desk{
public:
int current_time;
Desk(){
current_time=0;
}
int getWaitingTime(int arrivalTime){
//return the waiting time at this desk for a given arrival time
return max(0, current_time-arrivalTime);
}
int process(int serviceTime){
//process request at this desk and return the current time after service
current_time+=serviceTime;
return current_time;
}
};
class ServiceRequest{
public:
static int max_id;
int id;
int arrivalTime;
int service_time;
int priority;// 0 for low, 1 for medium, 2 for high
int waiting_time;
int deskServedAt;//contains the desk at which this service was served
ServiceRequest(){
// set request id
id=(++max_id);
//assuming all request comes at t=0
arrivalTime=0;
//create a service with random priority and service time
service_time = rand()%4+5;//generates service time between 5-8
///assign random priority
priority=rand()%3;
}
//resets the counter for requests
static void reset(){
max_id=0;
}
void fulfillRequest(int timeFulfilledAt, int desk){
waiting_time=timeFulfilledAt-service_time-arrivalTime;
deskServedAt=desk;
}
void print(){
cout<<"Request: #"<<id<<endl<<"Priority: ";
switch(priority){
case 0: cout<<"LOW";
break;
case 1: cout<<"MEDIUM";
break;
case 2: cout<<"HIGH";
break;
}
cout<<endl<<"Service Time: "<<service_time<<" minutes"<<endl;
cout<<"Service at Station: #"<<deskServedAt<<endl;
cout<<"Waiting Time: "<<waiting_time<<" minutes"<<endl<<endl;
}
};
class ServiceDesks{
public:
vector<Desk> desks;
ServiceDesks(int deskCount): desks(deskCount){ServiceRequest::reset();}
// function to service a request
void serviceWithMinimumTime(ServiceRequest &req){
int arrival_time=req.arrivalTime;
int min_desk=0;
int min_time=desks[min_desk].getWaitingTime(arrival_time);
//get the desk which will result to lowest waiting time
for(int i=1;i<desks.size();i++){
if(min_time>desks[i].getWaitingTime(arrival_time)){
min_time = desks[i].getWaitingTime(arrival_time);
min_desk=i;
}
}
//this service will be served at desk min_desk
int timeFulfilledAt = desks[min_desk].process(req.service_time);
req.fulfillRequest(timeFulfilledAt, min_desk+1);
//print request details
req.print();
}
};
// set max_id
int ServiceRequest::max_id = 0;
void doSimulation(int numOfRequests, int numberOfStations){
//scenario one 100 requests with one service station
int k=1;
vector<ServiceRequest> arr(numOfRequests);
//do simulation
ServiceDesks d(numberOfStations);//with one serving desk
//find average waiting time for each priority
vector<float> averages(3,0);
vector<int> count(3,0);
for(ServiceRequest req:arr){
d.serviceWithMinimumTime(req);
averages[req.priority]+=req.waiting_time;
count[req.priority]++;
}
cout<<"Average Waiting time for Priority HIGH: "<<averages[2]/count[2]<<endl;
cout<<"Average Waiting time for Priority MEDIUM: "<<averages[1]/count[1]<<endl;
cout<<"Average Waiting time for Priority LOW: "<<averages[0]/count[0]<<endl;
}
int main()
{
srand(time(NULL));
cout<<setprecision(2)<<fixed;
doSimulation(100, 1);
doSimulation(100, 2);
}
code screenshot
Sample Output Screenshots