-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
VendingMachine.cs
98 lines (83 loc) · 2.6 KB
/
VendingMachine.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
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
using System;
namespace VendingMachineApp
{
public class VendingMachine
{
private static VendingMachine _instance;
public Inventory Inventory { get; private set; }
private IVendingMachineState _idleState;
private IVendingMachineState _readyState;
private IVendingMachineState _dispenseState;
private IVendingMachineState _returnChangeState;
private IVendingMachineState _currentState;
public Product SelectedProduct { get; private set; }
public double TotalPayment { get; private set; }
private VendingMachine()
{
Inventory = new Inventory();
_idleState = new IdleState(this);
_readyState = new ReadyState(this);
_dispenseState = new DispenseState(this);
_returnChangeState = new ReturnChangeState(this);
_currentState = _idleState;
SelectedProduct = null;
TotalPayment = 0.0;
}
public static VendingMachine GetInstance()
{
if (_instance == null)
{
_instance = new VendingMachine();
}
return _instance;
}
public void SelectProduct(Product product)
{
_currentState.SelectProduct(product);
}
public void InsertCoin(Coin coin)
{
_currentState.InsertCoin(coin);
}
public void InsertNote(Note note)
{
_currentState.InsertNote(note);
}
public void DispenseProduct()
{
_currentState.DispenseProduct();
}
public void ReturnChange()
{
_currentState.ReturnChange();
}
public void SetState(IVendingMachineState state)
{
_currentState = state;
}
public IVendingMachineState GetIdleState() => _idleState;
public IVendingMachineState GetReadyState() => _readyState;
public IVendingMachineState GetDispenseState() => _dispenseState;
public IVendingMachineState GetReturnChangeState() => _returnChangeState;
public void SetSelectedProduct(Product product)
{
SelectedProduct = product;
}
public void ResetSelectedProduct()
{
SelectedProduct = null;
}
public void AddCoin(Coin coin)
{
TotalPayment += coin.GetValue();
}
public void AddNote(Note note)
{
TotalPayment += note.GetValue();
}
public void ResetPayment()
{
TotalPayment = 0.0;
}
}
}