-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGumballMachine.cpp
89 lines (80 loc) · 2.04 KB
/
GumballMachine.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<iostream>
#include "GumballMachine.hpp"
#include "NoCoinState.hpp"
#include "HasCoinState.hpp"
#include "SoldOutState.hpp"
#include "SoldState.hpp"
#include "WinnerState.hpp"
using namespace std;
GumballMachine::GumballMachine(int n) {
this->count = n;
this->noCoinState = new NoCoinState(this);
this->soldOutState = new SoldOutState(this);
this->hasCoinState = new HasCoinState(this);
this->soldState = new SoldState(this);
this->winnerState = new WinnerState(this);
this->curState = soldOutState;
if(n > 0) {
this->curState = noCoinState;
}
}
void GumballMachine::insertCoin() {
curState->insertCoin();
}
void GumballMachine::ejectCoin() {
curState->ejectCoin();
}
void GumballMachine::turnCrank() {
curState->turnCrank();
curState->dispense();
}
void GumballMachine::releaseBall() {
cout << "A gumball is rolling out of the slot: ";
if(count != 0) {
count = count - 1;
cout << "Thank you for the purchase!" << endl;
return;
}
cout << "ERROR: Something wrong happened" << endl;
}
// getters
State* GumballMachine::getNoCoinState() {
return noCoinState;
}
State* GumballMachine::getHasCoinState() {
return hasCoinState;
}
State* GumballMachine::getSoldOutState() {
return soldOutState;
}
State* GumballMachine::getSoldState() {
return soldState;
}
State* GumballMachine::getWinnerState() {
return winnerState;
}
int GumballMachine::getCount() {
return count;
}
// setters
void GumballMachine::setNoCoinState(State* state) {
this->noCoinState = state;
}
void GumballMachine::setHasCoinState(State* state) {
this->hasCoinState = state;
}
void GumballMachine::setSoldOutState(State* state) {
this->soldOutState = state;
}
void GumballMachine::setSoldState(State* state) {
this->soldState = state;
}
void GumballMachine::setWinnerState(State* state) {
this->winnerState = state;
}
void GumballMachine::setState(State* state) {
this->curState = state;
}
void GumballMachine::refill(int n) {
this->count = this->count + n;
}