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
|
#include <iostream>
#include <cstdlib>
#include <list>
#include <iomanip>
using namespace std;
struct Drink
{
Drink(string _name, int _num, double _cost)
: drinkName(_name), numberDrinks(_num), drinkCost(_cost)
{ }
string drinkName;
int numberDrinks;
double drinkCost;
};
typedef std::list<Drink*> Drink_t;
typedef std::list<Drink*>::iterator DrinkItr_t;
typedef std::list<Drink*>::const_iterator DrinkConstItr_t;
void print(const Drink_t& _drink_list)
{
cout << setw(12) << "Drink Name"
<< setw(15) << "Cost"
<< setw(22) << "Amount left" << endl;
cout << endl;
for (DrinkConstItr_t itr = _drink_list.begin(); itr != _drink_list.end(); ++itr)
{
cout << setw(12) << (*itr)->drinkName
<< setw(15) << (*itr)->drinkCost
<< setw(22) << (*itr)->numberDrinks << endl;
}
}
int main()
{
// Create the list of drinks
// Use pointers so later, all_drinks will have same instance of each drink
// i.e. modify drink in "drinks" will also modify drink in
// "all_drinks"
Drink_t available_drinks;
available_drinks.push_back(new Drink("Dr. Pepper", 10, 0.5));
available_drinks.push_back(new Drink("Pepsi", 15, 0.5));
available_drinks.push_back(new Drink("Surge", 8, 0.25));
available_drinks.push_back(new Drink("Coca-Cola", 12, 0.5));
// copy of drinks
Drink_t all_drinks = available_drinks;
double machineCash = 0.;
// set the random seed
srand(time(NULL));
do
{
// just a separator to make output clear
cout.fill('=');
cout << setw(50) << "" << endl;
cout.fill(' ');
int m = rand() % available_drinks.size();
DrinkItr_t drink = available_drinks.begin();
// STL list doesn't have random access so must advance iterator
std::advance(drink, m);
double money = (double)(rand() % 20 + 80) / 100.00;
double change = (money - (*drink)->drinkCost);
// print list of all drinks
cout << "\nList of all drinks:\n" << endl;
print(all_drinks);
// print list of available
cout << "\nList of available drinks:\n" << endl;
print(available_drinks);
// print selection info
cout << "\nThe drink you chose was: " << (*drink)->drinkName << endl;
cout << "The price is : $" << fixed << setprecision(2) << (*drink)->drinkCost << endl;
cout << "You pay: $" << fixed << setprecision(2) << money << endl;
cout << "Your change is : $" << fixed << setprecision(2) << change << endl;
// decrease number of drinks
(*drink)->numberDrinks--;
// if out of drinks remove from available drinks
if((*drink)->numberDrinks == 0)
available_drinks.erase(drink);
// add machine cash
machineCash += (*drink)->drinkCost;
// since we are removing drinks if they are empty, we just do the do/while
// loop until available_drinks no longer has any drinks in it
} while (available_drinks.size() > 0);
cout << endl;
cout << fixed << setprecision(2) << "total cash: " << "$" << machineCash << endl;
}
| |