Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CSC 326 - Programming Assignment #4 - Banking Simulation This program is designe

ID: 3837397 • Letter: C

Question

CSC 326 - Programming Assignment #4 - Banking Simulation

This program is designed to get you intimately familiar with managing a queue … in the form of a bank-teller’s line! You will implement a queuing system, which in general, consists of servers (tellers) and a queue of objects to be served (customers).

The goal is to compute the average wait time - how long a customer waits until his transaction is performed by the bank-teller. We need to know 4 things to simulate a queuing system:

The number of events (2 in this case – arrivals and departures) and how they affect the system

The number of servers (start with one and add more for extra credit)

The distribution of arrival times (for example, a customer might arrive approximately every 5 minutes).

The expected service time (for example, 6 minutes- you can vary this by transaction type for extra credit)

Note: Changing the values of these parameters will affect the average wait time.

To simulate the passing of a unit of time (a minute for example) we increment the clock and run the simulation for a predetermined amount of time – say 100 minutes i.e. use a loop.

For each value of the clock (say 1-100) the following actions are processed (loop body):

If a customer arrives they enter the back of the line (the queue) and their arrival time is stored.

If a teller is free and someone is waiting, the customer at the front of the line moves to the teller and the service time is initialized (to 6 for example) and the customer’s total wait time can be determined.

If a customer is at the teller’s window, the time remaining for that customer to be serviced is decremented.

Average wait time = total wait time for all customers/total number of customers

Input: The number of servers (start with 1), the distribution of arrival times, the expected service time and the length of the simulation.

Include objects that represent customers (they keep track of how long they are waiting), tellers( they can be busy or free), the line ( a queue of customers) and timers(tellers decrement their timer and customers on line increment their timers).

Use a random number generator to determine the probability of a customer arriving during each loop iteration: (0.0 – no arrival to 1.0 – definitely arrives). For example, if a customer arrives on an average of every 5 minutes, the probability is 1 chance in 5 (or .2).

#include <cstdlib>

//rand() returns a value between 0 – RANDMAX

We want a range of 0.0 – 1.0: float(rand( ))/float (RANDMAX)
If the random number is between 0 and the arrival probability (.2 in our example) then a customer arrives, otherwise no customer arrives.

please it needs to be matching this implement.

a//@file queuea.h

#ifndef Queue_

#define Queue_

#include <iostream>

#include <stdexcept>

using namespace std;

template<class QueueItemType>

class Queue {

// FIFO objects

   public:

Queue(int MaxQueueSize = 10);

      Queue(Queue<QueueItemType> &Q);

~Queue() {delete [] queue;}

bool IsEmpty() const {return front == rear;}

bool IsFull() const {

          if ((rear + 1) % MaxSize == front) return 1;

          else return 0;};

QueueItemType Front() const; // return front element

      void enqueue(const QueueItemType& x);

void dequeue();

      int getMaxSize(){return MaxSize;};

   private:

int front; // one counterclockwise from first

int rear; // last element

int MaxSize; // size of array queue

QueueItemType *queue; // element array

};

#endif

template<class QueueItemType>

Queue<QueueItemType>::Queue(int MaxQueueSize)

{// Create an empty queue whose capacity

// is MaxQueueSize.

   MaxSize = MaxQueueSize + 1;

   queue = new QueueItemType[MaxSize];

   front = rear = 0;

}

template<class QueueItemType>

Queue<QueueItemType>::Queue(Queue<QueueItemType> &Q)

{

   int i;

   front = 0; rear = 0;

   MaxSize = Q.getMaxSize();

   queue = new QueueItemType[MaxSize];

   while(!Q.IsEmpty())

   {

       queue[rear] = Q.Front();

       Q.dequeue();

       rear = (rear + 1) % MaxSize;

   }

   for (i = front; i != rear; i = (i + 1) % MaxSize)

               Q.enqueue(queue[i]);

}

template<class QueueItemType>

