-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.cpp
More file actions
245 lines (232 loc) · 6.23 KB
/
state.cpp
File metadata and controls
245 lines (232 loc) · 6.23 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// State Design Pattern
#include <iostream>
#include <string>
#include <cmath>
#include <cassert>
#include <vector>
#include <utility>
/*
Sequence: insert coins, then select an item...then determine if enough cash, etc.*/
class VendingMachine;
class State {
protected:
VendingMachine* machine;
int selection;
public:
State(VendingMachine* machine_) : machine(machine_) {}
virtual ~State() {}
virtual void dispenseItem() = 0;
virtual void selectItem() = 0;
virtual void insertCoins() = 0;
virtual void returnMoney() = 0;
};
class Idle : public State {
public:
Idle(VendingMachine* machine_) : State(machine_) {}
void insertCoins() override;
void selectItem() override {
std::cout << "Insert cash first\n";
}
void dispenseItem() override {
std::cout << "Insert cash first\n";
}
void returnMoney() override {
std::cout << "Insert cash first\n";
}
};
class VendingMachine {
friend class State;
State* state_;
double balance_;
std::vector<std::pair<std::string, double>> items_;
public:
VendingMachine(std::vector<std::pair<std::string, double>> items) : state_(new Idle(this)), items_(items) { }
~VendingMachine() {
delete state_;
}
void dropItem(double idx) {
if(balance_ < items_[idx].second) {
throw "not enough balance";
} else {
balance_ -= items_[idx].second;
}
}
void addBalance() {
double coin;
std::cin >> coin;
coin = std::floor(coin * 100) / 100; // truncate to the penny
assert(coin > 0); // no way to take away money publicly
balance_ += coin;
}
void changeState(State* state) {
if(this->state_ != nullptr) {
delete this->state_;
}
this->state_ = state;
}
int checkItem(std::string val) {
for(int i=0; i<items_.size(); i++) {
if(val == items_[i].first) {
if(balance_ < items_[i].second) {
return -1;
} else {
return i;
}
}
}
return -1;
}
double getBalance() const {
return balance_;
}
std::string getItems() const {
std::string ret = "";
for(int i=0; i<items_.size(); i++) {
ret += items_[i].first + "\n";
}
return ret;
}
void emptyCash() {
if(balance_ < 0) {
std::cout << "no money\n";
}
std::cout << "Here's " << balance_ << " back\n";
balance_ = 0;
}
// Actions:
void dispenseItem() {
state_->dispenseItem();
}
void selectItem() {
state_->selectItem();
}
void insertCoins() {
state_->insertCoins();
}
void returnMoney() {
state_->returnMoney();
}
};
class ProcessMoney : public State {
public:
ProcessMoney(VendingMachine* machine_) : State(machine_) {}
void insertCoins() override { // the gate to open coins is open
machine->addBalance();
}
void selectItem() override;
void dispenseItem() override {
std::cout << "Choose an item";
}
void returnMoney() override {
machine->emptyCash();
machine->changeState(new Idle(machine));
}
};
class ItemSelection : public State {
public:
ItemSelection(VendingMachine* machine_) : State(machine_) {}
void insertCoins() override {
machine->addBalance();
machine->changeState(new ProcessMoney(machine));
}
void selectItem() override { // can always keep selecting items...
std::string item;
std::cout << "Choose from: " << machine->getItems();
std::cin >> item;
selection = machine->checkItem(item);
if(selection == -1) {
std::cout << "Not enough cash: insert coins or choose another\n";
return;
}
}
void dispenseItem() override;
void returnMoney() override {
machine->emptyCash();
machine->changeState(new Idle(machine));
}
};
class ItemDispensed : public State {
public:
ItemDispensed(VendingMachine* machine_) : State(machine_) {}
void insertCoins() override {
machine->addBalance();
machine->changeState(new ProcessMoney(machine));
}
void selectItem() override {
std::string item;
std::cout << "Choose from: " << machine->getItems() << "\n";
std::cin >> item;
selection = machine->checkItem(item);
if(selection == -1) {
std::cout << "Not enough cash: insert coins or choose another\n";
return;
}
machine->changeState(new ItemSelection(machine));
}
void dispenseItem() override {
std::cout << "Insert coins or select an item again\n";
}
void returnMoney() override {
machine->emptyCash();
machine->changeState(new Idle(machine));
}
};
void ItemSelection::dispenseItem() {
if(selection == -1) {
std::cout << "Please select a correct item\n";
return;
}
machine->dropItem(selection);
machine->changeState(new ItemDispensed(machine));
}
void ProcessMoney::selectItem() {
std::string item;
std::cout << "Choose from: " << machine->getItems() << "\n";
std::cin >> item;
selection = machine->checkItem(item);
if(selection == -1) {
std::cout << "Not enough cash: insert coins or choose another\n";
return;
}
machine->changeState(new ItemSelection(machine));
}
void Idle::insertCoins() {
machine->addBalance();
machine->changeState(new ProcessMoney(machine));
}
int main() {
std::vector<std::pair<std::string, double>> items;
std::string item;
double cost;
int num;
std::cout << "Enter items, starting with # of items\n";
std::cin >> num;
for(int i=0; i<num; i++) {
std::cin >> item >> cost;
items.push_back({item, cost});
}
std::cout << "vending machine ready to use\n";
// INITIALIZATION
VendingMachine chips(items); // initialize chips machine with items
std::string command = "";
while(true) {
std::cin >> command;
if(command == "coins") {
chips.insertCoins();
}
else if(command == "select") {
chips.selectItem();
}
else if(command == "dispense") {
chips.dispenseItem();
}
else if(command == "return") {
chips.returnMoney();
}
else if(command == "EXIT") {
break;
}
}
std::cout << "The vending machine is now closed\n";
return 0;
}