chocolate vending machine code

I have been asked to create a chocolate vending machine. So far i have produced a large amount of code, however i am confused as to what is stopping the program from working as there are a large amount of errors being produced.
This is my code so far -
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

num state { Out_Of_Chocolate, No_Credit, Has_Credit, Dispenses_Chocolate, Maintenance_Mode };
#include <vector>

using namespace std;

class StateContext;

class State
{
protected:
	StateContext* CurrentContext;
public:
	State(StateContext* Context) { CurrentContext = Context; }
	virtual ~State(void) {}
};

class StateContext
{
protected:
	State* CurrentState = nullptr;
	int stateIndex = 0;
	vector<State*> availableStates;
public:
	virtual ~StateContext(void);
	virtual void setState(state newState);
	virtual int getStateIndex(void);
};

StateContext::~StateContext(void)
{
	for (int i = 0; i < this->availableStates.size(); i++) delete this->availableStates[i];
	this->availableStates.clear();
}

void StateContext::setState(state newState)
{
	this->CurrentState = availableStates[newState];
	this->stateIndex = newState;
}


int StateContext::getStateIndex(void)
{
	return this->stateIndex;
}

enum state { Out_Of_Chocolate, No_Credit, Has_Credit, Dispenses_Chocolate };

class Transition
{
public:
	virtual bool insertMoney(int) { cout << "Error!" << endl; return false; }
	virtual bool makeSelection(int) { cout << "Error!" << endl; return false; }
	virtual bool moneyRejected(void) { cout << "Error!" << endl; return false; }
	virtual bool addChocolate(int) { cout << "Error!" << endl; return false; }
	virtual bool dispence(void) { cout << "Error!" << endl; return false; }
	//virtual bool enterPin(int pin) { cout << "Error!" endl; return false; }
	//virtual bool exit(void) { cout << "Error!" endl; return false; }
};

class ChocoState : public State, public Transition
{
public:
	ChocoState(StateContext* Context) : State(Context) {}
};

class OutOfChocolate : public ChocoState
{
public:
	OutOfChocolate(StateContext* Context) : ChocoState(Context) {}
	bool addChocolate(int number);
};

class NoCredit : public ChocoState
{
public:
	NoCredit(StateContext* Context) : ChocoState(Context) {}
	bool insertMoney(int credit);
};

class HasCredit : public ChocoState
{
public:
	HasCredit(StateContext* Context) : ChocoState(Context) {}
	bool insertMoney(int credit);
	bool makeSelection(int option);
	bool moneyRejected(void);
};

class DispensesChocolate : public ChocoState
{
public:
	DispensesChocolate(StateContext* Context) : ChocoState(Context) {}
	bool dispense(void);
};

bool OutOfChocolate::addChocolate(int number)
{
	((Chocolate_Dispenser*)CurrentContext)->inventory += number;
	cout << "Adding chocolate... Inventory = " << ((Chocolate_Dispenser*)CurrentContext)->inventory << endl;
	CurrentContext->setState(No_Credit);
	return true;
}
bool NoCredit::insertMoney(int credit)
{
	((Chocolate_Dispenser*)CurrentContext)->credit += credit;
	cout << "Adding credit... credit = " << ((Chocolate_Dispenser*)CurrentContext)->credit << endl;
	CurrentContext->setState(Has_Credit);
	return true;
}

bool HasCredit::insertMoney(int credit)
{
	((Chocolate_Dispenser*)CurrentContext)->credit += credit;
	cout << "Adding credit... Credit = " << ((Chocolate_Dispenser*)CurrentContext)->credit << endl;
	return true;
}

bool HasCredit::makeSelection(int option)
{
	//the option will be equal to the number of bars that the customer has selected 
	cout << "You have selected " << option << " bar(s) of chocolate" << endl;

	if (((Chocolate_Dispenser*)CurrentContext) ->inventory < option)
	{
		cout < "Error: you have selected more chocolate than the machine contains" << endl;
		return true;
	}

	if (((Chocolate_Dispenser*)CurrentContext)->credit < option)
	{
		cout << "Error: you don't have enough credit for that selection" << endl;
		return true;
	}
	cout << "Credit and inventory is sufficient for your selection" << endl;

	((Chocolate_Dispenser*)CurrentContext)->inventory -= option; //deduct inventory
	((Chocolate_Dispenser*)CurrentContext)->credit -= option; //deduct credit 

	CurrentContext->setState(Dispenses_Chocolate);
	return true;
}

bool HasCredit::moneyRejected(void)
{
	cout << "Rejectng money...." << endl;
	((Chocolate_Dispenser*)CurrentContext)->credit = 0;
	CurrentContext->setState(No_Credit);
	return true;
}

