-
Notifications
You must be signed in to change notification settings - Fork 0
/
CandyMachine.cpp
142 lines (136 loc) · 2.73 KB
/
CandyMachine.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
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
class cashRegister
{
private:
int cashOnHand;
public:
cashRegister();
cashRegister(int cashIn);
int getCurrentBalance();
void acceptAmount(int amountIn);
};
class dispenserType
{
private:
int numberOfItems;
int cost;
public:
dispenserType();
dispenserType(int setNoOfItems, int setCost);
int getNoOfItems();
int getCost();
void makeSale();
};
cashRegister::cashRegister()
{
cashOnHand = 500;
}
cashRegister::cashRegister(int cashIn)
{
cashOnHand = cashIn;
}
int cashRegister::getCurrentBalance()
{
return cashOnHand;
}
void cashRegister::acceptAmount(int amountIn)
{
cashOnHand = cashOnHand + amountIn;
}
dispenserType::dispenserType()
{
numberOfItems = 50;
cost = 50;
}
dispenserType::dispenserType(int setNoOfItems, int setCost)
{
numberOfItems = setNoOfItems;
cost = setCost;
}
int dispenserType::getNoOfItems()
{
return numberOfItems;
}
int dispenserType::getCost()
{
return cost;
}
void dispenserType::makeSale()
{
numberOfItems--;
}
#include <iostream.h>
#include<conio.h>
void showSelection();
void sellProduct(dispenserType& product, cashRegister& pCounter);
int main()
{ clrscr();
cashRegister counter;
dispenserType candy(100, 50);
dispenserType chips(100, 65);
dispenserType gum(75, 45);
dispenserType cookies(50, 85);
int choice;
showSelection();
cout<<"Enter choice:";
cin >> choice;
while (choice != 5)
{
switch (choice)
{
case 1:
sellProduct(candy, counter);
break;
case 2:
sellProduct(chips, counter);
break;
case 3:
sellProduct(gum, counter);
break;
case 4:
sellProduct(cookies, counter);
break;
default:
cout << "Invalid selection." << endl;
}
showSelection();
cin >> choice;
}
return 0;
}
void showSelection()
{
cout << "*** Welcome to Candy Shop ***" << endl;
cout << "To select an item, enter " << endl;
cout << "1 for Candy" << endl;
cout << "2 for Chips" << endl;
cout << "3 for Gum" << endl;
cout << "4 for Cookies" << endl;
cout << "5 to exit" << endl;
}
void sellProduct(dispenserType& product, cashRegister& pCounter)
{
int amount;
int amount2;
if (product.getNoOfItems() > 0)
{
cout << "Please deposit " << product.getCost() << " cents:" << endl;
cin >> amount;
if (amount < product.getCost())
{
cout << "Please deposit another " << product.getCost()- amount << " cents:" << endl;
cin >> amount2;
amount = amount + amount2;
}
if (amount >= product.getCost())
{
pCounter.acceptAmount(amount);
product.makeSale();
cout << "Collect your item at the bottom and " << "enjoy." << endl;
}
else
cout << "The amount is not enough. " << "Collect what you deposited." << endl;
cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl << endl;
}
else
cout << "Sorry, this item is sold out." << endl;
}