-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInterface.java
More file actions
304 lines (283 loc) · 9 KB
/
UserInterface.java
File metadata and controls
304 lines (283 loc) · 9 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
293
294
295
296
297
298
299
300
301
302
303
304
import java.util.*;
import java.text.*;
import java.io.*;
public class UserInterface implements Serializable {
private static final long serialVersionUID = 1L;
private static UserInterface userInterface;
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static Warehouse warehouse;
//commands
private static final int EXIT = 0;
private static final int ADD_CLIENT = 1;
private static final int ADD_MANUFACTURER = 2;
private static final int ADD_PRODUCTS = 3;
private static final int ASSIGN_PRODUCT = 4;
private static final int UNASSIGN_PRODUCT = 5;
private static final int SHOW_CLIENTS = 6;
private static final int SHOW_MANUFACTURERS = 7;
private static final int SHOW_PRODUCTS = 8;
private static final int SHOW_SUPPLIERS = 9;
private static final int SHOW_SUPPLIES = 10;
private static final int SAVE =11;
private static final int HELP = 12;
public UserInterface() {
if (yesOrNo("Look for saved data and use it?")) {
retrieve();
}
else {
warehouse = Warehouse.instance();
}
process();
}
public static UserInterface instance() {
if (userInterface == null) {
return userInterface = new UserInterface();
} else {
return userInterface;
}
}
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 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 double getDouble(String prompt) {
do {
try {
String item = getToken(prompt);
Double num = Double.valueOf(item);
return num.doubleValue();
} catch (NumberFormatException nfe) {
System.out.println("Please input a number ");
}
} while (true);
}
public boolean yesOrNo(String prompt) {
String more = getToken(prompt + " (Y/N)");
return more.charAt(0) == 'y' || more.charAt(0) == 'Y';
}
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("Please retry with a number");
}
} while (true);
}
public void help() {
System.out.println("Enter a number between 0 and 11:");
System.out.println(EXIT + " to Exit");
System.out.println(ADD_CLIENT + " to add a new client");
System.out.println(ADD_MANUFACTURER + " to add a new manufacturer");
System.out.println(ADD_PRODUCTS + " to add new products");
System.out.println(ASSIGN_PRODUCT + " to assign a product to a manufacturer");
System.out.println(UNASSIGN_PRODUCT + " to unassign a product to a manufacturer");
System.out.println(SHOW_CLIENTS + " to show a clients based on appropriate fields");
System.out.println(SHOW_MANUFACTURERS + " to show manufacturers based on appropriate fields");
System.out.println(SHOW_PRODUCTS + " to show products based on appropriate fields");
System.out.println(SHOW_SUPPLIERS + " to show suppliers and prices of a part");
System.out.println(SHOW_SUPPLIES + " to show manufacturers and all products they offer");
System.out.println(SAVE + " to save the warehouse");
System.out.println(HELP + " for help");
}
public void addClient() {
String name = getToken("Enter client name ");
int id = getNumber("Enter client ID ");
float balance = getNumber("Enter client balance ");
Client result;
result = warehouse.addClient(name, id, balance);
if (result == null) {
System.out.println("Could not add member");
}
System.out.println(result.toString());
}
public void addManufacturer() {
String name = getToken("Enter manufacturer name ");
Manufacturer result;
result = warehouse.addMannufacturer(name);
if (result == null) {
System.out.println("Could not add manufacturer");
}
System.out.println(result.toString());
}
public void addProducts() {
Product result;
do {
String name = getToken("Enter product name ");
int id = getNumber("Enter product ID ");
result = warehouse.addProduct(name, id);
if (result == null) {
System.out.println("Could not add manufacturer");
}
System.out.println(result.toString());
if (!yesOrNo("Add more products?")) {
break;
}
} while (true);
}
public void assignProduct() {
int id = getNumber("Enter product ID ");
Product p = warehouse.searchProduct(id);
if (p != null) { // we found it
id = getNumber("Enter manufacturer ID ");
Manufacturer m = warehouse.searchManufacturer(id);
if (m != null) {
Offer o = warehouse.searchOffer(p.getID(), m.getManufacturerID());
if (o == null) {
double price = getDouble("Enter the offer's price");
Offer offer = warehouse.addOffer(price, p.getID(), m.getManufacturerID());
System.out.println(offer.toString());
} else
System.out.println("Offer for product from manufacturer already exists");
} else
System.out.println("Could not find the manufacturer ID");
} else
System.out.println("Could not find the product ID");
}
public void unassignProduct() {
int id = getNumber("Enter product ID ");
Product p = warehouse.searchProduct(id);
if (p != null) { // we found it
id = getNumber("Enter manufacturer ID ");
Manufacturer m = warehouse.searchManufacturer(id);
if (m != null) {
Offer offer = warehouse.searchOffer(p.getID(), m.getManufacturerID());
if (offer != null) {
warehouse.removeOffer(p, m, offer);
System.out.println("Offer has been unassigned");
}
} else
System.out.println("Could not find the manufacturer ID");
} else
System.out.println("Could not find the product ID");
}
public void showManufacturers() {
Iterator allMaufacturers = warehouse.getManufacterers();
while (allMaufacturers.hasNext()) {
Manufacturer manufacturer = (Manufacturer) (allMaufacturers.next());
System.out.println(manufacturer.toString());
}
}
public void showProducts() {
Iterator allProducts = warehouse.getProducts();
while (allProducts.hasNext()) {
Product product = (Product) (allProducts.next());
System.out.println(product.toString());
}
}
public void showClients() {
Iterator allClients = warehouse.getClients();
while (allClients.hasNext()) {
Client client = (Client) (allClients.next());
System.out.println(client.toString());
}
}
public void showSupplied() {
int id;
id = getNumber("Please enter product Id: ");
Product p = warehouse.searchProduct(id);
if (p != null) {
Iterator allOffers = warehouse.getSuppliers(p);
while (allOffers.hasNext()) {
Offer offer = (Offer)(allOffers.next());
System.out.println(offer.toString());
}
}
else
System.out.println("Could not find product");
}
public void showSupplies() {
Iterator allMaufacturers = warehouse.getManufacterers();
while (allMaufacturers.hasNext()) {
Manufacturer manufacturer = (Manufacturer)(allMaufacturers.next());
Iterator allOffers = manufacturer.getOffers();
while (allOffers.hasNext()) {
Offer offer = (Offer)(allOffers.next());
System.out.println(offer.toString());
}
}
}
public void save() {
if (Warehouse.save()) {
System.out.println("Warehouse has been saved");
}
else {
System.out.println("Error while saving");
}
}
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 process() {
int command;
help();
while ((command = getCommand()) != EXIT) {
switch (command) {
case ADD_CLIENT: addClient();
break;
case ADD_MANUFACTURER: addManufacturer();
break;
case ADD_PRODUCTS: addProducts();
break;
case HELP: help();
break;
case ASSIGN_PRODUCT: assignProduct();
break;
case UNASSIGN_PRODUCT: assignProduct();
break;
case SHOW_CLIENTS: showClients();
break;
case SHOW_MANUFACTURERS: showManufacturers();
break;
case SHOW_PRODUCTS: showProducts();
break;
case SHOW_SUPPLIERS: showSupplied();
break;
case SHOW_SUPPLIES: showSupplies();
break;
case SAVE: save();
break;
}
}
}
public static void main(String[] args) {
UserInterface.instance();
}
}