QueueItemType Queue<QueueItemType>::Front() const

{// Return first element of queue. Throw

// OutOfBounds exception if the queue is empty.

   if (IsEmpty())

   throw logic_error("Queue exception: cannot retrieve from empty queue ");

   return queue[front];

}

template<class QueueItemType>

void Queue<QueueItemType>::enqueue(const QueueItemType& x)

{// Add x to the rear of the queue. Throw

// NoMem exception if the queue is full.

   if (IsFull()) throw logic_error("Queue exception: Queue is full ");

   queue[rear] = x;

   rear = (rear + 1) % MaxSize;

}

template<class QueueItemType>

void Queue<QueueItemType>::dequeue()

{// Delete first element and put in x. Throw

// OutOfBounds exception if the queue is empty.

   if (IsEmpty())

   throw logic_error("Queue exception: cannot delete from empty queue ");

    front = (front + 1) % MaxSize;

}

we use c++

Explanation / Answer

The Explanation of the code is given clearly in comments and also the detailed explanation about the concept is given below.

Explanation:
A driver file for the Queue class which simulates a Queue at a bank. User inputs a simulation time, transaction time, number of servers, and how often a new customer arrives. The driver then uses the Queue class to determine the average wait time of each customer, and how many customers remain in the line at the end of the simulation

Program.cpp

#include <iostream>
#include <iomanip>
using namespace std;
#include "Queue.h"
struct Teller_s {
bool active;
int time_At;
};
int main() {
//Min/Max values for user input
const int MIN_SIM_TIME = 0, MAX_SIM_TIME = 10000,
MIN_TRANS_TIME = 0, MAX_TRANS_TIME = 100,
MIN_NUM_SERV = 0, MAX_NUM_SERV = 10,
MIN_ARRIV_TIME = 0, MAX_ARRIV_TIME = 100;

char runAgain = 'Y'; //Set runAgain so the program runs
int sim_Time, trans_Time, num_Serv, arriv_Time; //User input variables
int i, c_Time; //Counters
int customers, wait_Time; //Total customers and integer for customer waiting time
while (runAgain != 'N') {
i = 0; c_Time = 0;
customers = 0; wait_Time = 0;

Queue bankQ; //Create queue object

cout << " ------------------------------------------"
<< " - Welcome to the Bank Simulation Program -"
<< " ------------------------------------------";

//Menu information
cout << " Please input the following data(Time in minutes): ";
cout << " Length of the Simulation: ";
cin >> sim_Time;
while (sim_Time <= MIN_SIM_TIME || sim_Time > MAX_SIM_TIME) {
cout << "Invalid input. Please re-enter: ";
cin >> sim_Time;
}
cout << "Average Transaction Time: ";
cin >> trans_Time;
while (trans_Time <= MIN_TRANS_TIME || trans_Time > MAX_TRANS_TIME) {
cout << "Invalid input. Please re-enter: ";
cin >> trans_Time;
}
cout << "Average Number of Servers: ";
cin >> num_Serv;
while (num_Serv <= MIN_NUM_SERV || num_Serv > MAX_NUM_SERV) {
cout << "Invalid input. Please re-enter: ";
cin >> num_Serv;
}
cout << "Average Time Between Arrivals: ";
cin >> arriv_Time;
while (arriv_Time <= MIN_ARRIV_TIME || arriv_Time > MAX_ARRIV_TIME) {
cout << "Invalid input. Please re-enter: ";
cin >> arriv_Time;
}

//Dynamically allocate an array for the teller structure
Teller_s * tellArray = new Teller_s[num_Serv];

//Set all tellers to empty
for (i = 0; i < num_Serv; i++) {
tellArray[i].active = false;
tellArray[i].time_At = trans_Time;
}

while (c_Time < sim_Time) { //Run until simulation time is reached
if (c_Time % arriv_Time == 0) { //Check if a customer should be enqueued
bankQ.enqueue();
customers++;
}
for (i = 0; i < num_Serv; i++) {
if (tellArray[i].active == false && bankQ.getSize() != 0) { //Dequeue if a teller is open
bankQ.dequeue();
tellArray[i].active = true;
tellArray[i].time_At = trans_Time;
}
}

for (i = 0; i < num_Serv; i++) {

if (tellArray[i].active == true) {
tellArray[i].time_At--; //Decrement time spent at teller
}
if (tellArray[i].time_At == 0) {
tellArray[i].active = false; //Set teller to open if time limit is reached
}
}

wait_Time += bankQ.getSize(); //Set wait time to persons left in queue
c_Time++;
}
//Output user input data
cout << " ---------------"
<< " - Data Output -"
<< " --------------- ";

cout << setw(31) << left << "Simulation Time: ";
cout << sim_Time << endl;

cout << setw(31) << left << "Average Transaction Time: ";
cout << trans_Time << endl;

cout << setw(31) << left << "Average Number of Servers: ";
cout << num_Serv << endl;

cout << setw(31) << left << "Average Time Between Arrivals: ";
cout << arriv_Time << endl << endl;

//Output calculated data
cout << "Average Total Wait Time: ";
cout << fixed << setprecision(2)
<< (float)wait_Time/customers;
cout << " Customers in line at end of simulation: "
<< bankQ.getSize() << endl;

//Ask to run again
cout << " Run the program again? (y/n): ";
cin >> runAgain;
runAgain = (char)toupper(runAgain);
while (runAgain != 'Y' && runAgain != 'N') {
cout << "Invalid Entry, please re-enter: ";
cin >> runAgain;
runAgain = (char)toupper(runAgain);
}
//Deallocate teller structure array
delete [] tellArray;
}
return 0;
}

