Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Cashier.h"
- #include "Customer.h"
- #include "LLQueue.h"
- #include "Node.h"
- #include <iostream>
- #include <fstream>
- using namespace std;
- //*******************************************************************************************************
- void fillCustomerQueue(LLqueue<Customer> custList[], Customer *custArray, int ¤tTime,
- int &index, Cashier serv[], ofstream &output);
- int findMinQueue(LLqueue<Customer> custList[], Cashier serv[]);
- void processCustomers(LLqueue<Customer> custList[],Cashier serv[], const int currentTime,
- ofstream &output);
- // void processCashier(LLqueue<Customer> custlist[]) I did not end up using this function,
- // as it was already worked into others
- bool isDone(Cashier serv[], LLqueue<Customer> custList[]);
- //*******************************************************************************************************
- const int CASHIER_SIZE = 3; // Used in case the program winds up longer.
- const int CUSTOMER_SIZE = 50;
- //*******************************************************************************************************
- int main()
- {
- LLqueue<Customer> custList[CASHIER_SIZE]; // I used an array of Customer Queues.
- Customer custArray[CUSTOMER_SIZE]; // Holds customers before enqueued.
- Cashier serv[CASHIER_SIZE]; // Cashiers to serve customers.
- int currentTime = 0;
- int index = 0; // Used to load array.
- ifstream custData;
- ofstream output;
- custData.open("customerList.txt");
- output.open("Trace.txt");
- if(custData)
- {
- while(!custData.eof()) // Loads up custArray.
- {
- custData >> custArray[index].id;
- custData >> custArray[index].arrivalTime;
- custData >> custArray[index].serviceTime;
- index++;
- }
- index = 0; // Resets index for passing into function.
- fillCustomerQueue(custList,custArray,currentTime,index,serv,output); // Done to initialize the program.
- processCustomers(custList,serv,currentTime,output); // I could not think of another way to do this such
- currentTime++; // that isDone would not falsely flag.
- output << endl;
- while(!isDone(serv,custList))
- {
- fillCustomerQueue(custList,custArray,currentTime,index,serv,output);
- processCustomers(custList,serv,currentTime,output);
- currentTime++;
- output << endl;
- }
- output << "All customers processed within " << currentTime - 1 << " minutes." << endl;
- }
- else
- cout << "Error reading data." << endl;
- custData.close();
- output.close();
- return 0;
- }
- //*******************************************************************************************************
- // This function determines which is the most easily-accessed queue.
- //*******************************************************************************************************
- int findMinQ(LLqueue<Customer>custList[],Cashier serv[])
- {
- int index = 0;
- int size1, size2 = 0; // I use these to tally how 'busy' each queue is.
- for(int i = 1; i < CASHIER_SIZE; i++) // Starts at 1 since index is initialized to 0.
- {
- size1 = custList[index].getSize(); // The size is tallied...
- size2 = custList[i].getSize();
- if(serv[index].busy == true) // And if the cashier is busy, one person is added.
- size1++;
- if(serv[i].busy == true)
- size2++;
- if(size1 > size2) // Sizes are compared. If i is better than index, index is reassigned to i.
- index = i;
- }
- return index;
- }
- //*******************************************************************************************************
- // Displays the current time, and, using this, determines when to move customers into the queue.
- //*******************************************************************************************************
- void fillCustomerQueue(LLqueue<Customer> custList[],Customer *custArray,
- int ¤tTime, int &index, Cashier serv[], ofstream &output)
- {
- int temp = findMinQ(custList,serv); // Initializes temp.
- output << "Time = " << currentTime << endl;
- while(custArray[index].arrivalTime == currentTime)
- {
- custList[temp].enqueue(custArray[index]); // Assigns customers to the shortest queue.
- output << "Customer #" << custArray[index].id << " has been enqueued to queue "
- << temp << "." << endl;
- temp = findMinQ(custList,serv); // Temp is reset in case more than one customer will be queued.
- index++;
- }
- }
- //*******************************************************************************************************
- // Processes enqueued customers through the cashiers. Different messages based on whether they are busy.
- //*******************************************************************************************************
- void processCustomers(LLqueue<Customer>custList[],Cashier serv[], const int currentTime,
- ofstream &output)
- {
- Customer temp;
- for(int i = 0; i < CASHIER_SIZE; i++)
- {
- if(serv[i].busy == true && serv[i].endTime == currentTime)
- serv[i].busy = false;
- if(serv[i].busy == true)
- output << "Cashier #" << i + 1 << " is currently serving customer #" << serv[i].custID
- << " until time: " << serv[i].endTime << endl;
- if(serv[i].busy == false && !custList[i].isEmpty())
- {
- // Customer starts getting service.
- custList[i].dequeue(temp);
- serv[i].custID = temp.id;
- serv[i].endTime = currentTime + temp.serviceTime;
- serv[i].busy = true;
- output << "Cashier #" << i + 1 << " has started serving customer #" << serv[i].custID
- << " until time: " << serv[i].endTime << endl;
- }
- if(serv[i].busy == false && custList[i].isEmpty())
- output << "Cashier #" << i + 1 << " is not busy." << endl;
- }
- }
- //*******************************************************************************************************
- // If all cashiers are not busy and the queue is empty, then there is no more work to do.
- //*******************************************************************************************************
- bool isDone(Cashier serv[], LLqueue<Customer>custList[])
- {
- bool success = true;
- for(int i = 0; i < CASHIER_SIZE; i++)
- {
- if(!custList[i].isEmpty())
- success = false;
- if(serv[i].busy == true)
- success = false;
- }
- return success;
- }
- //*******************************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement