// Main simulation program (client code)

 

#include <cstdlib>

 

#include "QueType.h"

 

using namespace std;

 

bool arrive(int timeBetween);

 

int main()

 

//Program simulates the wait of customers at

//a one teller drive in bank

 

{

 

   

 

  ItemType customer;

  ServerType server;

  string name;

  QueType waitQue(10);

  int clock;

  int timeLimit,processTime,timeBetween;

  int totalWaitTime=0, totalCustomers=0;

 

  //Prompt for simulation parameters

 

  cout << "Enter the amount of time you wish to simulate";

  cin >> timeLimit;

  cout << "Enter the average amount of time it takes for each process";

  cin >> processTime;

  cout << "Enter the average time between job arrivals";

  cin >> timeBetween;

 

 

 

  clock = 0;

  while (clock < timeLimit )

  {

      //update new time

      clock++;

    cout<< "Clock Time is now: "<<clock<<endl;

 

      //update the server to decrement transaction time

 

      server.updateServer();

 

      //Does a customer arrive?

     

     

    if (arrive(timeBetween))

            //input the customer information and add

            //the customer to the queue

      {

            cout << "Enter customer name";

            cin >> name;

            customer.Initialize(name,clock);

            waitQue.Enqueue(customer);

      }

 

      //Can we serve a new customer?

 

      if (server.isFree())

      {

            if (!waitQue.IsEmpty())

            {

            waitQue.Dequeue(customer);

            customer.Print();

            cout<<"was taken at: "<<clock<<endl;

 

            //add current customer wait time to totals

 

            totalWaitTime = totalWaitTime + (clock - customer.getstartTime());

            totalCustomers++;

           

            server.setServerBusy(processTime);

            }

 

      }

  }

 

  //Print totals

 

  cout<<"The average wait time was:   ";

  cout<<totalWaitTime / totalCustomers;

 

  return 0;

}

 

bool arrive(int timeBetween)

{

 

    //Does a customer arrive?

      //Let us assume that it is equally likely for the customer to arrive

      //in one of the timeBetween minutes

    int randomInt;

 

      randomInt = rand();

      if (static_cast<float>(randomInt)/static_cast<float>(RAND_MAX) < 1/float(timeBetween))

            return 1;

      else return 0;

}

 

//server/client definitions

 

/ ItemType.h holds item and server class definitions

// they could be in two files

#include <string>

#include <iostream>

using namespace std;

 

class ItemType

{

public:

      void Initialize(string, int);

      void Print();

      int getstartTime();

private:

 

      string name;

      int starttime;

};

 

 

class ServerType

{

public:

      ServerType();

      void updateServer();

      bool isFree();

      void setServerBusy(int);

 

private:

      bool busy;

      int processedTime;

};

 

 

 

//server/client functions

 

//File that holds the ItemType functions and

//Server functions

//They can be kept in two seperate files, if you

//wish: ItemType.cpp and Server.cpp

 

#include "ItemType.h"

void ItemType::Initialize(string n, int x)

 

//Post:  both values of the item are set

 

{

      name = n;

      starttime = x;

}

 

void ItemType::Print()

{

      cout<<"Customer name: "<<name;

}

 

int ItemType::getstartTime()

{

      return starttime;

}

 

ServerType::ServerType()

{

      busy = false;

}

 

void ServerType::setServerBusy(int processtime)

//Post:  Sets server to busy

//       and sets processedTime to the total time

//       needed for processing

{

      busy = true;

      processedTime = processtime;

}

 

void ServerType::updateServer()

//Function:  checks to see if the server is busy

//           if it is, decrements processedTime

//           if process is done, sets busy to false

{

      if (busy)

            processedTime--;

      if (processedTime == 0)

            busy = false;

}

 

bool ServerType::isFree()

{

  return (!busy);

}