Use a linked list queue to simulate customers waiting in line

This is my first program working with queues and I have no idea where to start. Fair warning this is a long program. My main question is with the functions in the main program near the bottom of this post. I posted this question in order so you know what everything means.
I read from a "customerList.txt" file in the format of id<tab>arrivalTime<tab>serviceTime:
1
2
3
4
5
1          0         7
2          0         3
3          1         2
4          3         1
5          3         4


I have 3 header files and one main program. I have a Queue.h, with normal queue member functions. I have a Cashier.h structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct Cashier
{
    int custid;
    int endTime;
    bool busy;
    Cashier();
};
Cashier::Cashier()
{
    custid = 0;
    endTime = 0;
    busy = false;
}


And I have a Customer.h structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct Customer
{
    int id;
    int arrivalTime;
    int serviceTime;
    Customer();
};
Customer::Customer()
{
    id = 0;
    arrivalTime = 0;
    serviceTime = 0;
}


Now I have the Simulation.cpp program. This is where I am lost. In the main() I read the customers from the "customers.txt" file into an array of Customers (no more than 50 customer records). I add the customer to the queue at the appropriate time. Each of the three cashiers will process customers from the same queue. I have to use 3 functions in the Simulation.cpp. I have a general idea what to do but I don't know how to implement it.

fillCustomerQueue: this function reads the text file and fills the customer queue when a correct time comes. For example, all customers with an arriving time the same as the current time should be enqueued. the prototype is:
 
void fillCustomerQueue(ifstream &custData, LLQueue<Customer> &custList, Customer &custIn, int currentTime);


processCustomers: for all cashiers, if not busy, should serve a customer. If busy and time for a job to end, end it. This is the prototype:
 
void processCustomers(LLQueue<Customer> &custList, Cashier serv[], const int currectTime);


isDone: returns true if all cashiers are no longer busy with customers. Check the current job statuses of each cashier with the current time to determine if they should still be busy. The prototype is:
 
bool testCashier(Cashier serv[], LLQueue<Customer> &custList);


In main() I have the following. I'm still working on this, I'm just sharing it so you know what the declarations look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
   Customer carray[50];    //all customers
   LLQueue<Customer> custList;    //customer arrival time
   Cashier cashiers[3];

   ifstream file;
   file.open("customerList.txt");

   if (file.fail())
      cout << "Can not open file.\n";
   else
   {
      int i = 0;
      while (file >> carray[i].id >> carray[i].arrivalTime >> carray[i].serviceTime)
      i++;
   }
   file.close();
   return 0;
}


The function are what I really need help with. This is what I have so far in fillCustomerQueue. I don't know where to go from here:
1
2
3
4
5
6
7
void fillCustomerQueue(ifstream &custData, LLQueue<Customer> &custList, Customer &custIn, int currentTime)
{
   Customer tempArray[50];
   int i = 0;
   while (tempArray[i].arrivalTime == currentTime)
        custList.enqueue();
}


For isDone I just have a pseudocode of what the function should return.
1
2
3
4
bool isDone(Cashier serv[], LLQueue<Customer> &custList)
{
     all cashiers not busy and && queue is empty == true
}


For processCustomers I also have a pseudocode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void processCustomers(LLQueue<Customer> &custList, Cashier serv[], const int currentTime)
{
   for(all cashiers)
   {
        if(cashier is busy)
             cout << still busy
        if(cashier is busy && endTime == currentTime)
              cashier = not busy
        if(cashier not busy)
              delete queue from custList
              set cashier's info
        if(cashier not busy && custList is empty)
              cout << all cashier are free
     }
} 
Last edited on
You need to sort the customers array by their arrival time:

1
2
3
4
std::sort(
    carray, carray+SIZE,
    [] ( const Customer & a, const Customer & b) { return a.arrivalTime < b.arrivalTime; }
);


After you have sorted the array by arrival time, you can fit the elements to your queue:

1
2
3
4
for ( int i = 0; i < SIZE; ++i )
{
    custList.push( carray[i] );
}



The cashing:

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
void Cashier::cash( LLQueue<Customer> & custList )
{
    while( isDone( Cashier serv[], LLQueue<Customer> &custList ) == false )
    {
        if( cust.list.empty() == false )
        {
            busy = true;
            Customer customer =custLlist.pop();
            // do something with your customer;
            busy = false;
        }
    }
}

bool isDone(Cashier serv[], LLQueue<Customer> &custList)
{
    for( int i = 0; i < 3; ++i ) if( serv[i].busy ) return false;
    return custList.empty() ?  true : false;
}

void cash( Cashier cashiers[], LLQueue<Cashier> & customerList )
{
    std::thread th[3];

    for( int i = 0; i < 3; ++i )
    {
        th[i] = std::move( std::thread( &Cashier::cash, &cashiers[i], customerList ) );
    }
    for( int i = 0; i < 3; ++i ) th[i].join();
}

Last edited on
Topic archived. No new replies allowed.