-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClerkState.java
More file actions
196 lines (185 loc) · 7.41 KB
/
ClerkState.java
File metadata and controls
196 lines (185 loc) · 7.41 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
package com.company;
import java.awt.*;
import java.util.*;
import java.text.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
public class ClerkState extends WareState {
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private WareContext context;
private static ClerkState instance;
private ClientList clientList;
private ProductList productList;
private SupplierList supplierList;
//functions
private static final int EXIT = 0;
private static final int ADD_CLIENT = 1;
private static final int SHOW_CLIENT_LIST = 2;
private static final int SHOW_PRODUCT_LIST = 3;
private static final int SHOW_INVOICE_LIST = 4;
private static final int SHOW_PRODUCT_WAITLIST = 5;
private static final int SHIPMENT = 6;
private static final int CLIENT_MENU = 7;
private static final int HELP = 8;
private static final int CHECKOUT = 9;
private JFrame frame;
private LogoutButton logoutButton = new LogoutButton();
private ClientButton clientButton = new ClientButton();
private ClerkState() {
super();
supplierList = SupplierList.instance();
productList = ProductList.instance();
clientList = ClientList.instance();
//context = WareContext.instance();
logoutButton.setListener();
clientButton.setListener();
}
public static ClerkState instance() {
if (instance == null) {
instance = new ClerkState();
}
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 addClient() {
boolean successful;
String clientName = getToken("Enter the client's name: ");
String clientAddress = getToken("Enter the client's address: ");
Client newClient = new Client(clientName, clientAddress); //DOESNT WANT TO CREATE THE CLIENT OR ADD TO LIST
successful = clientList.insertMember(newClient);
if (successful) {
System.out.println("Client with details (" + newClient.toString() + ") added successfully");
} else {
System.out.println("Issue adding client!");
}
}
public void clientLister () {
LoginState.instance().clear();
(WareContext.instance()).changeState(5);
}
public void productLister () {
Iterator<Product> P_Iterator = productList.getProducts();
System.out.println("Printing Product List");
while (P_Iterator.hasNext()) {
Product item = P_Iterator.next();
System.out.println("ID: " + item.getId() + " Price: " + item.getPrice() + " Quantity: " + item.getQuantity());
}
}
public void shipment () {
int supplierID = Integer.parseInt(getToken("Enter the ID of the supplier to receive shipment from"));
int position1 = supplierList.IDcheck(supplierID);
Supplier shipper = supplierList.get_listed_obj(position1);
Iterator<Supply> shipment = shipper.shipment();
while (shipment.hasNext()) {
Supply item = shipment.next();
int productID = item.print_name_id();
int quantity = Integer.parseInt(getToken("Enter the quantity received of the product (0 if none)"));
int position2 = productList.IDcheck(productID);
Product prod = productList.get_listed_obj(position2);
quantity = quantity + prod.getQuantity();
prod.setQuantity(quantity);
}
}
public void invoiceLister () {
int clientID = Integer.parseInt(getToken("Enter the ID of a client, to get invoices"));
int position = clientList.IDcheck(clientID);
if (position == -1) {
System.out.println("Invalid ID, potentially no clients exist, add a client before trying to get invoices");
} else {
Client item = clientList.get_listed_obj(position);
item.DisplayInvoices();
}
}
public void productWait () {
System.out.println("Not implemented");
}
private void salesMenu(){
LoginState.instance().clear();
int UID = Integer.parseInt(getToken("Enter the ID of the client to login as: "));
(WareContext.instance()).setUID(UID);
(WareContext.instance()).changeState(1); //go to clerk state
}
private void Checkout()
{
int ID = Integer.parseInt(getToken("What is the ID of the client wishing to checkout?"));
int position = clientList.IDcheck(ID);
Client client = clientList.get_listed_obj(position);
client.newInvoice();
}
public void logout(){
LoginState.instance().clear();
(WareContext.instance()).changeState(0); // exit
}
private void help () {
System.out.println("Enter a number between " + EXIT + " and " + HELP + " as explained below:");
System.out.println(ADD_CLIENT+ ": to add a client");
System.out.println(SHOW_CLIENT_LIST + ": to show client list");
System.out.println(SHOW_PRODUCT_LIST+ ": to product list");
System.out.println(SHOW_INVOICE_LIST + ": to display invoice list");
System.out.println(SHOW_PRODUCT_WAITLIST + ": to display product waitlist");
System.out.println(SHIPMENT + ": receive shipment");
System.out.println(CLIENT_MENU + ": to switch to the Client menu");
System.out.println(HELP + ": for help");
System.out.println(CHECKOUT + ": to checkout a client");
System.out.println(EXIT + ": to Exit\n");
}
public void process() {
help();
boolean done = false;
while (!done){
switch (getCommand()) {
case ADD_CLIENT: addClient();
break;
case SHOW_CLIENT_LIST: clientLister();
break;
case SHOW_PRODUCT_LIST: productLister();
break;
case SHOW_INVOICE_LIST: invoiceLister();
break;
case SHOW_PRODUCT_WAITLIST: productWait();
break;
case SHIPMENT: shipment();
break;
case CHECKOUT: Checkout();
break;
case CLIENT_MENU:
salesMenu();
done = true;
break;
case HELP: help();
break;
case EXIT: done = true;
}
}
logout();
}
public void run() {
process();
}
}