bool DispensesChocolate::dispense(void)
{
	cout << "Dispensing..." << endl;
	cout << "Inventory = " << ((Chocolate_Dispenser*)CurrentContext)->inventory << endl;
	cout << "Credit = " << ((Chocolate_Dispenser*)CurrentContext)->credit << endl;

	if (((Chocolate_Dispenser*)CurrentContext)->inventory == 0) CurrentContext->setState(Out_Of_Chocolate);
	else if (((Chocolate_Dispenser*)CurrentContext)->credit == 0) CurrentContext->setState(No_Credit);
	else CurrentContext->setState(Has_Credit);
	return true;
}

class Chocolate_Dispenser : public StateContext, public Transition
{
	friend class OutOfChocolate;
	friend class NoCredit;
	friend class HasCredit;
	friend class DispensesChocolate;
private:
	int inventory = 0; //number of chocolate 
	int credit = 0; //measure of the number of bars that can be purchased and not money
public:
	Chocolate_Dispenser(void);
	bool insertMoney(int credit);
	bool makeSelection(int option);
	bool moneyRejected(void);
	bool addChocolate(int number);
	bool dispense(void);
};

Chocolate_Dispenser::Chocolate_Dispenser(void)
{
	this->availableStates.push_back(new OutOfChocolate(this));
	this->availableStates.push_back(new NoCredit(this));
	this->availableStates.push_back(new HasCredit(this));
	this->availableStates.push_back(new DispensesChocolate(this));

	this->setState(Out_Of_Chocolate);
}


bool Chocolate_Dispenser::insertMoney(int credit)
{
	return ((ChocoState*)CurrentState)->insertMoney(credit);
}
bool Chocolate_Dispenser::makeSelection(int option)
{
	return ((ChocoState*)CurrentState)->makeSelection(option);
}
bool Chocolate_Dispenser::moneyRejected(void)
{
	return ((ChocoState*)CurrentState)->moneyRejected();
}
bool Chocolate_Dispenser::addChocolate(int number)
{
	return ((ChocoState*)CurrentState)->addChocolate(number);
}
bool Chocolate_Dispenser::dispense(void)
{
	return ((ChocoState*)currentState)->dispense();
}
bool Chocolate_Dispenser::enterPin(int pin)
{
	return ((chocoState*)CurrentState)->enterPin(pin);
}
bool Chocolate_Dispenser::exit(void)
{
	return ((ChocoState*)CurrentState)->exit();
}

int main(void)
{
	Chocolate_Dispenser MyDispenser;

	MyDispenser.addChocolate(10);
	MyDispenser.makeSelection(2);
	MyDispenser.insertMoney(10);
	MyDispenser.makeSelection(20);
	MyDispenser.makeSelection(10);
	MyDispenser.insertMoney(10);
	MyDispenser.dispence();

	return 0;
}
Last edited on
Some obvious errors.

Line 48: This duplicates line 2.

Line 52-56: cout is undefined because you did not include the <iostream> header.

Line 99,106,114,125,131,138,139,148,159,160: Chocolate_Dispenser is not defined until line 164.

Line 126: cout must be followed by <<, not <.







lines 212 and 216 are now the only lines giving me errors, the errors are saying that 'current state' is undefined, however the other lines including current state are working so i am confused on what i have done wrong.
Also, thank you for your help :)
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
enum state { Out_Of_Chocolate, No_Credit, Has_Credit, Dispenses_Chocolate, Maintenance_Mode };
#include <vector>
#include <iostream>

using namespace std;

class StateContext;

class State
{
protected:
	StateContext* CurrentContext;
public:
	State(StateContext* Context) { CurrentContext = Context; }
	virtual ~State(void) {}
};

class StateContext
{
protected:
	State* CurrentState = nullptr;
	int stateIndex = 0;
	vector<State*> availableStates;
public:
	virtual ~StateContext(void);
	virtual void setState(state newState);
	virtual int getStateIndex(void);
};

StateContext::~StateContext(void)
{
	for (int i = 0; i < this->availableStates.size(); i++) delete this->availableStates[i];
	this->availableStates.clear();
}

void StateContext::setState(state newState)
{
	this->CurrentState = availableStates[newState];
	this->stateIndex = newState;
}


int StateContext::getStateIndex(void)
{
	return this->stateIndex;
}

class Transition
{
public:
	virtual bool insertMoney(int) { cout << "Error!" << endl; return false; }
	virtual bool makeSelection(int) { cout << "Error!" << endl; return false; }
	virtual bool moneyRejected(void) { cout << "Error!" << endl; return false; }
	virtual bool addChocolate(int) { cout << "Error!" << endl; return false; }
	virtual bool dispense(void) { cout << "Error!" << endl; return false; }
	virtual bool enterPin(int pin) { cout << "Error!" << endl; return false; }
	virtual bool exit(void) { cout << "Error!" << endl; return false; }
};

