-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWareContext.java
More file actions
87 lines (83 loc) · 2.66 KB
/
WareContext.java
File metadata and controls
87 lines (83 loc) · 2.66 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
import java.util.*;
import java.text.*;
import java.io.*;
public class WareContext {
private int currentState;
private static WareContext context;
private int currentUser = 0;
private int userID;
private WareState[] states;
private int[][] nextState;
private WareContext() {
states = new WareState[6];
states[0] = LoginState.instance();
states[1] = ClientUI.instance();
states[2] = ClerkState.instance();
states[3] = ManagerState.instance();
states[4] = CartState.instance();
states[5] = QueryState.instance();
nextState = new int[6][6];
nextState[0][0]= -1;nextState[0][1]= 1;nextState[0][2]= 2;nextState[0][3]= 3;nextState[0][4]= -2;nextState[0][5]= -2;
nextState[1][0]= 0;nextState[1][1]= -2;nextState[1][2]= -2;nextState[1][3]= -2;nextState[1][4]= 4;nextState[1][5]= -2;
nextState[2][0]= 0;nextState[2][1]= 6;nextState[2][2]= -2;nextState[2][3]= -2;nextState[2][4]= -2;nextState[2][5]= 5;
nextState[3][0]= 0;nextState[3][1]= -2;nextState[3][2]= 6;nextState[3][3]= -2;nextState[3][4]= -2;nextState[3][5]= -2;
nextState[4][0]= -2;nextState[4][1]= 1;nextState[4][2]= -2;nextState[4][3]= -2;nextState[4][4]= -2;nextState[4][5]= -2;
nextState[5][0]= -2;nextState[5][1]= -2;nextState[5][2]= 2;nextState[5][3]= -2;nextState[5][4]= -2;nextState[5][5]= -2;
currentState = 0;
}
public void changeState(int transition) {
currentState = nextState[currentState][transition];
if (currentState == -2) {
System.out.println("Error has occurred");
terminate();
}
if (currentState == -1)
terminate();
if (currentState == 6)
saveUser(transition);
if (currentState == 0)
savedUser();
states[currentState].run();
}
private void terminate() {
System.out.println(" Goodbye \n ");
System.exit(0);
}
private void saveUser (int user) {
currentUser += user;
currentState = user;
//currentUser = 1, clerk as client
//currentUser = 2, manager as clerk
//currentUser = 3, manager as clerk as client
}
private void savedUser () {
if (currentUser == 2) {
currentUser -= 2;
currentState = 3;
} else if (currentUser == 1 || currentUser == 3) {
--currentUser;
currentState = 2;
}
// manager as clerk back to manager
//(manager as) clerk as client back to clerk
}
public void setUID (int uid) {
userID = uid;
}
public int getUID () {
return userID;
}
public static WareContext instance() {
if (context == null) {
return context = new WareContext();
} else {
return context;
}
}
public void process() {
states[currentState].run();
}
public static void main(String[] args) {
WareContext.instance().process();
}
}