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
|
#include <algorithm>
#include <exception>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
struct Beverage {
std::string name;
int price,
quantity,
sold;
Beverage();
Beverage(std::string name_arg, int price_arg,
int quantity_arg, int sold_arg = 0);
};
Beverage::Beverage() : name { "unknown" }, price {}, quantity {}, sold {} {}
Beverage::Beverage(std::string name_arg, int price_arg, int quantity_arg, int sold_arg)
: name { name_arg },
price { price_arg },
quantity { quantity_arg },
sold { sold_arg }
{}
const std::vector<int> COINS { 5, 10, 25, 100 };
//Function Prototypes
unsigned getBeverage(std::vector<Beverage>& drinks);
void clearCin();
int askPayment(const Beverage& b, int credit = 0);
int moneyHandler();
int askForInt(int min, int max);
double askForDouble(double min, double max);
void updateBeverageQty(Beverage& b);
void updateSoldBeverageQty(Beverage& b);
int giveBackMoney(int howmuch);
std::string displayInCent(int val);
int getChoice();
void computeTotalProfit(std::vector<double>& prof, const std::vector<Beverage>& bev);
void displayTotalProfit(const std::vector<double>& prof, const std::vector<Beverage>& bev);
unsigned getBestPerformer(const std::vector<double>& prof);
unsigned getWorstPerformer(const std::vector<double>& prof);
void displayTabHeader();
void displayDrink(const Beverage& bev, double d);
void waitForEnter();
int main()
{
// Load machine
std::vector<Beverage> beverages { { "Coca-Cola", 150, 20 },
{ "Sprite", 125, 20 },
{ "Gatorade", 225, 20 },
{ "Spring-Water", 185, 20 } };
int credit {}; // if user is owed money
char userAnswer = 'Y';
do {
unsigned drink = getBeverage(beverages);
if(drink == 'X') {
credit = giveBackMoney(credit);
continue;
}
credit += askPayment(beverages.at(drink), credit);
updateBeverageQty(beverages.at(drink));
updateSoldBeverageQty(beverages.at(drink));
std::cout << "Do you want another beverage? (Y/n):";
std::cin >> userAnswer;
} while (userAnswer == 'Y' || userAnswer == 'y');
credit = giveBackMoney(credit);
int operation {};
std::vector<double> profit;
computeTotalProfit(profit, beverages);
do {
operation = getChoice();
switch (operation)
{
case 1: // display total profit
displayTotalProfit(profit, beverages);
break;
case 2: { // Best
unsigned index = getBestPerformer(profit);
displayTabHeader;
displayDrink(beverages.at(index), profit.at(index));
}
break;
case 3: { // worst
unsigned index = getWorstPerformer(profit);
displayTabHeader;
displayDrink(beverages.at(index), profit.at(index));
}
break;
case 4:
return 0; // exit from program
}
} while (0 < operation && operation < 5);
waitForEnter();
return 0;
}
|