forked from abishekaditya/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGumballMachine.cs
More file actions
103 lines (97 loc) · 3.26 KB
/
Copy pathGumballMachine.cs
File metadata and controls
103 lines (97 loc) · 3.26 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
using System;
namespace StatePattern.Legacy
{
public class GumballMachine
{
private int _count;
private State _state = State.NoQuarters;
public GumballMachine(int count)
{
_count = count;
}
public void InsertQuarter()
{
switch (_state)
{
case State.NoQuarters:
_state = State.HasQuarters;
Console.WriteLine("Inserted a quarter");
break;
case State.Sold:
Console.WriteLine("Please wait for current gumball to come out");
break;
case State.HasQuarters:
Console.WriteLine("Can't add more quarters");
break;
case State.NoGumballs:
Console.WriteLine("Out of Stock");
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public void EjectQuarter()
{
switch (_state)
{
case State.NoQuarters:
Console.WriteLine("Nothing to eject");
break;
case State.Sold:
Console.WriteLine("Sorry, you have already turned the crank");
break;
case State.HasQuarters:
Console.WriteLine("Ejecting..");
_state = State.NoQuarters;
break;
case State.NoGumballs:
Console.WriteLine("Can't eject, never accepted quarters");
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public void TurnCrank()
{
switch (_state)
{
case State.NoQuarters:
Console.WriteLine("Insert quarter First");
break;
case State.Sold:
Console.WriteLine("Turning twice won't get you a gumball");
break;
case State.HasQuarters:
Console.WriteLine("Getting gumball...");
_state = State.Sold;
Dispense();
break;
case State.NoGumballs:
Console.WriteLine("Out of Stock");
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private void Dispense()
{
switch (_state)
{
case State.NoQuarters:
Console.WriteLine("You need to pay first");
break;
case State.Sold:
Console.WriteLine("A Gumball comes rolling out");
_count--;
_state = _count == 0 ? _state = State.NoGumballs : State.NoQuarters;
break;
case State.HasQuarters:
case State.NoGumballs:
Console.WriteLine("Can't dispense");
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}