-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathManagerState.java
More file actions
274 lines (232 loc) · 9.46 KB
/
ManagerState.java
File metadata and controls
274 lines (232 loc) · 9.46 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
package com.company;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.text.*;
import java.io.*;
import javax.swing.*;
public class ManagerState extends WareState {
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private WareContext context;
private static ManagerState instance;
private ClientList clientList;
private ProductList productList;
private SupplierList supplierList;
//manager specific calls
private static final int EXIT = 0;
private static final int ADD_PRODUCT = 1;
private static final int ADD_SUPPLIER = 2;
private static final int SHOW_SUPPLIER_LIST = 3;
private static final int SHOW_SUPPLIER_PRODUCTS = 4;
private static final int SHOW_PRODUCT_SUPPLIERS = 5;
private static final int UPDATE_PRODUCT_PRICE = 6;
private static final int SALES_MENU = 7;
private static final int HELP = 8;
private JFrame frame;
private LogoutButton logoutButton = new LogoutButton();
private ClerkButton clerkButton = new ClerkButton();
private ManagerState() {
super();
supplierList = SupplierList.instance();
productList = ProductList.instance();
clientList = ClientList.instance();
//context = WareContext.instance();
((LogoutButton) logoutButton).setListener();
}
public static ManagerState instance() {
if (instance == null) {
instance = new ManagerState();
}
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);
}
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 addProduct() {
boolean successful;
String productName = getToken("Enter the product name: ");
int productQuantity = Integer.parseInt(getToken("Enter the product quantity: "));
int productPrice = Integer.parseInt(getToken("Enter the product price: "));
Product newProduct = new Product(productName, productQuantity, productPrice);
successful = productList.insertProduct(newProduct);
if (successful) {
System.out.println("Product with details (" + newProduct.toString() + ") added successfully");
} else {
System.out.println("Issue adding product!");
}
}
public void addSupplier() {
boolean successful;
String supplierName = getToken("Enter the supplier name:");
Supplier newSupplier = new Supplier(supplierName);
successful = supplierList.insertSupplier(newSupplier);
if (successful) {
System.out.println("Supplier with name " + newSupplier.getName() + " and ID " + newSupplier.get_ID() + " added successfully");
} else {
System.out.println("Issue adding supplier!");
}
}
public void showSupplierList() {
Iterator<Supplier> S_Iterator = supplierList.getSuppliers();
System.out.println("Printing Supplier List");
while (S_Iterator.hasNext()) {
Supplier item = S_Iterator.next();
System.out.println("ID: " + item.get_ID() + " Name: " + item.getName());
}
}
public void showSuppliersProduct() {
int productID = Integer.parseInt(getToken("Enter the product ID you wish to suppliers of: "));
Iterator<Supplier> S_Iterator = supplierList.getSuppliers();
System.out.println("Printing All Suppliers of that Product");
while (S_Iterator.hasNext()) {
Supplier item = S_Iterator.next();
Iterator<Supply> productSup = item.shipment();
while (productSup.hasNext()) {
Supply itemTwo = productSup.next();
if (productID == itemTwo.get_ID()) {
System.out.println("ID: " + item.get_ID() + " Name: " + item.getName());
}
}
}
}
public void showProductSuppliers() {
int supplierID = Integer.parseInt(getToken("Enter the ID of the supplier to get list of products"));
int position1 = supplierList.IDcheck(supplierID);
Supplier item = supplierList.get_listed_obj(position1);
item.print_list();
}
public void updatePrice() {
int supplierID = Integer.parseInt(getToken("Enter the ID of the supplier to get list of products"));
int position1 = supplierList.IDcheck(supplierID);
Supplier item = supplierList.get_listed_obj(position1);
item.print_list();
int id = Integer.parseInt(getToken("ID of product to add, delete or edit"));
int IDcheck = productList.IDcheck(id);
if (IDcheck == -1) {
System.out.println("No product with that ID");
} else {
String choice = getToken("INSERT, REMOVE, or CHANGE?");
switch (choice) {
case "INSERT":
String nameP = getToken("enter the product name");
String price_info = getToken("enter the product price information");
double price = Double.parseDouble(getToken("enter the product price"));
item.create_item(price, id, nameP, price_info);
break;
case "REMOVE":
item.delete_item(id);
break;
case "CHANGE":
item.grab_item(id);
break;
default:
System.out.println("no action chosen, none taken");
}
}
}
private void salesMenu() {
LoginState.instance().clear();
(WareContext.instance()).changeState(2); //go to clerk state
}
private void help() {
System.out.println("Enter a number between " + EXIT + " and " + HELP + " as explained below:");
System.out.println(ADD_PRODUCT + ": to add a product");
System.out.println(ADD_SUPPLIER + ": to add Supplier");
System.out.println(SHOW_SUPPLIER_LIST + ": to show supplier list");
System.out.println(SHOW_SUPPLIER_PRODUCTS + ": to display supplier of a product");
System.out.println(SHOW_PRODUCT_SUPPLIERS + ": to display product supplied by the supplier ");
System.out.println(UPDATE_PRODUCT_PRICE + ": to update product and price");
System.out.println(SALES_MENU + ": to switch to the Sales Clerk menu");
System.out.println(HELP + ": for help");
System.out.println(EXIT + ": to Exit\n");
}
public void logout() {
LoginState.instance().clear();
(WareContext.instance()).changeState(0); // exit
}
// @Override
// public void actionPerformed(ActionEvent e) {
// if (event.getSourcce().equals(this.addProductButton))
// AddProduct();
// }
public void process() {
boolean done = false;
int command;
help();
while (!done) {
switch (getCommand()) {
case ADD_PRODUCT: addProduct();
break;
case ADD_SUPPLIER: addSupplier();
break;
case SHOW_SUPPLIER_LIST: showSupplierList();
break;
case SHOW_SUPPLIER_PRODUCTS: showSuppliersProduct();
break;
case SHOW_PRODUCT_SUPPLIERS: showProductSuppliers();
break;
case UPDATE_PRODUCT_PRICE: updatePrice();
break;
case SALES_MENU: salesMenu();
done = true;
break;
case HELP: help();
break;
case EXIT: done = true;
}
}
logout();
}
/*
int command;
help();
while ((command = getCommand()) != EXIT) {
switch (command) {
case ADD_PRODUCT: addProduct();
break;
case ADD_SUPPLIER: addSupplier();
break;
case SHOW_SUPPLIER_LIST: showSupplierList();
break;
case SHOW_SUPPLIER_PRODUCTS: showSuppliersProduct();
break;
case SHOW_PRODUCT_SUPPLIERS: showProductSuppliers();
break;
case UPDATE_PRODUCT_PRICE: updatePrice();
break;
case SALES_MENU: salesMenu();
break;
case HELP: help();
break;
}
}
logout();
*/
public void run() {
process();
}
}