Bank Queue Simulation

Hi, I'm trying to learn more about classes/vectors/queues, so I've been playing around with code I find online. The code in question is from this site:
http://stdcxx.apache.org/doc/stdlibug/10-3.html

I tried to adjust the code so that multiple customers can come in per minute. I changed the process time in class Customer to constant 1. I also added another for loop in main() and marked where i made the change.I think whats happening is that all 10 customers that walk in go straight to the 1st teller because they are Customer(t) for the same t. how can I change this so that each customer goes to a different teller? Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <vector>
#include <queue>
#include <cstdlib>
using namespace std;

class Customer
{
public:

  int arrivalTime;
  int processTime;

  Customer (int at = 0)
    : arrivalTime (at),
      processTime (1) {}

  // are we done with our transaction?
  bool done () {
    return --processTime < 0;
  }

  // order by arrival time
  bool operator< (const Customer& c) const {
    return arrivalTime < c.arrivalTime;
  }
};

class Teller
{
public:
  Teller (): free (true) { }

  bool isFree () {   // are we free to service new customer?
    if (free)
      return true;
    if (customer.done())
      free = true;
    return free;
  }

  // start serving new customer

  void addCustomer (const Customer &c) {
    customer = c;
    free = false;
  }

private:

  bool     free;
  Customer customer;
};

int main ()
{
  const int numberOfTellers = 2;
  const int numberOfMinutes = 10;
  const int custPerMin = 10;
  double totalWait          = 0;
  int numberOfCustomers     = 0;

  vector<Teller> teller (numberOfTellers);
  queue<Customer> line;

  for (int t = 0; t < numberOfMinutes; t++)
{
    for (int j = 0; j < custPerMin; j++) // 10 Customers per minute
    {
        line.push (Customer (t));
        for (int i = 0; i < numberOfTellers; i++)
        {
            if (teller[i].isFree () && !line.empty ())
                {
                Customer& frontCustomer = line.front ();
                numberOfCustomers++;
                totalWait += t - frontCustomer.arrivalTime;
                teller[i].addCustomer (frontCustomer);
                line.pop ();
                }
        }
    }
  }

  cout << "average wait: " << (totalWait / numberOfCustomers) << endl;
}
Topic archived. No new replies allowed.