-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathCheckOutApp.java
176 lines (149 loc) · 3.68 KB
/
CheckOutApp.java
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Solved By: Sandeep Ranjan (1641012352)
Solution for Programming Project
4.5
*/
import java.util.Scanner;
import java.util.Random;
class CirQueue {
private
int max,rear,front;
int[] a;
int size;
public
CirQueue(int m) {
max = m;
size = 0;
a = new int[m];
rear = front = -1;
}
void insert(int val) {
if(isFull()) {
System.out.println("CUSTOMER CANNOT BE ADDED AS QUEUE IS FULL");
} else {
if(isEmpty()) {
front = rear = 0;
a[rear] = val;
} else {
rear = (rear+1)%max;
a[rear] = val;
}
size++;
}
}
int size() {
return size;
}
void decFront(int rmv) {
a[front]-=rmv;
if (a[front] <= 0) {
delete();
}
}
void display() {
if(!isEmpty()) {
int i;
for(i=front; i!=rear; i=(i+1)%max) {
System.out.print(a[i] + " ");
}
System.out.print(a[i] + " ");
}
System.out.println();
}
boolean isFull() {
if(front == (rear+1)%max) return true;
else return false;
}
boolean isEmpty() {
if(size == 0) return true;
else return false;
}
int delete() {
if(isEmpty()) {
System.out.println("Underflow");
return -1;
} else {
int temp = a[front];
if(rear==front) {
rear = -1;
front = -1;
} else {
front = (front+1)%max;
}
size--;
return temp; }
}
}
class Checkout {
private int processPerMin, time;
private int maxItems;
private CirQueue[] lines;
public Checkout(int nLines, int maxLineLength, int processPerMin, int maxItems) {
this.processPerMin = processPerMin;
this.maxItems = maxItems;
this.time = 0;
lines = new CirQueue[nLines]; // Initialize all the lines to zero (Start)
for (int i = 0; i < nLines; i++) {
lines[i] = new CirQueue(maxLineLength);
}
}
public void addCustomer() {
time += 1;
// Shortest Line
int shortestLine = 0;
int minSize = lines[0].size();
for (int i = 0; i < lines.length; i++) {
if (lines[i].size() < minSize) {
minSize = lines[i].size();
shortestLine = i;
}
}
// Add the Customer to the Shortest Line
Random rand = new Random();
int items = rand.nextInt(maxItems) + 1;
lines[shortestLine].insert(items);
display();
}
public void incrementTime(int incLength) {
time += incLength;
for (int i = 0; i < lines.length; i++) {
if (lines[i].isEmpty()) { // skip empty queues
continue;
}
lines[i].decFront(processPerMin); // decrement item count
}
display();
}
private void display() {
System.out.println("Time (min): " + time);
for (int i = 0; i < lines.length; i++) {
System.out.print("Line " + i + " (size: " + lines[i].size() + "): ");
lines[i].display();
}
System.out.println("____________");
}
}
class CheckOutApp {
public static void main(String[] args) {
int nLines = 1; // Total no of Line
int maxLineLength = 3; // Total no of customers allowed in the line
int processPerMin = 2; // Item Processed/Min
int incLength = 1; // Incremenet time always by 1
int maxItems = 10; // Max item that can be bought by the customer
int choice;
Checkout cOut = new Checkout(nLines, maxLineLength, processPerMin, maxItems);
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("1. To Add a Customer");
System.out.println("2. To Increment Time");
System.out.println("3. Quit");
choice = sc.nextInt();
switch(choice) {
case 1 : cOut.addCustomer(); break;
case 2 : cOut.incrementTime(incLength); break;
case 3 : return;
default: System.out.println("Invalid input. Please try again");
}
}
}
}