-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGumballMachine.java
More file actions
123 lines (103 loc) · 2.35 KB
/
GumballMachine.java
File metadata and controls
123 lines (103 loc) · 2.35 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
112
113
114
115
116
117
118
119
120
121
122
123
public class GumballMachine implements IGumballMachine
{
State soldOutState;
State noMoneyState;
State hasMoneyState;
State soldState;
State state = soldOutState;
int count = 0;
boolean slotAvailability=false;
double totalMoney=0;
int slotCount=0;
public GumballMachine(int numberGumballs) {
soldOutState = new SoldOutState(this);
noMoneyState = new NoMoneyState(this);
hasMoneyState = new HasMoneyState(this);
soldState = new SoldState(this);
this.count = numberGumballs;
if (numberGumballs > 0) {
state = noMoneyState;
}
}
public void insertQuarter() {
totalMoney = totalMoney + 0.25;
state.insertMoney();
}
public void ejectQuarter() {
state.ejectMoney();
}
public void insertNickle()
{
totalMoney = totalMoney + 0.05;
state.insertMoney();
}
public void insertDime()
{
totalMoney = totalMoney + 0.10;
state.insertMoney();
}
public void turnCrank() {
state.turnCrank();
state.dispense();
}
public boolean isGumballInSlot()
{
if(slotCount>=1)
{
return true;
}
return false;
}
public void takeGumballFromSlot()
{
if(slotCount>=1)
{
slotCount=0;
}
}
void setState(State state) {
this.state = state;
}
void releaseBall() {
count = count - 1;
totalMoney = totalMoney-0.50;
System.out.println("A gumball comes rolling out the slot...");
System.out.println("Your total change is: " + totalMoney + " Please collect it.");
totalMoney=0;
takeGumballFromSlot();
}
int getCount() {
return count;
}
void refill(int count) {
this.count = count;
state = noMoneyState;
}
public State getState() {
return state;
}
public State getSoldOutState() {
return soldOutState;
}
public State getNoMoneyState() {
return noMoneyState;
}
public State getHasQuarterState() {
return hasMoneyState;
}
public State getSoldState() {
return soldState;
}
public String toString() {
StringBuffer result = new StringBuffer();
result.append("\nMighty Gumball, Inc.");
result.append("\nJava-enabled Standing Gumball Model #2004");
result.append("\nInventory: " + count + " gumball");
if (count != 1) {
result.append("s");
}
result.append("\n");
result.append("Machine is " + state + "\n");
return result.toString();
}
}