class ChocoState : public State, public Transition
{
public:
	ChocoState(StateContext* Context) : State(Context) {}
};

class OutOfChocolate : public ChocoState
{
public:
	OutOfChocolate(StateContext* Context) : ChocoState(Context) {}
	bool addChocolate(int number);
};

class NoCredit : public ChocoState
{
public:
	NoCredit(StateContext* Context) : ChocoState(Context) {}
	bool insertMoney(int credit);
};

class HasCredit : public ChocoState
{
public:
	HasCredit(StateContext* Context) : ChocoState(Context) {}
	bool insertMoney(int credit);
	bool makeSelection(int option);
	bool moneyRejected(void);
};

class DispensesChocolate : public ChocoState
{
public:
	DispensesChocolate(StateContext* Context) : ChocoState(Context) {}
	bool dispense(void);
};

bool OutOfChocolate::addChocolate(int number)
{
	((Chocolate_Dispenser*)CurrentContext)->inventory += number;
	cout << "Adding chocolate... Inventory = " << ((Chocolate_Dispenser*)CurrentContext)->inventory << endl;
	CurrentContext->setState(No_Credit);
	return true;
}
bool NoCredit::insertMoney(int credit)
{
	((Chocolate_Dispenser*)CurrentContext)->credit += credit;
	cout << "Adding credit... credit = " << ((Chocolate_Dispenser*)CurrentContext)->credit << endl;
	CurrentContext->setState(Has_Credit);
	return true;
}

bool HasCredit::insertMoney(int credit)
{
	((Chocolate_Dispenser*)CurrentContext)->credit += credit;
	cout << "Adding credit... Credit = " << ((Chocolate_Dispenser*)CurrentContext)->credit << endl;
	return true;
}

bool HasCredit::makeSelection(int option)
{
	//the option will be equal to the number of bars that the customer has selected 
	cout << "You have selected " << option << " bar(s) of chocolate" << endl;

	if (((Chocolate_Dispenser*)CurrentContext) ->inventory < option)
	{
		cout << "Error: you have selected more chocolate than the machine contains" << endl;
		return true;
	}

	if (((Chocolate_Dispenser*)CurrentContext)->credit < option)
	{
		cout << "Error: you don't have enough credit for that selection" << endl;
		return true;
	}
	cout << "Credit and inventory is sufficient for your selection" << endl;

	((Chocolate_Dispenser*)CurrentContext)->inventory -= option; //deduct inventory
	((Chocolate_Dispenser*)CurrentContext)->credit -= option; //deduct credit 

	CurrentContext->setState(Dispenses_Chocolate);
	return true;
}

bool HasCredit::moneyRejected(void)
{
	cout << "Rejectng money...." << endl;
	((Chocolate_Dispenser*)CurrentContext)->credit = 0;
	CurrentContext->setState(No_Credit);
	return true;
}

bool DispensesChocolate::dispense(void)
{
	cout << "Dispensing..." << endl;
	cout << "Inventory = " << ((Chocolate_Dispenser*)CurrentContext)->inventory << endl;
	cout << "Credit = " << ((Chocolate_Dispenser*)CurrentContext)->credit << endl;

	if (((Chocolate_Dispenser*)CurrentContext)->inventory == 0) CurrentContext->setState(Out_Of_Chocolate);
	else if (((Chocolate_Dispenser*)CurrentContext)->credit == 0) CurrentContext->setState(No_Credit);
	else CurrentContext->setState(Has_Credit);
	return true;
}

class Chocolate_Dispenser : public StateContext, public Transition
{
	friend class OutOfChocolate;
	friend class NoCredit;
	friend class HasCredit;
	friend class DispensesChocolate;
private:
	int inventory = 0; //number of chocolate 
	int credit = 0; //measure of the number of bars that can be purchased and not money
public:
	Chocolate_Dispenser(void);
	bool insertMoney(int credit);
	bool makeSelection(int option);
	bool moneyRejected(void);
	bool enterPin(int pin);
	bool exit(void);
	bool addChocolate(int number);
	bool dispense(void);
};

Chocolate_Dispenser::Chocolate_Dispenser(void)
{
	this->availableStates.push_back(new OutOfChocolate(this));
	this->availableStates.push_back(new NoCredit(this));
	this->availableStates.push_back(new HasCredit(this));
	this->availableStates.push_back(new DispensesChocolate(this));

	this->setState(Out_Of_Chocolate);
}


