-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckout.java
More file actions
158 lines (146 loc) · 6.84 KB
/
Copy pathCheckout.java
File metadata and controls
158 lines (146 loc) · 6.84 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
The Checkout class simulates three different models for self-checkout stations at a
grocery store to compare their efficiency:
1. One line for customers and n checkout stations
2. n lines for customers (customers choose the shortest line)
3. n lines for customers (customers choose a random line)
The simulation is meant to run for 2 hours and reports statistics including the number of
customers served, maximum queue length, and average waiting time.
*/
import java.util.*;
public class Checkout {
// Model 1: One line, n stations
public static void oneLine(int stations) {
Queue<Customer> line = new Queue<>();
int[] stationBusyTime = new int[stations];
int totalCustomers = 0;
int totalWaitTime = 0;
int maxQueueLength = 0;
Random rand = new Random();
for (int time = 0; time < 7200; time++) {
// Every 30 seconds, a new customer arrives
if (time % 30 == 0) {
int items = 5 + rand.nextInt(16);
Customer newCustomer = new Customer(items, time);
line.enqueue(newCustomer);
if (line.size() > maxQueueLength) maxQueueLength = line.size();
}
// For each station, check if it's available
for (int i = 0; i < stations; i++) {
if (stationBusyTime[i] > 0) {
stationBusyTime[i]--;
} else {
if (!line.isEmpty()) {
Customer current = line.dequeue();
// Calculate checkout time
int checkoutTime = current.getItems() * 2 + 15;
current.setStartTime(time);
current.setEndTime(time + checkoutTime);
totalCustomers++;
totalWaitTime += current.getStartTime() - current.getArrivalTime();
stationBusyTime[i] = checkoutTime;
}
}
}
}
System.out.println("Model 1: One Line, " + stations + " Stations ");
System.out.println("Total Customers Served: " + totalCustomers);
System.out.println("Average Wait Time (seconds): " + (totalCustomers == 0 ? 0 : totalWaitTime / totalCustomers));
System.out.println("Maximum Queue Length Reached: " + maxQueueLength + "\n");
}
// Model 2: n lines, customer chooses shortest line
public static void shortestLine(int stations) {
Queue<Customer>[] lines = new Queue[stations];
for (int i = 0; i < stations; i++) lines[i] = new Queue<>();
int[] stationBusyTime = new int[stations];
int totalCustomers = 0;
int totalWaitTime = 0;
int maxQueueLength = 0;
Random rand = new Random();
for (int time = 0; time < 7200; time++) {
if (time % 30 == 0) {
int items = 5 + rand.nextInt(16);
Customer newCustomer = new Customer(items, time);
int shortestLine = 0;
for (int i = 1; i < stations; i++) {
if (lines[i].size() < lines[shortestLine].size()) shortestLine = i;
}
lines[shortestLine].enqueue(newCustomer);
for (Queue<Customer> line : lines) {
if (line.size() > maxQueueLength) maxQueueLength = line.size();
}
}
for (int i = 0; i < stations; i++) {
if (stationBusyTime[i] > 0) {
stationBusyTime[i]--;
} else {
if (!lines[i].isEmpty()) {
Customer current = lines[i].dequeue();
// Calculate checkout time
int checkoutTime = current.getItems() * 2 + 15;
current.setStartTime(time);
current.setEndTime(time + checkoutTime);
totalCustomers++;
totalWaitTime += current.getStartTime() - current.getArrivalTime();
stationBusyTime[i] = checkoutTime;
}
}
}
}
System.out.println("Model 2: Shortest Line, " + stations + " Stations");
System.out.println("Total Customers Served: " + totalCustomers);
System.out.println("Average Wait Time (seconds): " + (totalCustomers == 0 ? 0 : totalWaitTime / totalCustomers));
System.out.println("Maximum Queue Length Reached: " + maxQueueLength + "\n");
}
// Model 3: n lines, customer chooses random line
public static void randomLine(int stations) {
Queue<Customer>[] lines = new Queue[stations];
for (int i = 0; i < stations; i++) lines[i] = new Queue<>();
int[] stationBusyTime = new int[stations];
int totalCustomers = 0;
int totalWaitTime = 0;
int maxQueueLength = 0;
Random rand = new Random();
for (int time = 0; time < 7200; time++) {
if (time % 30 == 0) {
int items = 5 + rand.nextInt(16);
Customer newCustomer = new Customer(items, time);
int randomLine = rand.nextInt(stations);
lines[randomLine].enqueue(newCustomer);
for (Queue<Customer> line : lines) {
if (line.size() > maxQueueLength) maxQueueLength = line.size();
}
}
for (int i = 0; i < stations; i++) {
if (stationBusyTime[i] > 0) {
stationBusyTime[i]--; // Station is still working
} else {
if (!lines[i].isEmpty()) {
Customer current = lines[i].dequeue();
// Calculate checkout time
int checkoutTime = current.getItems() * 2 + 15; // 2s/item + 15s to pay
current.setStartTime(time);
current.setEndTime(time + checkoutTime);
// Stats
totalCustomers++;
totalWaitTime += current.getStartTime() - current.getArrivalTime();
// Station is now busy
stationBusyTime[i] = checkoutTime;
}
}
}
}
// Print out results
System.out.println("Model 3: Random Line, " + stations + " Stations");
System.out.println("Total Customers Served: " + totalCustomers);
System.out.println("Average Wait Time (seconds): " + (totalCustomers == 0 ? 0 : totalWaitTime / totalCustomers));
System.out.println("Maximum Queue Length Reached: " + maxQueueLength + "\n");
}
public static void main(String[] args) {
// Simulate 3 checkout stations with individual lines
int stations = 3;
oneLine(stations);
shortestLine(stations);
randomLine(stations);
}
}