-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientState.java
More file actions
269 lines (247 loc) · 8.96 KB
/
Copy pathClientState.java
File metadata and controls
269 lines (247 loc) · 8.96 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import java.util.*;
import java.text.*;
import java.io.*;
public class ClientState extends WarState {
private static ClientState clientstate;
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static Warehouse warehouse;
private static final int EXIT = 0;
private static final int SHOW_CLIENT = 1;
private static final int SHOW_PRODUCTS = 2;
private static final int GET_TRANSACTIONS = 3;
private static final int ADD_TO_CART = 4;
private static final int EDIT_CART = 5;
private static final int SHOW_CART = 6;
private static final int SHOW_WAITLIST = 7;
private static final int HELP = 10;
// Constructor
private ClientState() {
warehouse = Warehouse.instance();
}
// Singleton guard
public static ClientState instance() {
if (clientstate == null) {
return clientstate = new ClientState();
} else {
return clientstate;
}
}
// Input/Output functions
public String getToken(String prompt) {
do {
try {
System.out.println(prompt);
String line = reader.readLine();
StringTokenizer tokenizer = new StringTokenizer(line,"\n\r\f");
if (tokenizer.hasMoreTokens()) {
return tokenizer.nextToken();
}
} catch (IOException ioe) {
System.exit(0);
}
} while (true);
}
/*private boolean yesOrNo(String prompt) {
String more = getToken(prompt + " (Y|y)[es] or anything else for no");
if (more.charAt(0) != 'y' && more.charAt(0) != 'Y') {
return false;
}
return true;
}*/
public int getNumber(String prompt) {
do {
try {
String item = getToken(prompt);
Integer num = Integer.valueOf(item);
return num.intValue();
} catch (NumberFormatException nfe) {
System.out.println("Please input a number ");
}
} while (true);
}
public Calendar getDate(String prompt) {
do {
try {
Calendar date = new GregorianCalendar();
String item = getToken(prompt);
DateFormat df = SimpleDateFormat.getDateInstance(DateFormat.SHORT);
date.setTime(df.parse(item));
return date;
} catch (Exception fe) {
System.out.println("Please input a date as mm/dd/yy");
}
} while (true);
}
public int getCommand() {
do {
try {
int value = Integer.parseInt(getToken("Enter command:" + HELP + " for help"));
if (value >= EXIT && value <= HELP) {
return value;
}
} catch (NumberFormatException nfe) {
System.out.println("Enter a number");
}
} while (true);
}
public void help() {
System.out.println("Enter a number between 0 and 12 as explained below:");
System.out.println(EXIT + " to Logout/Exit State\n");
System.out.println(SHOW_CLIENT + " to display the details of this client");
System.out.println(SHOW_PRODUCTS + " to display the catalog of products");
System.out.println(ADD_TO_CART + " to add an item to this client's cart");
System.out.println(EDIT_CART + " to edit this client's cart");
System.out.println(SHOW_CART + " to display this client's cart");
System.out.println(SHOW_WAITLIST + " to display this client's waitlist");
System.out.println(HELP + " for help");
}
// Show only THIS client's details
public void showClient() {
String id = WarContext.instance().getUser();
Iterator<Client> allClients = warehouse.getClients();
while (allClients.hasNext()) {
Client client = (Client) (allClients.next());
if (client.getId().equals(id)) {
System.out.println(client.toString());
return;
}
}
}
// Show full catalog of products with sales prices
public void showProducts() {
Iterator<Product> allProducts = warehouse.getProducts();
while (allProducts.hasNext()) {
Product product = (Product) (allProducts.next());
System.out.println(product.toString());
}
}
// Must show all transactions for THIS client
public void showTransactions() {
String clientId = WarContext.instance().getUser();
Calendar date = getDate("Please enter the date for which you want records as mm/dd/yy");
Iterator<Transaction> result = warehouse.getTransactions(clientId, date);
if (result == null) {
System.out.println("Invalid Member ID");
} else {
while (result.hasNext()) {
Transaction transaction = (Transaction) result.next();
System.out.println(transaction.getType() + " " + transaction.getTitle() + "\n");
}
System.out.println("\n There are no more transactions \n");
}
}
// Add item to this client's cart
public void addToCart() {
System.out.println("Add to Cart selected.");
String clientId = WarContext.instance().getUser();
String productName = getToken("Enter Product Name");
int quantity = getNumber("Enter quantity");
// Check if client exists
if (!warehouse.clientExists(clientId)) {
System.out.println("Error: client not found");
return; // Stop here if not found
} else {
System.out.println("ID " + clientId + " found.");
}
// Check if product exists
if (!warehouse.productExists(productName)) {
System.out.println("Error: product not found");
return; // Stop here if not found
} else {
System.out.println("Name " + productName + " found.");
}
// Next, instantiate a CartItem object and add it to the Client's cart (CartItem
// list)
if (warehouse.addToCart(clientId, productName, quantity)) {
System.out.println("Successfully added item to cart");
} else {
System.out.println("Error: failed to add item to cart");
}
}
// Edit this client's cart
public void editCart() {
String clientId = WarContext.instance().getUser();
if (warehouse.clientExists(clientId)) { // Check if client exists
warehouse.displayCart(clientId);
String productName = getToken("Please enter the name of the product in the cart");
if (warehouse.inCart(clientId, productName)) { // Check if product exists in the client's cart **** CHANGE CODE TO REFLECT THIS ****
int newQuant = getNumber("Please enter a new quantity for " + productName + ", or 0 to remove");
warehouse.editCart(clientId, productName, newQuant);
}
}
else {
System.out.println("ID not found");
}
}
public void showCart() {
String clientId = WarContext.instance().getUser();
// warehouse.displayCart(clientId);
Iterator<CartItem> cart = (warehouse.getClientById(clientId)).getCartItems();
while (cart.hasNext()) {
CartItem item = (CartItem) cart.next();
System.out.println(item.toString());
}
}
public void showWaitlist() {
// Get iterator for ProductList
Iterator<Product> productList = warehouse.getProducts();
while (productList.hasNext()) {
// For each Product:
Product product = (Product) productList.next();
// Get iterator for it's Waitlist
Iterator<WaitlistItem> waitlist = product.getWaitlistItems();
// For each WaitlistItem:
while (waitlist != null && waitlist.hasNext()) {
WaitlistItem waitlistItem = (WaitlistItem) waitlist.next();
// if Client = this Client, display Product + Quantity
if (waitlistItem.getClient().getId().equals(WarContext.instance().getUser())) {
System.out.println(waitlistItem.getProduct().getName() + " : " + waitlistItem.getQuantity());
}
}
}
}
// ***** End of functions callable from ClientState UI *****
public void process() {
int command;
help();
while ((command = getCommand()) != EXIT) {
switch (command) {
case SHOW_CLIENT: showClient();
break;
case SHOW_PRODUCTS: showProducts();
break;
case GET_TRANSACTIONS: showTransactions();
break;
case ADD_TO_CART: addToCart();
break;
case EDIT_CART: editCart();
break;
case SHOW_CART: showCart();
break;
case SHOW_WAITLIST: showWaitlist();
break;
case HELP: help();
break;
}
}
logout(); // Logout must occur OUTSIDE the loop, as shown
}
// Start prompts for ClientState
public void run() {
process();
}
public void logout()
{
if ((WarContext.instance()).getLogin() == WarContext.IsClerk
|| (WarContext.instance()).getLogin() == WarContext.IsManager)
{ //stem.out.println(" going to clerk \n ");
(WarContext.instance()).changeState(2); // exit with a code 1
}
else if (WarContext.instance().getLogin() == WarContext.IsClient)
{ //stem.out.println(" going to login \n");
(WarContext.instance()).changeState(0); // exit with a code 2
}
else
(WarContext.instance()).changeState(2); // exit code 2, indicates error
}
}