-
Notifications
You must be signed in to change notification settings - Fork 617
/
GumballMachine.cs
54 lines (46 loc) · 1.24 KB
/
GumballMachine.cs
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
using System;
namespace StatePattern
{
public class GumballMachine
{
public int Count { get; private set; }
public IState SoldOutState;
public IState NoQuarterState;
public IState HasQuarterState;
public IState SoldState;
public IState WinnerState;
public IState State { get; set; }
public GumballMachine(int count)
{
Count = count;
SoldOutState = new SoldOutState(this);
NoQuarterState = new NoQuarterState(this);
HasQuarterState = new HasQuarterState(this);
SoldState = new SoldState(this);
WinnerState = new WinnerState(this);
if (Count > 0)
{
State = NoQuarterState;
}
}
public void InsertQuarter()
{
State.InsertQuarter();
}
public void EjectQuarter()
{
State.EjectQuarter();
}
public void TurnCrank()
{
State.TurnCrank();
State.Dispense();
}
public void ReleaseBall()
{
Console.WriteLine("A ball comes rolling down");
if (Count == 0) return;
Count--;
}
}
}