/*
Programmer: Jennifer Leopold
Date: February 8, 2018
File: hw4.cpp
Purpose: Simulate a vending machine that sells snacks and
computes pounds user will lose by eating the
selected item based on user's metabolic rate.
To compile: fg++ hw4.cpp -o hw4
To execute: ./hw4
*/
#include <iostream>
using namespace std;
// Menu selections
const string MENU_ITEM_1 = "Dry Hay";
const string MENU_ITEM_2 = "Wet Hay";
const string MENU_ITEM_3 = "Kale";
const string MENU_ITEM_4 = "Ground Corn";
const string MENU_ITEM_5 = "Carrots";
const string MENU_ITEM_6 = "Carrots";
const string MENU_ITEM_7 = "Carrots";
const string MENU_ITEM_8 = "Industrially Milled Fortified "
"Sawdust Horse Feed";
const string MENU_ITEM_QUIT = "Quit";
const int NUM_MENU_ITEMS = 9;
// Price for each menu item
const float MENU_ITEM_1_PRICE = 1.5;
const float MENU_ITEM_2_PRICE = 1.25;
const float MENU_ITEM_3_PRICE = 0.75;
const float MENU_ITEM_4_PRICE = 2.5;
const float MENU_ITEM_5_PRICE = 1;
const float MENU_ITEM_6_PRICE = 1;
const float MENU_ITEM_7_PRICE = 1;
const float MENU_ITEM_8_PRICE = 0.5;
// Metabolic weight loss multiplying factor for each item
const int MENU_ITEM_1_WEIGHT_LOSS_FACTOR = 34;
const int MENU_ITEM_2_WEIGHT_LOSS_FACTOR = 33;
const int MENU_ITEM_3_WEIGHT_LOSS_FACTOR = 26;
const int MENU_ITEM_4_WEIGHT_LOSS_FACTOR = 12;
const int MENU_ITEM_5_WEIGHT_LOSS_FACTOR = 25;
const int MENU_ITEM_6_WEIGHT_LOSS_FACTOR = 25;
const int MENU_ITEM_7_WEIGHT_LOSS_FACTOR = 25;
const int MENU_ITEM_8_WEIGHT_LOSS_FACTOR = 13;
// Valid range for user's metabolic rate
const float METABOLIC_RATE_LOW = 0.05;
const float METABOLIC_RATE_HIGH = 0.09;
int main()
{
float metabolicRate; // user's metabolic rate
bool validInput; // true if input is valid
int menuChoice; // user's vending machine choice
bool isTimeToQuit = false; // true when user wants to quit
string selectedItemName; // chosen vending item name
float amtOwed; // amt owed for vending item
float amountRendered; // amt paid by user
float weightLoss; // # lbs lost for item eaten
// Greet user
cout << "\nWelcome to the Healthy Living "
<< "Vending Machine!\n\n";
// Get input for user's metabolic rate
do
{
cout << "Enter your metabolic rate ("
<< METABOLIC_RATE_LOW << "-"
<< METABOLIC_RATE_HIGH << "): ";
cin >> metabolicRate;
validInput = (metabolicRate >= METABOLIC_RATE_LOW) &&
(metabolicRate <= METABOLIC_RATE_HIGH);
if (! validInput)
cout << "Invalid input!\n";
} while (! validInput);
// Set precision to 2 decimal places for numeric outputs
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
// Let user repeatedly make selections from the vending
// machine until s/he chooses to 'quit'
do
{
// Display choices
cout << "\nHere's what we have for you today:\n";
cout << " 1. " << MENU_ITEM_1 << "\t\t$"
<< MENU_ITEM_1_PRICE << "\t("
<< MENU_ITEM_1_WEIGHT_LOSS_FACTOR << ")" << endl;
cout << " 2. " << MENU_ITEM_2 << "\t\t$"
<< MENU_ITEM_2_PRICE << "\t("
<< MENU_ITEM_2_WEIGHT_LOSS_FACTOR << ")" << endl;
cout << " 3. " << MENU_ITEM_3 << "\t\t$"
<< MENU_ITEM_3_PRICE << "\t("
<< MENU_ITEM_3_WEIGHT_LOSS_FACTOR << ")" << endl;
cout << " 4. " << MENU_ITEM_4 << "\t$"
<< MENU_ITEM_4_PRICE << "\t("
<< MENU_ITEM_4_WEIGHT_LOSS_FACTOR << ")" << endl;
cout << " 5. " << MENU_ITEM_5 << "\t\t$"
<< MENU_ITEM_5_PRICE << "\t("
<< MENU_ITEM_5_WEIGHT_LOSS_FACTOR << ")" << endl;
cout << " 6. " << MENU_ITEM_6 << "\t\t$"
<< MENU_ITEM_6_PRICE << "\t("
<< MENU_ITEM_6_WEIGHT_LOSS_FACTOR << ")" << endl;
cout << " 7. " << MENU_ITEM_7 << "\t\t$"
<< MENU_ITEM_7_PRICE << "\t("
<< MENU_ITEM_7_WEIGHT_LOSS_FACTOR << ")" << endl;
cout << " 8. " << MENU_ITEM_8 << " $"
<< MENU_ITEM_8_PRICE << "\t("
<< MENU_ITEM_8_WEIGHT_LOSS_FACTOR << ")" << endl;
cout << " 9. " << MENU_ITEM_QUIT << endl;
// Get selection; determine amount owed and lbs user
// will lose from eating this item
do
{
cout << "Please make a selection (1-"
<< NUM_MENU_ITEMS << "): ";
cin >> menuChoice;
validInput = (menuChoice >= 1) &&
(menuChoice <= NUM_MENU_ITEMS);
if (! validInput)
cout << "Invalid selection!\n";
} while (! validInput);
// Determine amount owed and # lbs user will lose
switch (menuChoice)
{
case 1: amtOwed = MENU_ITEM_1_PRICE;
weightLoss = MENU_ITEM_1_WEIGHT_LOSS_FACTOR *
metabolicRate;
selectedItemName = MENU_ITEM_1;
break;
case 2: amtOwed = MENU_ITEM_2_PRICE;
weightLoss = MENU_ITEM_2_WEIGHT_LOSS_FACTOR *
metabolicRate;
selectedItemName = MENU_ITEM_2;
break;
case 3: amtOwed = MENU_ITEM_3_PRICE;
weightLoss = MENU_ITEM_3_WEIGHT_LOSS_FACTOR *
metabolicRate;
selectedItemName = MENU_ITEM_3;
break;
case 4: amtOwed = MENU_ITEM_4_PRICE;
weightLoss = MENU_ITEM_4_WEIGHT_LOSS_FACTOR *
metabolicRate;
selectedItemName = MENU_ITEM_4;
break;
case 5: // Items 5-7 are all the same
case 6:
case 7: amtOwed = MENU_ITEM_7_PRICE;
weightLoss = MENU_ITEM_7_WEIGHT_LOSS_FACTOR *
metabolicRate;
selectedItemName = MENU_ITEM_7;
break;
case 8: amtOwed = MENU_ITEM_8_PRICE;
weightLoss = MENU_ITEM_8_WEIGHT_LOSS_FACTOR *
metabolicRate;
selectedItemName = MENU_ITEM_8;
break;
case 9: isTimeToQuit = true;
}
if (!isTimeToQuit)
{
// Prompt user for payment, and then output change
// and info about weight loss
cout << "\nYou chose " << selectedItemName << "...\n";
do
{
cout << "\nPlease deposit $" << amtOwed << ": ";
cin >> amountRendered;
validInput = amountRendered >= amtOwed;
if (! validInput)
cout << "Umm, that isn't enough!\n";
} while (!validInput);
cout << "Your change is $"
<< (amountRendered - amtOwed) << endl;
cout << "BTW, by eating " << selectedItemName
<< ", you will lose " << weightLoss << " lbs!\n";
}
} while (! isTimeToQuit);
// Sign off
cout << "\nStay healthy my friend!\n";
return 0;
}