-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomer.java
More file actions
109 lines (97 loc) · 4.12 KB
/
Customer.java
File metadata and controls
109 lines (97 loc) · 4.12 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
import java.util.Scanner;
import Helpers.CustomerModel;
public class Customer {
private static String choice = "";
private static boolean properExit = false;
public static void main(String[] args) {
System.out.print("Hello, how should I address you: ");
try {
// Get User Name
Scanner input = new Scanner(System.in);
String customerName = input.nextLine();
// Intercept and Handle SIGINT (CTRL + C) Signal
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (!properExit) {
System.out.println("\nReceived termination signal (SIGTERM).\n");
System.out.println("Leave from the front door next time, " + customerName + ".\n");
}
}));
// Set Up Connection with Server
try (CustomerModel customer = new CustomerModel(customerName)) {
System.out.println("\nWelcome to the Cafe, " + customerName + ".");
// Receive input from user as Order, send Request to Server
while (!choice.equals("exit")) {
// Prompt user on how to interact with the server
System.out.println("\nCommand Formats to Interact with Barista:\n" +
"****************************************" +
"\n[ To Order ]\n" +
"- order 1 tea / order 1 coffee\n" +
"- order 2 tea and 2 coffee\n" +
"- order 2 teas and 3 coffees\n" +
"- order 2 tea and 3 coffees\n" +
"\n[ View Order Status ]\n" +
"- order status\n" +
"\n[ Exit Cafe ]\n" +
"- exit\n" +
"****************************************");
System.out.print("Enter a Command: ");
// Get User Input
choice = input.nextLine().toLowerCase();
String[] substring = choice.split(" ");
// Handle User Input
switch (substring[0]) {
case "order":
// Handle Order Status
if (substring[1].equals("status")) {
// Retrieve all order status
String[] orderStatus = customer.getOrderStatus();
// Print All Order Status
System.out.println("\n[ RESULT SHOWN BELOW ]");
if (orderStatus.length == 0) {
System.out.println("Oops, no new order found for " + customerName);
} else {
System.out.println("Order Status for " + customerName + ": ");
for (String order : orderStatus) {
System.out.println(order);
}
}
System.out.println("\nPress enter to continue..\n\n");
input.nextLine();
}
// Handle Order Drinks
else {
String order = customer.orderDrinks(choice, customerName);
String orderSplit[] = order.split(",");
System.out.println("\n[ RESULT SHOWN BELOW ]");
if (orderSplit[1].equals("true")) {
System.out.println("Add-On Order received from " + customerName + " (" + orderSplit[0] + ")");
} else {
System.out.println("Order received from " + customerName + " (" + orderSplit[0] + ")");
}
System.out.println("\nPress enter to continue..\n\n");
input.nextLine();
}
break;
// User Exits The Cafe
case "exit":
customer.stopPolling(); // Interrupt and stop polling immediately after customer exit
properExit = true;
customer.exitCafe();
input.close();
System.out.println("\nBye Bye, " + customerName + ".\n");
break;
// User Typed a Wrong Command
default:
System.out.println("\n[ RESULT SHOWN BELOW ]");
System.out.println("Unknown Command: " + choice);
System.out.println("\nPress enter to continue..\n\n");
input.nextLine();
break;
}
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}