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
|
#include <iostream>
#include <string>
#include <iomanip>
#include <climits>
using namespace std;
//Function Prototypes
int displayDrinksAndGetUserChoice(const string[], const double[], const int[], int);
int displayOPerationsAndGetUserChoice();
void updateBeverageQty(int, int[]);
void updateSoldBeverageQty(int, int[]);
void ComputeTotalProfit(const double[], int[], double[], int);
void DisplayTotalProfit(const string[], const double[], const int[], double[], int);
int getBestPerformer(const double[], const int);
int getWorstPerformer(const double[], int);
void displayBestDrink(const string[], const double[], const int[], double[], int);
void displayWorstDrink(const string[], const double[], const int[], double[], int);
int main()
{
const int MAX_NUM_DRINKS = 4;
int operation;
int index = 0;
string beverage[MAX_NUM_DRINKS] = { "Coca-Cola", "Sprite" , "Gatorade" , "Spring-Water" };
double price[MAX_NUM_DRINKS] = { /* Coca-Cola*/1.50, /*Sprite*/1.25, /*Gatorade*/2.25, /*Spring-Water*/ 1.85 };
int qtyOnHand[MAX_NUM_DRINKS] = {/* Coca-Cola*/20, /*Sprite*/20, /*Gatorade*/20, /*Spring-Water*/20 };
int qtySold[MAX_NUM_DRINKS] = { 0,0,0,0 };
double totalProfit[MAX_NUM_DRINKS];
int selectedDrink;
char userAnswer;
do
{
selectedDrink = displayDrinksAndGetUserChoice(beverage, price, qtyOnHand, MAX_NUM_DRINKS);
switch (selectedDrink)
{
case 1:
cout << "You selected: " << beverage[selectedDrink - 1] << endl;
updateBeverageQty(selectedDrink - 1, qtyOnHand);
updateSoldBeverageQty(selectedDrink - 1, qtySold);
break;
case 2:
cout << "You selected: " << beverage[selectedDrink - 1] << endl;
updateBeverageQty(selectedDrink - 1, qtyOnHand);
updateSoldBeverageQty(selectedDrink - 1, qtySold);
break;
case 3:
cout << "You selected: " << beverage[selectedDrink - 1] << endl;
updateBeverageQty(selectedDrink - 1, qtyOnHand);
updateSoldBeverageQty(selectedDrink - 1, qtySold);
break;
case 4:
cout << "You selected: " << beverage[selectedDrink - 1] << endl;
updateBeverageQty(selectedDrink - 1, qtyOnHand);
updateSoldBeverageQty(selectedDrink - 1, qtySold);
break;
default:
cout << "Please enter 1,2,3 or 4 only!" << endl;
}
cout << "Do you want to go again? (Y/n):";
cin >> userAnswer;
} while ((userAnswer == 'Y') || (userAnswer == 'y'));
do
{
operation = displayOPerationsAndGetUserChoice();
switch (operation)
{
case 1:
//calculate total profit
ComputeTotalProfit(price, qtySold, totalProfit, MAX_NUM_DRINKS);
DisplayTotalProfit(beverage, price, qtySold, totalProfit, MAX_NUM_DRINKS);
break;
case 2:
//Best
ComputeTotalProfit(price, qtySold, totalProfit, MAX_NUM_DRINKS);
index = getBestPerformer(totalProfit, MAX_NUM_DRINKS);
displayBestDrink(beverage, price, qtySold, totalProfit, index);
break;
case 3:
//worst
ComputeTotalProfit(price, qtySold, totalProfit, MAX_NUM_DRINKS);
index = getWorstPerformer(totalProfit, MAX_NUM_DRINKS);
displayWorstDrink(beverage, price, qtySold, totalProfit, index);
break;
case 4:
//Totals
ComputeTotalProfit(price, qtySold, totalProfit, MAX_NUM_DRINKS);
DisplayTotalProfit(beverage, price, qtySold, totalProfit, MAX_NUM_DRINKS);
break;
case 5:
default:
cout << "For operations please enter 1, 2, 3, or 4 only, key in 5 to exit" << endl;
}
} while (operation != 5);
system("PAUSE");
return 0;
}
//Function to get user choice
int displayDrinksAndGetUserChoice(const string drink[],
const double price[],
const int qty[],
int arrSize)
{
int userChoice = 0;
cout << "Welcome to COP 1334 Vending Machine" << endl;
cout << "Please choose from one of the following refreshing options\n" << endl;
cout << left;
// HEADER
cout << setw(23) << std::left << "-__DRINK NAME_________-"
<< setw(8) << std::left << "-___DRINK COST__"
<< setw(12) << std::left << "____NUMBER OF DRINKS IN MACHINE"
<< endl;
for (int i = 0; i < arrSize; i++)
{
cout << setw(1) << std::left << i + 1
<< setw(2) << std::left << ". "
<< setw(23) << std::left << drink[i]
<< setw(2) << std::left << "$"
<< setw(8) << std::left << price[i]
<< setw(12) << std::left << qty[i]
<< endl;
}
cout << "\nYour choice: ";
cin >> userChoice;
while (!cin || (userChoice < 1 || userChoice > 4))
{
cout << "Wrong input. Please enter values between 1 and 4 only:" << endl;
cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\nYour choice: ";
std::cin >> userChoice;
}
return userChoice;
}
//Function to make sure the user enters only valid money
bool isValidMoney = true;
while (isValidMoney == true)
{
double moneyAmount = 0;
double NICKLE = 0.05, DIME = 0.10, QUARTER = 0.25, DOLLAR = 1.00;
if moneyAmount = NICKLE == true
cout << "Enter an amount of money" << endl;
cout << "You need additional" << moneyAmount << " to purchase your drink" << endl;
cout << "Please enter either nickles, dimes, quaters, or dollars: " << endl;
}
| |