-
Notifications
You must be signed in to change notification settings - Fork 0
/
qb263.java
64 lines (53 loc) · 2.19 KB
/
qb263.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
/* QB-263:
Write a program that simulates a queue of customers at a coffee shop using an ArrayDeque.
The program should allow the user to perform the following actions:
Add a new customer to the back of the queue
Serve the next customer in the queue (i.e. remove the customer from the front of the
queue) View the current queue of customers
The program should continue to prompt the user for actions until they choose to quit.
*/
import java.util.*;
class CoffeeShop {
public static void main(String[] args) {
ArrayDeque<String> queue = new ArrayDeque<>();
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println(
"ENTER YOUR CHOICE\n 1)ADD NEW CUSTOMER: \n 2)SERVE NEXT CUSTOMER \n 3) VIEW CURRENT QUEUE \n 4)QUIT");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
System.out.println("Enter Customer Name: ");
String name = sc.nextLine();
queue.addLast(name);
System.out.println(name + ": has been added to the queue.");
break;
case 2:
if (!queue.isEmpty()) {
String served = queue.pollFirst();
System.out.println("Now Serving: " + served);
} else {
System.out.println("Queue is Empty. No Customer to serve");
}
break;
case 3:
if (!queue.isEmpty()) {
Iterator itr = queue.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
} else {
System.out.println("Queue is Empty.. No Customers");
}
case 4:
System.out.println("Exiting !! COFFEE SHOP QUEUE SIMULATION..");
break;
default:
System.out.println("INVALID CHOICE !!");
break;
}
} while (choice != 4);
}
}