-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientUI.java
More file actions
178 lines (157 loc) · 4.94 KB
/
ClientUI.java
File metadata and controls
178 lines (157 loc) · 4.94 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
import java.util.*;
import java.io.*;
public class ClientUI extends WareState {
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private WareContext context;
private static ClientUI instance;
private WaitList waitList;
private ClientList clientList;
private ProductList productList;
private SupplierList supplierList;
private static final int EXIT = 0;
private static final int DETAILS = 1;
private static final int PRODUCT_SHOW= 2;
private static final int TRANSACTION = 3;
private static final int WAIT = 4;
private static final int CART = 5;
private static final int ORDER = 6;
private static final int PAY = 7;
private static final int HELP = 8;
private ClientUI() {
super();
supplierList = SupplierList.instance();
productList = ProductList.instance();
clientList = ClientList.instance();
waitList = WaitList.instance();
}
public static ClientUI instance() {
if (instance == null) {
instance = new ClientUI();
}
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 ClientUIDisplay ()
{
System.out.println("1: Show Client Details");
System.out.println("2: Show Products");
System.out.println("3: Show Transaction History");
System.out.println("4: Show Waitlist");
System.out.println("5: Edit Cart State");
System.out.println("6: Turn cart into order");
System.out.println("7: Pay an order");
System.out.println("0: Logout");
}
public void ClientProcess () {
int position = clientList.IDcheck((WareContext.instance()).getUID());
if (position == -1 && (WareContext.instance()).getUID() == 1) {
System.out.println("Adding dummy client as no client currently exist");
Client dummy = new Client("Joe Schmoe", "123 Nowheres Ville");
clientList.insertMember(dummy);
position = 0;
} else if (position == -1) {
System.out.println("Invalid ID, logging you out, try 1");
logout();
}
Client loggedInClient = clientList.get_listed_obj(position);
int clientCommand = 1;
ClientUIDisplay();
while ((clientCommand = getCommand()) != 0)
{
switch(clientCommand) {
case DETAILS:
ClientInfo(loggedInClient);
break;
case PRODUCT_SHOW:
ShowProducts();
break;
case TRANSACTION:
ShowTransactionHistory(loggedInClient);
break;
case WAIT:
ShowWaitlist(loggedInClient);
break;
case CART:
CartState();
break;
case ORDER:
addOrder(loggedInClient);
break;
case PAY:
makePayment(loggedInClient);
break;
}
}
logout();
}
public void ClientInfo (Client client) {
System.out.println("ID: " + client.getId());
System.out.println("Name: " + client.getName());
System.out.println("Address: " + client.getAddress());
}
public void ShowProducts() {
Iterator<Product> P_Iterator = productList.getProducts();
System.out.println("Printing Supplier List");
while (P_Iterator.hasNext()) {
Product item = P_Iterator.next();
System.out.println("Name: " + item.getName());
System.out.println("ID: " + item.getId());
System.out.println("Price: " + item.getPrice());
System.out.println("------------------------------");
}
}
public void ShowTransactionHistory(Client client) {
client.DisplayInvoices();
}
public void ShowWaitlist(Client client) {
Iterator<Wait> W_Iterator = waitList.getWaits();
int id = client.getId();
System.out.println("Printing Client's wait list");
while (W_Iterator.hasNext()) {
Wait item = W_Iterator.next();
if ((item.getClient()).getId()== id)
System.out.println("product id: " + (item.getProduct()).getId() + " product name: " + (item.getProduct()).getName() + " quantity: " + item.getQuantity());
}
}
public void CartState() {
(WareContext.instance()).changeState(4);
}
public void addOrder (Client item) {
clientList.GenerateOrder(item);
}
public void makePayment (Client item) {
int invoiceID = Integer.parseInt(getToken("Enter ID of invoice to pay"));
item.PayInvoice(invoiceID);
}
public void logout() {
(WareContext.instance()).changeState(0);
}
public void run() {
ClientProcess();
}
}