-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path123
More file actions
executable file
·190 lines (180 loc) · 4.96 KB
/
123
File metadata and controls
executable file
·190 lines (180 loc) · 4.96 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
error_reporting(1);
ini_set("display_errors", 1);
$gumballMachine = new gumballMachine(5);
$gumballMachine->insertQuarter();
$gumballMachine->turnCrank();
var_dump($gumballMachine);
class gumballMachine{
public $soldOutState;
public $noQuarterState;
public $hasQuarterState;
public $soldState;
public $winnerState;
public $state;
public $count = 0;
public function __construct($number){var_dump(new SoldOutState( $this ));die;
$this->soldOutState = new SoldOutState( $this );
$this->noQuarterState = new NoQuarterState( $this );
$this->hasQuarterState = new HasQuarterState( $this );
$this->soldState = new SlodState( $this );
$this->winnerState = new WinnerState( $this );
$this->count = $number;
if($number > 0){
$this->state = $this->noQuarterState;
}
}
public function insertQuarter(){
$this->state->insertQuarter();
}
public function ejectQuarter(){
$this->state->ejectQuarter();
}
public function turnCrank(){
$this->state->turnCrank();
$this->state->dispense();
}
public function setState($state){
$this->state = $state;
}
public function releaseBall(){
print ("a gumball comes rolling out the slot");
if($this->count != 0){
$this->count = $this->count -1;
}
}
public function getCount(){
return $this->count;
}
public function getNoQuarterState(){
return $this->noQuarterState;
}
}
interface State{
public function insertQuarter();
public function ejectQuarter();
public function turnCrank();
public function dispense();
}
class WinnerState implements State{
public $gumballMachine;
public function __construct($gumballMachine){
$this->gumballMachine = $gumballMachine;
}
public function insertQuarter(){
print "winner m";
}
public function ejectQuarter(){
print "winner the crank";
}
public function turnCrank(){
print "twinnerther gumball";
}
public function dispense(){
print "winner dispense";
$this->gumballMachine->releaseBall();
if($this->gumballMachine->getCount() == 0){
$this->gumballMachine->setState($this->gumballMachine->getSoldOutState());
}else{
$this->gumballMachine->releaseBall();
if($this->gumballMachine->getCount() > 0){
$this->gumballMachine->setState($this->gumballMachine->getNoQuarterState());
}else{
print "no gumballs";
$this->gumballMachine->setState($this->gumballMachine->getSoldOutState());
}
}
}
}
class SlodState implements State{
public $gumballMachine;
public function __construct($gumballMachine){
$this->gumballMachine = $gumballMachine;
}
public function insertQuarter(){
print "wait, already gire gum";
}
public function ejectQuarter(){
print "sorry,you already turned the crank";
}
public function turnCrank(){
print "turning twice doesnt get you another gumball";
}
public function dispense(){
$this->gumballMachine->releaseBall();
if($this->gumballMachine->getCount() > 1){
$this->gumballMachine->setState($this->gumballMachine->getNoQuarterState());
}else{
print "oops, out of gumballs";
$this->gumballMachine->setState($this->gumballMachine->getSoldOutState());
}
}
}
class SoldOutState implements State{
public $gumballMachine;
public function __construct($gumballMachine){
$this->gumballMachine = $gumballMachine;
}
public function insertQuarter(){
print "wait, already gire gum";
}
public function ejectQuarter(){
print "sorry,you already turned the crank";
}
public function turnCrank(){
print "turning twice doesnt get you another gumball";
}
public function dispense(){
$this->gumballMachine->releaseBall();
if($this->gumballMachine->getCount() > 1){
$this->gumballMachine->setState($this->gumballMachine->getNoQuarterState());
}else{
print "oops, out of gumballs";
$this->gumballMachine->setState($this->gumballMachine->getSoldOutState());
}
}
}
class NoQuarterState implements State{
public $gumballMachine;
public function __construct($gumballMachine){
$this->gumballMachine = $gumballMachine;
}
public function insertQuarter(){
print("you insert a quarter");
$this->gumballMachine->setState($this->gumballMachine->getHasQuarterState());
}
function ejectQuarter(){
print("you havent inerted a quarter");
}
function turnCrank(){
print("you turned ,but theres no quarter");
}
function dispense(){
print("you need to pay first");
}
}
class HasQuarterState implements State{
public $gumballMachine;
function __construct($gumballMachine){
$this->gumballMachine = $gumballMachine;
}
public function insertQuarter(){
print "you can not insert another quarter";
}
public function ejectQuarter(){
print "quarter returned";
$this->gumballMachine->setState($this->gumballMachine->getNoQuarterState());
}
public function turnCrank(){
print "you return";
$winner = rand(1,10);
if($winner == 5 && $this->gumballMachine->getCount() > 1){
$this->gumballMachine->setState($this->gumballMachine->getWinnerState());
}else{
$this->gumballMachine->setState($this->gumballMachine->getSolidState());
}
}
public function dispense(){
print "no gumball dispensed";
}
}