-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.cpp
143 lines (120 loc) · 5.37 KB
/
simulation.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "simulation.h"
void customerArrival(int time, int &idNum, LinkedList &shopperList) {
// PRE: Takes in current time, id number, and the shopper list
// POST: Generates random customers, creates their data, and adds them to shopper list
// Generates random number of customers to arrive at the store
int numCustomers = rand() % (3 - 1) + 2;
int itemPercent;
listType tempShopper;
// Updates customer data and adds them to Linked List
for (int i = 0; i < numCustomers; i++) {
// Assigns the cartID and then increments the value of the variable
tempShopper.cartID = idNum;
idNum++;
// Randomly determines how many items each customer will get
itemPercent = rand() % (100 - 1) + 1;
if (itemPercent <= 40) {
tempShopper.itemCount = rand() % (10 - 1) + 1;
} else if (itemPercent <= 70) {
tempShopper.itemCount = rand() % (20 - 11) + 11;
} else if (itemPercent <= 90) {
tempShopper.itemCount = rand() % (30 - 21) + 21;
} else {
tempShopper.itemCount = rand() % (60 - 31) + 31;
}
// Determines shop time and enterQTime for each customer
int shopTimeSeconds = 0;
for (int j = 0; j < tempShopper.itemCount; j++) {
shopTimeSeconds += (rand() % (60 - 30) + 30);
}
// Rounds up (>= 30) or down (< 30) to the nearest minute
int shopMinutes = (shopTimeSeconds + 29) / 60;
tempShopper.enterQTime = time + shopMinutes;
// Adds customers to shopperList and increments list count
shopperList.addElement(tempShopper);
LinkedList::listCount++;
}
}
void updateLines(int time, LinkedList &shopperList, vector<Queue> &queues) {
// PRE: Takes in current time, the shopper list, and the vector of Queue objects
// POST: Checks the shopper list and each queue to see if any customers need to be moved in or out of them and updates
// the object's data accordingly
// Checks to see if anyone is ready to leave the checkouts
for (Queue &queue: queues) {
queueNodeData tempData = queue.peek();
while (!queue.queueIsEmpty() && tempData.timeAvailable <= time) {
queue.lineData.queueCount--;
queue.lineData.currItems -= tempData.itemCount;
queue.deQueue();
if (!queue.queueIsEmpty()) {
tempData = queue.peek();
} else {
break;
}
}
}
// Checks to see if anyone in the store is ready to be added to the checkout line
listType tempData = shopperList.peek();
while (tempData.enterQTime == time) {
// Finds the shortest queue
int shortest_queue = 0;
for (int i = 0; i < queues.size(); i++) {
if (queues[i].lineData.currItems < queues[shortest_queue].lineData.currItems) {
shortest_queue = i;
}
}
Queue updateQueue = queues[shortest_queue];
// Updates data for new checkout customer
queueNodeData tempQueueShopper;
tempQueueShopper.itemCount = tempData.itemCount;
// Determines timeAvailable each customer -- checkout time is .15 min (9 seconds) per item
int checkoutTimeSeconds = tempQueueShopper.itemCount * 9;
// Rounds up (>= 30) or down (< 30) to the nearest minute
int checkoutMinutes = (checkoutTimeSeconds + 29) / 60;
tempQueueShopper.timeAvailable = time + checkoutMinutes;
// Update currentItems and totalItems for the queue
updateQueue.lineData.currItems += tempQueueShopper.itemCount;
updateQueue.lineData.totalItems += tempQueueShopper.itemCount;
// Updates queue counts and max queue length if necessary
updateQueue.lineData.queueCount++;
updateQueue.lineData.totalCustomers++;
if (updateQueue.lineData.queueCount > updateQueue.lineData.maxQueueLength) {
updateQueue.lineData.maxQueueLength = updateQueue.lineData.queueCount;
}
// Adds checkout customer to update queue
updateQueue.enQueue(tempQueueShopper);
queues[shortest_queue] = updateQueue;
// Deletes shopper from shopperList and decrements list count
shopperList.delElement();
LinkedList::listCount--;
tempData = shopperList.peek();
}
// Update all queue statistics
for (Queue &queue: queues) {
queueData data = queue.lineData;
// Updates totalIdleTime
if (data.queueCount == 0) {
data.totalIdleTime++;
}
// Updates totalOverTime
if (time > 720 && data.queueCount > 0) {
data.totalOverTime++;
}
queue.lineData = data;
}
}
void outputReport(vector<Queue> queues) {
// PRE: Takes in vector of Queue objects
// POST: Outputs simulation report
int width = 25;
for (int i = 0; i < queues.size(); i++) {
queueData data = queues[i].lineData;
cout << "CHECKOUT LINE:\t" << i
<< "\n " << left << setw(width) << "Total Customers Helped:" << data.totalCustomers
<< "\n " << setw(width) << "Total Number of Items:" << data.totalItems
<< "\n " << setw(width) << "Max Line Length:" << data.maxQueueLength
<< "\n " << setw(width) << "Total Idle Time:" << data.totalIdleTime
<< "\n " << setw(width) << "Total Over Time:" << data.totalOverTime
<< endl;
}
}