bool Chocolate_Dispenser::insertMoney(int credit)
{
	return ((ChocoState*)CurrentState)->insertMoney(credit);
}
bool Chocolate_Dispenser::makeSelection(int option)
{
	return ((ChocoState*)CurrentState)->makeSelection(option);
}
bool Chocolate_Dispenser::moneyRejected(void)
{
	return ((ChocoState*)CurrentState)->moneyRejected();
}
bool Chocolate_Dispenser::addChocolate(int number)
{
	return ((ChocoState*)CurrentState)->addChocolate(number);
}
bool Chocolate_Dispenser::dispense(void)
{
	return ((ChocoState*)currentState)->dispense();
}
bool Chocolate_Dispenser::enterPin(int pin)
{
	return ((chocoState*)CurrentState)->enterPin(pin);
}
bool Chocolate_Dispenser::exit(void)
{
	return ((ChocoState*)CurrentState)->exit();
}

int main(void)
{
	Chocolate_Dispenser MyDispenser;

	MyDispenser.addChocolate(10);
	MyDispenser.makeSelection(2);
	MyDispenser.insertMoney(10);
	MyDispenser.makeSelection(20);
	MyDispenser.makeSelection(10);
	MyDispenser.dispense();
	MyDispenser.insertMoney(10);
	MyDispenser.enterPin(4);
	MyDispenser.exit(); 


	return 0;
}
lines 212 and 216 are now the only lines giving me errors, the errors are saying that 'current state' is undefined, however the other lines including current state are working so i am confused on what i have done wrong.

In destructor 'virtual StateContext::~StateContext()': 32:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] At global scope: 56:28: warning: unused parameter 'pin' [-Wunused-parameter] In member function 'virtual bool OutOfChocolate::addChocolate(int)': 98:4: error: 'Chocolate_Dispenser' was not declared in this scope 98:24: error: expected primary-expression before ')' token 98:25: error: expected ')' before 'CurrentContext' 99:70: error: expected primary-expression before ')' token 99:71: error: expected ')' before 'CurrentContext' At global scope: 96:39: warning: unused parameter 'number' [-Wunused-parameter] In member function 'virtual bool NoCredit::insertMoney(int)': 105:4: error: 'Chocolate_Dispenser' was not declared in this scope 105:24: error: expected primary-expression before ')' token 105:25: error: expected ')' before 'CurrentContext' 106:64: error: expected primary-expression before ')' token 106:65: error: expected ')' before 'CurrentContext' At global scope: 103:32: warning: unused parameter 'credit' [-Wunused-parameter] In member function 'virtual bool HasCredit::insertMoney(int)': 113:4: error: 'Chocolate_Dispenser' was not declared in this scope 113:24: error: expected primary-expression before ')' token 113:25: error: expected ')' before 'CurrentContext' 114:64: error: expected primary-expression before ')' token 114:65: error: expected ')' before 'CurrentContext' At global scope: 111:33: warning: unused parameter 'credit' [-Wunused-parameter] In member function 'virtual bool HasCredit::makeSelection(int)': 123:8: error: 'Chocolate_Dispenser' was not declared in this scope 123:28: error: expected primary-expression before ')' token 123:29: error: expected ')' before 'CurrentContext' 129:2: error: expected ')' before 'if' 134:73: warning: suggest braces around empty body in an 'if' statement [-Wempty-body] 136:4: error: 'Chocolate_Dispenser' was not declared in this scope 136:24: error: expected primary-expression before ')' token 136:25: error: expected ')' before 'CurrentContext' 137:24: error: expected primary-expression before ')' token 137:25: error: expected ')' before 'CurrentContext' In member function 'virtual bool HasCredit::moneyRejected()': 146:4: error: 'Chocolate_Dispenser' was not declared in this scope 146:24: error: expected primary-expression before ')' token 146:25: error: expected ')' before 'CurrentContext' In member function 'virtual bool DispensesChocolate::dispense()': 154:30: error: 'Chocolate_Dispenser' was not declared in this scope 154:50: error: expected primary-expression before ')' token 154:51: error: expected ')' before 'CurrentContext' 155:47: error: expected primary-expression before ')' token 155:48: error: expected ')' before 'CurrentContext' 157:28: error: expected primary-expression before ')' token 157:29: error: expected ')' before 'CurrentContext' 157:104: error: expected ')' before ';' token 158:33: error: expected primary-expression before ')' token 158:34: error: expected ')' before 'CurrentContext' 158:99: error: expected ')' before ';' token In member function 'virtual bool Chocolate_Dispenser::dispense()': 212:23: error: 'currentState' was not declared in this scope In member function 'virtual bool Chocolate_Dispenser::enterPin(int)': 216:11: error: 'chocoState' was not declared in this scope 216:22: error: expected primary-expression before ')' token 216:23: error: expected ')' before 'CurrentState' At global scope: 214:40: warning: unused parameter 'pin' [-Wunused-parameter] In member function 'virtual bool Chocolate_Dispenser::dispense()': 213:1: warning: control reaches end of non-void function [-Wreturn-type] In member function 'virtual bool Chocolate_Dispenser::enterPin(int)': 217:1: warning: control reaches end of non-void function [-Wreturn-type]
Topic archived. No new replies allowed.