Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. #include "Cashier.h"
  2. #include "Customer.h"
  3. #include "LLQueue.h"
  4. #include "Node.h"
  5. #include <iostream>
  6. #include <fstream>
  7. using namespace std;
  8.  
  9. //*******************************************************************************************************
  10.  
  11. void fillCustomerQueue(LLqueue<Customer> custList[], Customer *custArray, int &currentTime,
  12. int &index, Cashier serv[], ofstream &output);
  13. int findMinQueue(LLqueue<Customer> custList[], Cashier serv[]);
  14. void processCustomers(LLqueue<Customer> custList[],Cashier serv[], const int currentTime,
  15. ofstream &output);
  16. // void processCashier(LLqueue<Customer> custlist[]) I did not end up using this function,
  17. // as it was already worked into others
  18. bool isDone(Cashier serv[], LLqueue<Customer> custList[]);
  19.  
  20. //*******************************************************************************************************
  21.  
  22. const int CASHIER_SIZE = 3; // Used in case the program winds up longer.
  23. const int CUSTOMER_SIZE = 50;
  24.  
  25. //*******************************************************************************************************
  26.  
  27. int main()
  28. {
  29. LLqueue<Customer> custList[CASHIER_SIZE]; // I used an array of Customer Queues.
  30. Customer custArray[CUSTOMER_SIZE]; // Holds customers before enqueued.
  31. Cashier serv[CASHIER_SIZE]; // Cashiers to serve customers.
  32. int currentTime = 0;
  33. int index = 0; // Used to load array.
  34. ifstream custData;
  35. ofstream output;
  36. custData.open("customerList.txt");
  37. output.open("Trace.txt");
  38.  
  39. if(custData)
  40. {
  41. while(!custData.eof()) // Loads up custArray.
  42. {
  43. custData >> custArray[index].id;
  44. custData >> custArray[index].arrivalTime;
  45. custData >> custArray[index].serviceTime;
  46. index++;
  47. }
  48. index = 0; // Resets index for passing into function.
  49. fillCustomerQueue(custList,custArray,currentTime,index,serv,output); // Done to initialize the program.
  50. processCustomers(custList,serv,currentTime,output); // I could not think of another way to do this such
  51. currentTime++; // that isDone would not falsely flag.
  52. output << endl;
  53.  
  54. while(!isDone(serv,custList))
  55. {
  56. fillCustomerQueue(custList,custArray,currentTime,index,serv,output);
  57. processCustomers(custList,serv,currentTime,output);
  58. currentTime++;
  59. output << endl;
  60. }
  61. output << "All customers processed within " << currentTime - 1 << " minutes." << endl;
  62. }
  63. else
  64. cout << "Error reading data." << endl;
  65.  
  66. custData.close();
  67. output.close();
  68. return 0;
  69. }
  70.  
  71. //*******************************************************************************************************
  72. // This function determines which is the most easily-accessed queue.
  73. //*******************************************************************************************************
  74.  
  75. int findMinQ(LLqueue<Customer>custList[],Cashier serv[])
  76. {
  77. int index = 0;
  78. int size1, size2 = 0; // I use these to tally how 'busy' each queue is.
  79.  
  80. for(int i = 1; i < CASHIER_SIZE; i++) // Starts at 1 since index is initialized to 0.
  81. {
  82. size1 = custList[index].getSize(); // The size is tallied...
  83. size2 = custList[i].getSize();
  84. if(serv[index].busy == true) // And if the cashier is busy, one person is added.
  85. size1++;
  86. if(serv[i].busy == true)
  87. size2++;
  88.  
  89. if(size1 > size2) // Sizes are compared. If i is better than index, index is reassigned to i.
  90. index = i;
  91. }
  92.  
  93. return index;
  94. }
  95.  
  96. //*******************************************************************************************************
  97. // Displays the current time, and, using this, determines when to move customers into the queue.
  98. //*******************************************************************************************************
  99.  
  100. void fillCustomerQueue(LLqueue<Customer> custList[],Customer *custArray,
  101. int &currentTime, int &index, Cashier serv[], ofstream &output)
  102. {
  103. int temp = findMinQ(custList,serv); // Initializes temp.
  104. output << "Time = " << currentTime << endl;
  105.  
  106. while(custArray[index].arrivalTime == currentTime)
  107. {
  108. custList[temp].enqueue(custArray[index]); // Assigns customers to the shortest queue.
  109. output << "Customer #" << custArray[index].id << " has been enqueued to queue "
  110. << temp << "." << endl;
  111. temp = findMinQ(custList,serv); // Temp is reset in case more than one customer will be queued.
  112. index++;
  113. }
  114. }
  115.  
  116. //*******************************************************************************************************
  117. // Processes enqueued customers through the cashiers. Different messages based on whether they are busy.
  118. //*******************************************************************************************************
  119.  
  120. void processCustomers(LLqueue<Customer>custList[],Cashier serv[], const int currentTime,
  121. ofstream &output)
  122. {
  123. Customer temp;
  124. for(int i = 0; i < CASHIER_SIZE; i++)
  125. {
  126. if(serv[i].busy == true && serv[i].endTime == currentTime)
  127. serv[i].busy = false;
  128.  
  129.  
  130. if(serv[i].busy == true)
  131. output << "Cashier #" << i + 1 << " is currently serving customer #" << serv[i].custID
  132. << " until time: " << serv[i].endTime << endl;
  133.  
  134. if(serv[i].busy == false && !custList[i].isEmpty())
  135. {
  136. // Customer starts getting service.
  137. custList[i].dequeue(temp);
  138. serv[i].custID = temp.id;
  139. serv[i].endTime = currentTime + temp.serviceTime;
  140. serv[i].busy = true;
  141. output << "Cashier #" << i + 1 << " has started serving customer #" << serv[i].custID
  142. << " until time: " << serv[i].endTime << endl;
  143. }
  144.  
  145. if(serv[i].busy == false && custList[i].isEmpty())
  146. output << "Cashier #" << i + 1 << " is not busy." << endl;
  147. }
  148. }
  149.  
  150. //*******************************************************************************************************
  151. // If all cashiers are not busy and the queue is empty, then there is no more work to do.
  152. //*******************************************************************************************************
  153.  
  154. bool isDone(Cashier serv[], LLqueue<Customer>custList[])
  155. {
  156. bool success = true;
  157.  
  158. for(int i = 0; i < CASHIER_SIZE; i++)
  159. {
  160. if(!custList[i].isEmpty())
  161. success = false;
  162. if(serv[i].busy == true)
  163. success = false;
  164. }
  165. return success;
  166. }
  167.  
  168. //*******************************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement