-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClerkState.java
More file actions
292 lines (262 loc) · 9.41 KB
/
Copy pathClerkState.java
File metadata and controls
292 lines (262 loc) · 9.41 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import java.util.*;
import java.text.*;
import java.io.*;
public class ClerkState extends WarState {
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static Warehouse warehouse;
private static ClerkState instance;
private static final int EXIT = 0;
private static final int ADD_CLIENT = 1;
private static final int GET_PRODUCTS = 2;
private static final int GET_CLIENTS = 3;
private static final int GET_OUTSTANDING_ORDERS = 4;
private static final int SWITCH_TO_CLIENT = 5;
private static final int GET_WAITLIST_PRODUCTS = 6;
private static final int RECEIVE_SHIPMENT = 7;
private static final int ACCEPT_CLIENT_PAYMENT = 8;
private static final int SAVE = 9;
private static final int RETRIEVE = 10;
private static final int HELP = 11;
private ClerkState() {
warehouse = Warehouse.instance();
}
public static ClerkState instance() {
if (instance == null)
instance = new ClerkState();
return instance;
}
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 or " + HELP + " for help"));
if (value >= EXIT && value <= HELP) {
return value;
}
} catch (NumberFormatException nfe) {
System.out.println("Enter a number: ");
}
} while (true);
}
//************************** CLIENT FUNCTIONS ******************************//
public void addClient() {
String name = getToken("Enter client name: ");
String address = getToken("Enter address: ");
String phone = getToken("Enter phone number: ");
//String id = getToken("Enter ID: ");
Client result;
result = warehouse.addClient(name, address, phone);
if (result == null) {
System.out.println("Could not add member");
}
else {
System.out.println(result);
}
}
public void getClients() {
Iterator<Client> allClients = warehouse.getClients();
while (allClients.hasNext()) {
Client client = (Client) (allClients.next());
System.out.println(client.toString());
}
}
public void getOutstandingOrders() {
double balance;
Iterator<Client> allClients = warehouse.getClients();
while (allClients.hasNext()) {
Client client = (Client) (allClients.next());
balance = client.getBalance();
if(balance > 0)
{
System.out.println(client.toString());
}
}
}
public void acceptPayment() {
String clientId = getToken("Please enter the ID of the client you wish to accept payment");
if (warehouse.clientExists(clientId)) {
Double newPayment = Double.parseDouble(getToken("Enter payment: "));
warehouse.acceptPayment(clientId, newPayment);
}
else
System.out.println("Client ID doesnt exist.");
}
public void clientMenu() {
String clientID = getToken("Enter client ID: ");
// int cID = Integer.parseInt(clientID); unused
if(warehouse.instance().clientExists(clientID)) {
(WarContext.instance()).setUser(clientID);
(WarContext.instance()).changeState(1);
}
else
System.out.println("Invalid client ID.");
}
//************************* PRODUCT FUNCTIONS ******************************//
public void getProducts() {
Iterator<Product> allProducts = warehouse.getProducts();
while (allProducts.hasNext()) {
Product product = (Product)(allProducts.next());
System.out.println(product.toString());
}
}
public void displayWaitlist() {
String targetProduct = getToken("Please enter Product Name to display its waitlist");
// get product, iterate through it's waitlist and print each toString'
Iterator<WaitlistItem> waitlist;
if (warehouse.getProductByName(targetProduct).getWaitlistItems() != null) {
waitlist = (warehouse.getProductByName(targetProduct)).getWaitlistItems();
} else {
return;
}
while (waitlist.hasNext()) {
WaitlistItem waitlistItem = (WaitlistItem) waitlist.next();
System.out.println(waitlistItem.toString());
}
}
public void receiveShipment() {
String targetSupplier = getToken("Please enter Supplier Name to receive a shipment for it");
String targetProduct = getToken("Please enter Product name");
String targetQuantity = getToken("Please enter Quantity of product");
warehouse.receiveShipment(targetSupplier, targetProduct, targetQuantity);
}
private void save() {
if (warehouse.save()) {
System.out.println(" The warehouse has been successfully saved in the file WarehouseData \n" );
}
else {
System.out.println(" There has been an error in saving \n" );
}
}
private void retrieve() {
try {
Warehouse tempWarehouse = Warehouse.retrieve();
if (tempWarehouse != null) {
System.out.println(" The warehouse has been successfully retrieved from the file WarehouseData \n" );
warehouse = tempWarehouse;
} else {
System.out.println("File doesnt exist; creating new warehouse" );
warehouse = Warehouse.instance();
}
} catch(Exception cnfe) {
cnfe.printStackTrace();
}
}
public void logout() {
//client
if ((WarContext.instance()).getLogin() == WarContext.IsClient)
{
System.out.println("getLogin() == IsClient");
(WarContext.instance()).changeState(1);
}
//sales clerk
else if ((WarContext.instance()).getLogin() == WarContext.IsClerk)
{
System.out.println("getLogin() == IsClerk");
(WarContext.instance()).changeState(2);
}
//manager
else if ((WarContext.instance()).getLogin() == WarContext.IsManager)
{
System.out.println("getLogin() == IsManager");
(WarContext.instance()).changeState(3);
}
//error
else
{
System.out.println("getLogin() is something else");
(WarContext.instance()).changeState(0);}
}
public void help() {
System.out.println("Enter a number corresponding to a command as indicated below:");
System.out.println(EXIT + " to exit");
System.out.println(ADD_CLIENT + " to add a client");
System.out.println(GET_PRODUCTS + " to print products");
System.out.println(GET_CLIENTS + " to print clients");
System.out.println(GET_OUTSTANDING_ORDERS + " to print clients with outstanding balance");
System.out.println(SWITCH_TO_CLIENT + " to switch to client");
System.out.println(GET_WAITLIST_PRODUCTS + " to get products waitlist");
System.out.println(RECEIVE_SHIPMENT + " to receive a shipment");
System.out.println(ACCEPT_CLIENT_PAYMENT + " to accept client payment");
System.out.println(SAVE + " to save data");
System.out.println(RETRIEVE + " to retrieve");
System.out.println(HELP + " for help");
}
public void process() {
int command;
help();
while ((command = getCommand()) != EXIT) {
switch (command) {
case ADD_CLIENT: addClient();
break;
case GET_PRODUCTS: getProducts();
break;
case GET_CLIENTS: getClients();
break;
case GET_OUTSTANDING_ORDERS: getOutstandingOrders();
break;
case SWITCH_TO_CLIENT: clientMenu();
break;
case GET_WAITLIST_PRODUCTS: displayWaitlist();
break;
case RECEIVE_SHIPMENT: receiveShipment();
break;
case ACCEPT_CLIENT_PAYMENT: acceptPayment();
break;
case SAVE: save();
break;
case RETRIEVE: retrieve();
break;
case HELP: help();
break;
}
}
logout();
}
public void run() {
process();
}
}