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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Drink
{
Drink(string n, double cs, int cn)
{
name = n;
cost = cs;
count = cn;
}
Drink()
{
name = " ";
cost = 0;
count = 0;
money = 0;
}
string name;
double cost;
double money;
int count;
int displayChoices();
double buyDrink(int c, Drink[]);
double inputMoney(double c);
int dailyReport(double m, int i);
};
int Drink::displayChoices()
{
int choice;
cout << "\n" << setw(7) << "Item" << setw(16) << "Cost\n";
cout << "1. Coca Cola 1.00\n";
cout << "2. Root Beer 1.00\n";
cout << "3. Orange Soda 1.00\n";
cout << "4. Grape Soda 1.00\n";
cout << "5. Bottled Water 1.50\n";
cout << "\nEnter your beverage choice: ";
cin >> choice;
while (choice < 1 || choice > 5)
{
cout << "Please select items 1-5";
cout << "\nRe-enter: ";
cin >> choice;
}
return choice;
}
double Drink::buyDrink(int choice, Drink data[])
{
string selection;
switch (choice)
{
case 1:
selection = "Coca Cola";
if (data[0].count == 0)
{
cout << "\nSOLD OUT\n";
return choice;
}
break;
case 2:
selection = "Root Beer";
if (data[1].count == 0)
{
cout << "\nSOLD OUT\n";
return choice;
}
break;
case 3:
selection = "Orange Soda";
if (data[2].count == 0)
{
cout << "\nSOLD OUT\n";
return choice;
}
break;
case 4:
selection = "Grape Soda";
if (data[3].count == 0)
{
cout << "\nSOLD OUT\n";
return choice;
}
break;
case 5:
selection = "Bottled Water";
if (data[4].count == 0)
{
cout << "\nSOLD OUT\n";
return choice;
}
break;
}
cout << "You selected " << selection << ".\n";
double change;
if (choice > 0 && choice < 5)
{
cost = 1.00;
change = inputMoney(cost);
return change;
}
if (choice == 5)
{
cost = 1.50;
change = inputMoney(cost);
return change;
}
}
int Drink::dailyReport(double money, int count)
{
if (money > 0)
{
cout << "*****Dispensing*****\n";
cout << "Here is your beverage! Enjoy!\n";
count--;
return count--;
}
else
{
cout << "Come back next time!\n";
return count;
}
}
double Drink::inputMoney(double cost)
{ double money = 0;
if (cost == 1.00)
{
cout << "Please enter $" << fixed << setprecision(2) << cost << "\n";
cin >> money;
while (money < cost && money !=0)
{
cout << "Please enter the correct change.\n";
cin >> money;
}
if (money > cost && money !=0)
{
money = money - cost;
cout << "Your change is $" << money << "\n";
}
else if (money == 0 && money < cost)
{
money = 0;
cout << "Purchase canceled.\n";
}
return cost;
}
else if (cost == 1.50)
{
cout << "Please enter $" << fixed << setprecision(2) << cost << "\n";
cin >> money;
while (money < cost && money !=0)
{
cout << "Please enter the correct change.\n";
cin >> money;
}
if (money > cost && money !=0)
{
money = money - cost;
cout << "Your change is $" << money << "\n";
}
else if (money == 0 && money < cost)
{
money = 0;
cout << "Purchase canceled.\n";
}
return cost;
}
}
int main()
{
Drink vending;
int choice;
int continuing;
double cost;
double depositcola = 0;
double depositroot = 0;
double depositorange = 0;
double depositgrape = 0;
double depositwater = 0;
const int DRINKS = 5;
Drink data[DRINKS] = {Drink("Coca Cola", 1.00, 20),
Drink("Root Beer", 1.00, 20),
Drink("Orange Soda", 1.00, 20),
Drink("Grape Soda", 1.00, 20),
Drink("Bottled Water", 1.50, 20)};
cout << "Welcome to the Drink Machine!\n";
cout << "Press 1 to enter, 0 to stop.";
cin >> continuing;
while (continuing != 0)
{
choice = vending.displayChoices();
cost = vending.buyDrink(choice, data);
switch (choice)
{
case 1:
if (data[0].count >= 1)
{data[0].count = vending.dailyReport(data[0].cost, data[0].count);
depositcola+=1.0;
cout <<"\n";
cout << data[0].name << " Status:" <<endl;
cout << "$" << depositcola << " deposited, ";
cout << data[0].count <<" remain for purchase." << endl;}
break;
case 2:
if (data[1].count > 0)
{data[1].count = vending.dailyReport(data[1].cost, data[1].count);
depositroot+=1.0;
cout <<"\n";
cout << data[1].name << " Status:" <<endl;
cout << "$" << depositroot << " deposited, ";
cout << data[1].count <<" remain for purchase." << endl;}
break;
case 3:
if (data[2].count > 0)
data[2].count = vending.dailyReport(data[2].cost, data[2].count);
depositorange+=1.0;
cout <<"\n";
cout << data[2].name << " Status:" <<endl;
cout << "$" << depositorange << " deposited, ";
cout << data[2].count <<" remain for purchase." << endl;
break;
case 4:
if (data[3].count > 0)
data[3].count = vending.dailyReport(data[3].cost, data[3].count);
depositgrape+=1.0;
cout <<"\n";
cout << data[3].name << " Status:" <<endl;
cout << "$" << depositgrape << " deposited, ";
cout << data[3].count <<" remain for purchase." << endl;
break;
case 5:
if (data[4].count > 0)
data[4].count = vending.dailyReport(data[4].cost, data[4].count);
depositwater+=1.5;
cout <<"\n";
cout << data[4].name << " Status:" <<endl;
cout << "$" << depositwater << " deposited, ";
cout << data[4].count <<" remain for purchase." << endl;
break;
}
cout << "\nIf you would like to make another purchase enter 1 to continue or 0 to stop.\n";
cin >> continuing;
}
return 0;
}
|