-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClientUI.java
More file actions
191 lines (161 loc) · 6.2 KB
/
ClientUI.java
File metadata and controls
191 lines (161 loc) · 6.2 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
package com.company;
import javax.swing.*;
import java.awt.*;
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 ClientList clientList;
private ProductList productList;
private SupplierList supplierList;
private WaitlistList waitlists;
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 HELP = 8;
private JFrame frame;
private LogoutButton logoutButton = new LogoutButton();
private ClientUI() {
super();
supplierList = SupplierList.instance();
productList = ProductList.instance();
clientList = ClientList.instance();
waitlists = WaitlistList.instance();
logoutButton.setListener();
}
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: 8 for help"));
if (value >= EXIT ) {
return value;
}
} catch (NumberFormatException nfe) {
System.out.println("Enter a number");
}
} while (true);
}
public void ClientUIDisplay ()
{
System.out.println(DETAILS + ": to show Client Details");
System.out.println(PRODUCT_SHOW + ": to show Products");
System.out.println(TRANSACTION + ": to show Transaction History");
System.out.println(WAIT +": to show waitlist");
System.out.println(CART + ": to modify Cart");
System.out.println(EXIT + ": to Exit\n");
}
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();
boolean done = false;
while (!done)
{
switch(getCommand()) {
case DETAILS:
ClientInfo(loggedInClient);
break;
case PRODUCT_SHOW:
ShowProducts();
break;
case TRANSACTION:
ShowTransactionHistory(loggedInClient);
break;
case WAIT:
ShowWaitlist(loggedInClient);
break;
case CART:
done = true;
(WareContext.instance()).changeState(4);
break;
case HELP:
ClientUIDisplay();
break;
case EXIT: done = true;
break;
default:
System.out.println("Invalid command, try again");
}
}
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) {
clientList.PrintWaitList(client);
}
public void AddItemsToCart(Client client) {
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: "));
client.AddToCart(productToAdd, quant);
}
public void ChangeCartQuantity(Client client) {
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: "));
client.ChangeQuantity(productID, qty);
}
public void RemoveCartItem(Client client) {
int productID = Integer.parseInt(getToken("Please enter the product ID you wish to delete: "));
client.RemoveCartProduct(productID);
}
public void logout() {
LoginState.instance().clear();
(WareContext.instance()).changeState(0);
}
public void run() {
ClientProcess();
}
}