-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartState.java
More file actions
135 lines (123 loc) · 3.86 KB
/
CartState.java
File metadata and controls
135 lines (123 loc) · 3.86 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
import java.util.*;
import java.text.*;
import java.io.*;
public class CartState extends WareState {
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private WaitList waitList;
private ClientList clientList;
private ProductList productList;
private SupplierList supplierList;
private static CartState instance;
private static final int EXIT = 0;
private static final int ADD = 1;
private static final int EDIT = 2;
private static final int DELETE = 3;
private static final int VIEW = 4;
private static final int HELP = 5;
private CartState() {
super();
supplierList = SupplierList.instance();
productList = ProductList.instance();
clientList = ClientList.instance();
waitList = WaitList.instance();
}
public static CartState instance() {
if (instance == null) {
instance = new CartState();
}
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 help() {
System.out.println("Enter a number between 0 and 4 as explained below:");
System.out.println(EXIT + " to Client State\n");
System.out.println(ADD + " to too add to cart.");
System.out.println(EDIT + " to edit product in cart.");
System.out.println(DELETE + " to remove a itme from the cart.");
System.out.println(VIEW + " to view the cart.");
}
public void add() {
int clientID = (WareContext.instance()).getUID();
int position = clientList.IDcheck(clientID);
Client item = clientList.get_listed_obj(position);
int productToAddID = Integer.parseInt(getToken("Please enter the product ID you wish to add: "));
Product productToAdd = productList.search(productToAddID);
int quant = Integer.parseInt(getToken("Enter the quantity to be added to the cart: "));
item.AddToCart(productToAdd, quant);
}
public void edit() {
int clientID = (WareContext.instance()).getUID();
int position = clientList.IDcheck(clientID);
Client item = clientList.get_listed_obj(position);
int productID = Integer.parseInt(getToken("Enter the product ID you wish to change the quantity of: "));
int qty = Integer.parseInt(getToken("Enter the new quantity: "));
item.ChangeQuantity(productID, qty);
}
public void delete() {
int clientID = (WareContext.instance()).getUID();
int position = clientList.IDcheck(clientID);
Client item = clientList.get_listed_obj(position);
int productID = Integer.parseInt(getToken("Please enter the product ID you wish to delete: "));
item.RemoveCartProduct(productID);
}
public void view() {
int clientID = (WareContext.instance()).getUID();
int position = clientList.IDcheck(clientID);
Client item = clientList.get_listed_obj(position);
item.DisplayCart();
}
public void logout() {
(WareContext.instance()).changeState(1);
}
public void process() {
int command;
help();
while ((command = getCommand()) != EXIT){
switch (command) {
case ADD:
add();
break;
case EDIT:
edit();
break;
case DELETE:
delete();
break;
case VIEW:
view();
break;
default:
System.out.println("Not a valid command");
}
}
logout();
}
public void run() {
process();
}
}