Newer
Older
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
package ufrn.imd.utils;
import lombok.Data;
import ufrn.imd.controller.Controller;
import ufrn.imd.domain.Client;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Scanner;
import static ufrn.imd.utils.NameSpace.HOST_DEPOSIT_SERVICE;
@Data
public class MachineState {
public static final String INITIAL= "INITIAL";
public static final String RENDER = "RENDER";
public static final String UPDATE_STATE = "UPDATE_STATE";
public static final String READ = "READ";
public static final String DEPOSIT = "DEPOSIT";
public static final String BALANCE = "BALANCE";
public static final String HELP = "HELP";
public static final String INVALID_INPUT = "INVALID_INPUT";
public static final String QUIT = "QUIT";
private String state;
private Scanner sc;
private String inputValue;
public MachineState() {
state = INITIAL;
inputValue = new String();
sc = new Scanner(System.in);
}
public Boolean quit() {
return !state.equals(QUIT);
}
public void update() {
if(state.equals(INITIAL)) {
state = HELP;
} else if(state.equals(HELP)) {
state = READ;
} else if(state.equals(READ)) {
state = inputValue;
} else if(state.equals(INVALID_INPUT)) {
state = HELP;
} else if(state.equals(DEPOSIT)) {
state = HELP;
}
}
public void processEvents() throws MalformedURLException, NotBoundException, RemoteException {
if(state.equals(READ)) {
inputValue = new String();
inputValue = sc.nextLine().trim().toUpperCase();
System.out.println(String.format(">>> input value: %s", inputValue));
if(!inputValue.equals(DEPOSIT) && !inputValue.equals(DEPOSIT) && !inputValue.equals(BALANCE)){
inputValue = INVALID_INPUT;
}
} else if(state.equals(DEPOSIT)) {
double v = sc.nextDouble();
System.out.println(String.format(">>> input value: %.2f", v));
Client client = new Client(1);
Controller server = (Controller) Naming.lookup(HOST_DEPOSIT_SERVICE);
server.registerClient(client);
}
}
public void render() {
try {
if(state.equals(INITIAL)) {
System.out.println(">>> Initializing...");
Thread.sleep(1000);
} else if(state.equals(HELP)) {
System.out.println("These are the avaliable operations");
System.out.println("---------------------------------");
System.out.println(" - deposit \t - balance");
System.out.println(" - quit");
System.out.println("---------------------------------");
Thread.sleep(1000);
} else if(state.equals(READ)) {
System.out.println(">>> Please enter with wich operation do you want");
} else if(state.equals(INVALID_INPUT)) {
System.out.println(">>> Please enter with a VALID operation");
Thread.sleep(3000);
} else if(state.equals(DEPOSIT)) {
System.out.println(">>> Starting deposit operation");
System.out.println(">>> Please enter with the value that you want deposit");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}