-
Notifications
You must be signed in to change notification settings - Fork 1
/
WarehouseContext.java
132 lines (116 loc) · 3.84 KB
/
WarehouseContext.java
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
import java.util.*;
import java.text.*;
import java.time.Year;
import java.io.*;
public class WarehouseContext {
private int currentState;
private static Warehouse warehouse;
private static WarehouseContext context;
private int currentClient;
private String clientID;
private BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
public static final int IsClient = 0;
public static final int IsManager = 1;
public static final int IsClerk = 2;
private WarehouseState[] states;
private int[][] nextState;
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);
}
private boolean yesOrNo(String prompt) {
String more = getToken(prompt + " (Y|y)[es] or anything else for no");
if (more.charAt(0) != 'y' && more.charAt(0) != 'Y') {
return false;
}
return true;
}
private void retrieve() {
try {
Warehouse tempWarehouse = Warehouse.retrieve();
if (tempWarehouse != null) {
System.out.println(" The warehouse has been successfully retrieved from the file WarehouseData \n" );
warehouse = tempWarehouse;
} else {
System.out.println("File doesnt exist; creating new warehouse" );
warehouse = Warehouse.instance();
}
} catch(Exception cnfe) {
cnfe.printStackTrace();
}
}
public void setLogin(int code)
{currentClient = code;}
public void setClient(String cID)
{ clientID = cID;}
public int getLogin()
{ return currentClient;}
public String getClient()
{ return clientID;}
private WarehouseContext() { //constructor
System.out.println("In WarehouseContext constructor");
if (yesOrNo("Look for saved data and use it?")) {
retrieve();
} else {
warehouse = Warehouse.instance();
}
// set up the FSM and transition table;
states = new WarehouseState[4];
states[0] = ClientState.instance();
states[1] = ManagerState.instance();
states[2]= ClerkMenuState.instance();
states[3]= Loginstate.instance();
nextState = new int[4][4];
nextState[0][0] = 3;nextState[0][1] = 3;nextState[0][2] = 3;nextState[0][3] = -2;
nextState[1][0] = 3;nextState[1][1] = 3;nextState[1][2] = 2;nextState[1][3] = -2;
nextState[2][0] = 0;nextState[2][1] = 3;nextState[2][2] = 3;nextState[2][3] = -2;
nextState[3][0] = 0;nextState[3][1] = 1;nextState[3][2] = 2;nextState[3][3] = -1;
currentState = 3;
}
public void changeState(int transition)
{
//System.out.println("current state " + currentState + " \n \n ");
currentState = nextState[currentState][transition];
if (currentState == -2)
{System.out.println("Error has occurred"); terminate();}
if (currentState == -1)
terminate();
//System.out.println("current state " + currentState + " \n \n ");
states[currentState].run();
}
private void terminate()
{
if (yesOrNo("Save data?")) {
if (warehouse.save()) {
System.out.println(" The warehouse has been successfully saved in the file WarehouseData \n" );
} else {
System.out.println(" There has been an error in saving \n" );
}
}
System.out.println(" Goodbye \n "); System.exit(0);
}
public static WarehouseContext instance() {
if (context == null) {
System.out.println("calling constructor");
context = new WarehouseContext();
}
return context;
}
public void process(){
states[currentState].run();
}
public static void main (String[] args){
WarehouseContext.instance().process();
}
}