-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWareContext.java
More file actions
111 lines (100 loc) · 3.69 KB
/
WareContext.java
File metadata and controls
111 lines (100 loc) · 3.69 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
package com.company;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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 static JFrame wareFrame;
private WareContext() {
states = new WareState[6];
states[0] = LoginState.instance();
states[1] = ClientUI.instance();
states[2] = ClerkState.instance();
states[3] = ManagerState.instance();
states[4] = ModifyCartState.instance();
states[5] = ClientQuery.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]= 4;nextState[2][2]= -2;nextState[2][3]= -2;nextState[2][4]= -2;nextState[2][5]= 4;
nextState[3][0]= 0;nextState[3][1]= -2;nextState[3][2]= 4;nextState[3][3]= -2;nextState[3][4]= -2;nextState[3][5]= -2;
nextState[4][0]= 0;nextState[4][1]= 4;nextState[4][2]= -2;nextState[4][3]= -2;nextState[4][4]= -2;nextState[4][5]= -2;
nextState[5][0]= 0;nextState[5][1]= -2;nextState[5][2]= 4;nextState[5][3]= -2;nextState[5][4]= -2;nextState[5][5]=-2;
currentState = 0;
wareFrame = new JFrame("Warehouse GUI");
wareFrame.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e){System.exit(0);}});
wareFrame.setSize(400,400);
wareFrame.setLocation(400,400);
}
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 == 4)
saveUser(transition);
if (currentState == 0)
savedUser();
System.out.println(currentState);
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
//currentUser = 4, Clerk's modify cart state
//currentUser = 5, ClientQuery state
}
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 JFrame getFrame()
{
return wareFrame;
}
public void process() {
states[currentState].run();
}
public static void main(String[] args) {
WareContext.instance().process();
}
}