-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryState.java
More file actions
126 lines (114 loc) · 3.53 KB
/
QueryState.java
File metadata and controls
126 lines (114 loc) · 3.53 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
import java.util.*;
import java.text.*;
import java.io.*;
public class QueryState 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 QueryState instance;
private static final int LOGOUT = 0;
private static final int ALL = 1;
private static final int WITH_BALANCE = 2;
private static final int NO_HISTORY = 3;
private static final int HELP = 4;
private QueryState() {
super();
supplierList = SupplierList.instance();
productList = ProductList.instance();
clientList = ClientList.instance();
waitList = WaitList.instance();
}
public static QueryState instance() {
if (instance == null) {
instance = new QueryState();
}
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 >= LOGOUT && 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 3 as explained below:");
System.out.println(LOGOUT + " to return to Clerk");
System.out.println(ALL + " to prrint all clients.");
System.out.println(WITH_BALANCE + " to prints only clients with outstanding balance");
System.out.println(NO_HISTORY + " to print only clients with no transaction history.");
}
public void all() {
Iterator<Client> C_Iterator = clientList.getClients();
System.out.println("Printing Client List");
while (C_Iterator.hasNext()) {
Client item = C_Iterator.next();
System.out.println("ID: " + item.getId() + " Name: " + item.getName() + " Address: " + item.getAddress());
}
}
public void bal() {
Iterator<Client> C_Iterator = clientList.getClients();
System.out.println("Printing Client List With only Outstanding balances");
while (C_Iterator.hasNext()) {
Client item = C_Iterator.next();
if (item.boolBalance())
System.out.println("ID: " + item.getId() + " Name: " + item.getName() + " Address: " + item.getAddress());
}
}
public void no() {
Iterator<Client> C_Iterator = clientList.getClients();
System.out.println("Printing Client List with only no transaction histories");
while (C_Iterator.hasNext()) {
Client item = C_Iterator.next();
if (item.boolHistory())
System.out.println("ID: " + item.getId() + " Name: " + item.getName() + " Address: " + item.getAddress());
}
}
public void logout() {
(WareContext.instance()).changeState(2);
}
public void process() {
int command;
help();
while ((command = getCommand()) != LOGOUT){
switch (command) {
case ALL:
all();
break;
case WITH_BALANCE:
bal();
break;
case NO_HISTORY:
no();
break;
default:
System.out.println("Not a valid command");
}
}
logout();
}
public void run() {
process();
}
}