Queue.h
Explanation :
A Header file for the Queue class which simulates a Queue in a bank
environment.
Operations are:
Queue(): Default constructor that sets first and last to null, and size to 0
enqueue(): Adds a person into the queue and increments mySize
dequeue(): Removes a person from the queue and decrements mySize
front(): Returns the object in the front of the queue
getSize(): Returns the current size of the queue
~Queue(): Deallocates the queue

#ifndef QUEUE
#define QUEUE
#include <iostream>
using namespace std;
typedef int ElementType;
class Queue {

public:

//Default constructor
Queue();

//Add to the back of the queue
void enqueue();

//Remove from the front of the queue
void dequeue();

//Returns the front of the queue
ElementType front();

//Return size of the queue
int getSize();

//Destructor
~Queue();

private:

class Node {

public:

ElementType data;
Node *next;
Node(ElementType i) { // Node constructor
data = i;
next = NULL;
}
}; //--- end of Node class

typedef Node *NodePointer;

Node *first;
Node *last;
int mySize;

}; //--- end of Queue class
#endif

Queue.cpp

#include <iostream>
using namespace std;
#include "Queue.h"
Queue::Queue() {
mySize = 0;
first = NULL;
last = NULL;
}
void Queue::enqueue() {
NodePointer nPtr = new Node(1);
NodePointer predPtr = first;

if (first == NULL) { //Insert if queue is empty
nPtr->next = first;
first = nPtr;
} else { //Insert into the queue at the end
while (predPtr->next) {
predPtr = predPtr->next;
}
nPtr->next = predPtr->next;
}
mySize++;
last = nPtr; //Set last to new pointer
}
void Queue::dequeue() {
if (first) {
NodePointer dPtr = first;
first = first->next; //Set first to the second node in the list
delete dPtr; //Delete the node that has been dequeued
}
mySize--;
}
ElementType Queue::front() {
if (first) {
NodePointer ptr = first;
return ptr->data;
}
}
int Queue::getSize() {
return mySize;
}
Queue::~Queue() {
if (first) {
//Deallocate all nodes in queue
NodePointer current = first;
NodePointer temp = first;

temp = current->next;
delete current;
current = temp;
}
}