-
Notifications
You must be signed in to change notification settings - Fork 2
/
MachineStates.cpp
73 lines (55 loc) · 1.87 KB
/
MachineStates.cpp
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
// MachineStates.cpp
#include "MachineStates.h"
AbstractState::~AbstractState() {}
void AbstractState::setState(Machine &machine, AbstractState *state) {
AbstractState *aux = machine.state;
machine.state = state;
delete aux;
}
void AbstractState::updateStock(Machine &machine, unsigned int quantity) {
machine.stock = quantity;
}
void AbstractState::damage(Machine &machine) {
setState(machine, new Broken());
};
void AbstractState::fix(Machine &machine) {
setState(machine, machine.stock > 0
? static_cast<AbstractState *>(new Normal())
: static_cast<AbstractState *>(new SoldOut()));
};
Normal::~Normal() {}
void Normal::sell(Machine &machine, unsigned int quantity) {
unsigned int currStock = machine.getStock();
if (currStock < quantity) {
throw std::runtime_error("Not enough stock");
}
updateStock(machine, currStock - quantity);
if (machine.getStock() == 0) {
setState(machine, new SoldOut());
}
}
void Normal::refill(Machine &machine, unsigned int quantity) {
int currStock = machine.getStock();
updateStock(machine, currStock + quantity);
}
void Normal::fix(Machine &machine) {
throw std::runtime_error("If it ain't broke, don't fix it!");
};
SoldOut::~SoldOut() {}
void SoldOut::sell(Machine &machine, unsigned int quantity) {
throw std::runtime_error("Sold out!");
}
void SoldOut::refill(Machine &machine, unsigned int quantity) {
updateStock(machine, quantity);
setState(machine, new Normal());
}
void SoldOut::fix(Machine &machine) {
throw std::runtime_error("If it ain't broke, don't fix it!");
};
Broken::~Broken() {}
void Broken::sell(Machine &machine, unsigned int quantity) {
throw std::runtime_error("Machine is broken! Fix it before sell");
}
void Broken::refill(Machine &machine, unsigned int quantity) {
throw std::runtime_error("Machine is broken! Fix it before refill");
}