Working on a simulation which closely resembles waiting in a line at a bank. Program runs the simulation based on the user's inputs of # of servers, simulation time, average time between arrival, and transaction time. Here's what I have so far:
/*~~~prog3.cpp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| |
| Author: Megan MacDonald |
| Date Completed: April 3, 2009 |
| Class: CSC 2110, section 002 |
| Tennessee Technological University |
| |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <iostream>
#include <string>
using namespace std;
#include "Control.h"
#include "server.h"
#include "Queue_of_Jobs.h"
#define Max_Server 10
#define Min_Server 0
#define Max_simTime 10000
#define Min_simTime 0
#define Max_transTime 100
#define Min_transTime 0
#define Max_arrivalTime 100
#define Min_arrivalTime 0
int main()
{
int servers, simTime, transTime, arrivalTime;
//get parameters
cout<<"Number of Servers? ";
cin>>servers;
cout << endl;
while (servers > Max_Server || servers < Min_Server)
{
cout << "The number of servers you entered is outside of scope. " << endl
<< "Please enter a number between 0 & 10: ";
cin >> servers;
cout << endl;
}
cout<<"Simulation Time? ";
cin>>simTime;
cout << endl;
while (simTime > Max_simTime || simTime < Min_simTime)
{
cout << "The simulation time you entered is outside of scope. " << endl
<< "Please enter a number between 0 & 10000: ";
cin >> simTime;
cout << endl;
}
cout<<"Transition Time? ";
cin>>transTime;
cout << endl;
while (transTime > Max_transTime || transTime < Min_transTime)
{
cout << "The transaction time you entered is outside of scope. " << endl
<< "Please enter a number between 0 & 100: ";
cin >> transTime;
cout << endl;
}
cout<<"Arrival Time? ";
cin>>arrivalTime;
cout << endl;
while (arrivalTime > Max_arrivalTime || arrivalTime < Min_arrivalTime)
{
cout << "The arrival time you entered is outside of scope. " << endl
<<"Please enter a number between 0 & 100: ";
cin >> arrivalTime;
cout << endl;
}
cout<<servers<<endl;
cout<<simTime<<endl;
cout<<transTime<<endl;
cout<<arrivalTime<<endl;
Control c;
Server s;
Queue_of_Jobs q;
Job temp;
int clock;
int t; //used for random number
int tempTime; //used for time of customer in front
int waitTime; //used to calculate the job's wait time
s.insert(servers);
s.display();
cout << endl << endl;
for (clock=0; clock<simTime; clock++)
{
t=rand()%5;
if(q.checkForNewJob(t))
{
temp.inTime = clock;
q.enqueue(temp);
cout << "Customer enqueued" << endl;
}
else
cout << "Customer did not arrive" << endl;
if (q.checkLine())
cout << "There is a customer waiting in line" << endl;
int tempFront=q.front();
if (q.dequeue())
{
waitTime = clock - tempFront;
cout << "Wait Time of Customer is: " << waitTime << endl;
cout << "~~~~~A Customer was DeQueued~~~" << endl;
}
c.totalWait(waitTime);
//check servers
//update servers
cout << "*************************************" << endl;
}
}
/*~~~server.h~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| |
| Author: Megan MacDonald |
| Date Completed: April 3, 2009 |
| Class: CSC 2110, section 002 |
| Tennessee Technological University |
| |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <iostream>
#include <string>
using namespace std;
typedef int ServerType;
class Server
{
private:
int mySize;
class Node
{
public:
int data;
Node * next;
Node(int ident){
data = ident;
next = NULL;
}
};
typedef Node * NodePointer;
NodePointer first;
public:
Server();
bool isAvailable();
void engage();
void disEngage();
void insert(int numServers);
void display();
};
/*~~~server.cpp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| |
| Author: Megan MacDonald |
| Date Completed: April 3, 2009 |
| Class: CSC 2110, section 002 |
| Tennessee Technological University |
| |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <iostream>
using namespace std;
#include "server.h"
Server::Server()
{
first = NULL;
mySize = 0;
}
bool Server::isAvailable()
{
}
void Server::engage()
{
}
void Server::disEngage()
{
}
void Server::insert(int numServers)
{
cout << "MySize: " << mySize << endl;
int index;
int i;
for (index=0; index<numServers; index++)
{
NodePointer nPtr = new Node(index);
if (index == 0)
{
nPtr->next = first;
first = nPtr;
}
else
{
NodePointer predPtr = first;
for (i=0; i==mySize; i++)
{
predPtr = predPtr->next;
}
nPtr->next = predPtr->next;
predPtr->next = nPtr;
}
mySize++;
}
cout << "MySize: " << mySize << endl;
}
void Server::display()
{
NodePointer ptr;
ptr = first;
while (ptr != NULL)
{
cout << ptr->data;
ptr = ptr->next;
}
}
/*~~~Queue_of_Jobs.h~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| |
| Author: Megan MacDonald |
| Date Completed: April 3, 2009 |
| Class: CSC 2110, section 002 |
| Tennessee Technological University |
| |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <iostream>
#include <string>
using namespace std;
typedef struct Job ElementType;
#define capacity 100
struct Job
{
int inTime;
};
class Queue_of_Jobs
{
private:
ElementType myArray[capacity];
int mySize;
int frontOfQueue;
int back;
int count;
public:
Queue_of_Jobs();
bool enqueue(ElementType newJob);
bool dequeue();
int front();
int size();
bool checkForNewJob(int number);
bool checkLine();
};
/*~~~job.cpp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| |
| Author: Megan MacDonald |
| Date Completed: April 3, 2009 |
| Class: CSC 2110, section 002 |
| Tennessee Technological University |
| |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "Queue_of_Jobs.h"
Queue_of_Jobs::Queue_of_Jobs()
{
mySize = 0;
frontOfQueue = 0;
back = 0;
count = 0;
}
bool Queue_of_Jobs::enqueue(ElementType newJob)
{
if (mySize == capacity)
{
cout << "Queue if full." << endl;
return false;
}
else
{
myArray[back] = newJob;
back = (back + 1)%capacity;
count++;
mySize++;
return true;
}
}
bool Queue_of_Jobs::dequeue()
{
if (front() == 0) //Queue is empty
{
cout << "Queue was empty - could not remove a customer." << endl;
return false;
}
else
{
ElementType j = myArray[frontOfQueue];
if (frontOfQueue == capacity)
{
frontOfQueue = 0;
}
frontOfQueue = frontOfQueue + 1;
count--;
mySize--;
cout << "Front of queue: " << frontOfQueue << endl;
return true;
}
}
int Queue_of_Jobs::front()
{
if (checkLine())
{
ElementType j = myArray[frontOfQueue];
return (j.inTime);
}
else
{
//Queue is empty
return 0;
}
}
int Queue_of_Jobs::size()
{
mySize = (back - frontOfQueue)%capacity;
return mySize;
}
bool Queue_of_Jobs::checkForNewJob(int number)
{
if (number%2)
return true;
else
return false;
}
bool Queue_of_Jobs::checkLine()
{
if (count >= 1)
return true;
else
return false;
}
/*~~~Control.h~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| |
| Author: Megan MacDonald |
| Date Completed: April 3, 2009 |
| Class: CSC 2110, section 002 |
| Tennessee Technological University |
| |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <iostream>
class Control
{
private:
int averageWaitTime;
int totalWaitTime;
public:
Control();
int totalWait(int waittime);
int averageWait(int numJobs);
void display();
};
/*~~~Control.h~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| |
| Author: Megan MacDonald |
| Date Completed: April 3, 2009 |
| Class: CSC 2110, section 002 |
| Tennessee Technological University |
| |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <iostream>
using namespace std;
#include "Control.h"
Control::Control()
{
totalWaitTime = 0;
averageWaitTime = 0;
}
int Control::totalWait(int waittime)
{
totalWaitTime = totalWaitTime + waittime;
}
int Control::averageWait(int numJobs)
{
averageWaitTime = totalWaitTime / numJobs;
}
void Control::display()
{
cout << "Total Wait Time: " << totalWaitTime << endl;
cout << "Average Wait Time was: " << averageWaitTime << endl;
}