-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.java
More file actions
89 lines (65 loc) · 2.7 KB
/
client.java
File metadata and controls
89 lines (65 loc) · 2.7 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
public interface VendingMachineState
{
public void selectProductAndInsertMoney(int amount, String productName);
public void dispenseProduct();
}
public class NoMoneyState implements VendingMachine{
public void selectProductAndInsertMoney(int amount, String productName){
System.out.println(amount + "USD were inserted and " + productName + " was selected.");
}
public void dispenseProduct(){
System.out.println("Machine is unable to dispense product because product was not selected.");
}
}
public class HasMoneyState implements VendingMachine{
public void selectProductAndInsertMoney(int amount, String productName){
System.out.println("Machine has money and product selected. Please wait... ");
}
}
public void dispenseProduct(){
System.out.println("Product dispensed...");
}
}
public class VendingMachine implements VendingMachineState
{
private VendingMachineState vendingMachineState;
public VendingMachine()
{
vendingMachineState = new NoMoneyState();
}
public VendingMachineState getVendingMachineState(){
return vendingMachineState;
}
public void setVendingMachineState(VendingMachineState vendingMachineState){
this.vendingMachineState = vendingMachineState;
}
public void selectProductAndInsertMoney(int amount, String productName){
vendingMachineState.selectProductAndInsertMoney(amount, productName);
VendingMachineState hasMoneyState = new HasMoneyState();
if(vendingMachineState instanceof NoMondeyState){
setVendingMachineState(hasMoneyState);
System.out.println("Machine state moved to : "+ vendingMachineState.getClass().getName());
}
}
public void dispenseProduct(){
VendingMachineState noMoneyState = new NoMoneyState();
vendingMachineState.dispenseProduct();
if(vendingMachineState instanceof HasMoneyState){
setVendingMachineState(noMoneyState);
System.out.println("Machine state moved to : "+ vendingMachineState.getClass().getName());
}
}
}
/* Main Driver */
public class Client{
public static void main(String[] args){
VendingMachine vendingMachine = new VendingMachine();
System.out.println("Machine current state : "+ vendingMachine.getVendingMachineState().getClass().getName()+"\n");
vendingMachine.dispenseProduct();
vendingMachine.selectProductAndInsertMoney(400, "Coca-cola");
System.out.println("Machine actual state : " + vendingMachine.getVendingMachineState().getClass().getName()+"\n");
vendingMachine.selectProductAndInsertMoney(200, "DrPepper");
vendingMachine.dispenseProduct();
System.out.println("\nMachine current state : "+ vendingMachine.getVendingMachineState().getClass().getName());
}
}