-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClientQuery.java
More file actions
131 lines (116 loc) · 3.98 KB
/
ClientQuery.java
File metadata and controls
131 lines (116 loc) · 3.98 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
package com.company;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.StringTokenizer;
public class ClientQuery extends WareState{
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private WareContext context;
private static ClientQuery instance;
private ClientList clientList;
private static final int EXIT = 0;
private static final int ALL = 1;
private static final int OUTSTANDING = 2;
private static final int NO_TRANSACTION = 3;
private static final int HELP = 8;
private ClientQuery() {
super();
clientList = ClientList.instance();
}
public static ClientQuery instance() {
if (instance == null) {
instance = new ClientQuery();
}
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 CQDisplay () {
System.out.println("1: Show All Clients");
System.out.println("2: Show Clients With Outstanding Balance");
System.out.println("3: Show Clients With No Transactions");
System.out.println("0: Logout");
}
public void DisplayAllClients()
{
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 DisplayOutstandingInvoiceClients()
{
Iterator<Client> C_Iterator = clientList.getClients();
System.out.println("Printing Client List");
while (C_Iterator.hasNext()) {
Client item = C_Iterator.next();
if (item.NumOutstandingInvoices() > 0)
System.out.println("ID: " + item.getId() + " Name: " + item.getName() + " Address: " + item.getAddress());
}
}
public void DisplayNoTransactionClients()
{
Iterator<Client> C_Iterator = clientList.getClients();
System.out.println("Printing Client List");
while (C_Iterator.hasNext()) {
Client item = C_Iterator.next();
if (item.NumInvoices() == 0)
System.out.println("ID: " + item.getId() + " Name: " + item.getName() + " Address: " + item.getAddress());
}
}
public void logout()
{
LoginState.instance().clear();
(WareContext.instance()).changeState(2);
}
public void CQProcess()
{
int command = 1;
CQDisplay();
while ((command = getCommand()) != EXIT){
switch (command) {
case ALL: DisplayAllClients();
break;
case OUTSTANDING: DisplayOutstandingInvoiceClients();
break;
case NO_TRANSACTION: DisplayNoTransactionClients();
break;
case HELP: CQDisplay();
break;
default:
System.out.println("Invalid input, try again");
}
}
logout();
}
public void run() {
CQProcess();
}
}