Solution

/*

Programmer: Jennifer Leopold

Date: September 13, 2016

File: hw4.cpp

Purpose: Simulate a vending machine that dispenses

homeopathic remedies/supplements.

To compile: fg++ hw4.cpp -o hw4

To execute: ./hw4

*/

#include <iostream>

using namespace std;

// Menu selections

const string MENU_ITEM_1 = "Ginko Root";

const string MENU_ITEM_2 = "Mandrake Root";

const string MENU_ITEM_3 = "Ginseng Root";

const string MENU_ITEM_4 = "Square Root";

const string MENU_ITEM_5 = "Vitamin R Root";

const string MENU_ITEM_QUIT = "Quit";

const int NUM_MENU_ITEMS = 6;

// Price for each menu item

const float MENU_ITEM_1_PRICE = 4.5;

const float MENU_ITEM_2_PRICE = 1.23;

const float MENU_ITEM_3_PRICE = 2.39;

const float MENU_ITEM_4_PRICE = 99.98;

const float MENU_ITEM_5_PRICE = 0.78;

// Initial quantity of each menu item

const int MENU_ITEM_1_QTY = 5;

const int MENU_ITEM_2_QTY = 4;

const int MENU_ITEM_3_QTY = 9;

const int MENU_ITEM_4_QTY = 11;

const int MENU_ITEM_5_QTY = 8;

const string OUT_OF_STOCK = " *NO LONGER AVAILABLE* ";

// Snarky error messages

const string ATTENTION_MSG = "Can't you read? It says";

const string TRY_AGAIN1 = "Sigh...let's try this again.";

const string TRY_AGAIN2 =

"Take a deep breath and think VERY carefully.";

const string TRY_AGAIN3 =

"Really...would that make sense on ANY planet?";

const string TRY_AGAIN4 =

"Out of curiosity, are you related to Ralph Wiggum?";

// Sales tax rate

const float TAX_RATE = 0.16;

int main()

{

string customerName; // customer's name

float total = 0; // customer's total charges

float tax; // tax on customer's total

float amountRendered; // amount customer renders

int numMenu1 = MENU_ITEM_1_QTY; // qty of each menu item

int numMenu2 = MENU_ITEM_2_QTY;

int numMenu3 = MENU_ITEM_3_QTY;

int numMenu4 = MENU_ITEM_4_QTY;

int numMenu5 = MENU_ITEM_5_QTY;

short menuChoice; // menu selection

short quantity; // quantity of selected item

bool invalidEntry; // true whenever user doesn't

// enter valid input

bool isTimeToQuit = false; // true when purchases

// completed

// Set precision to 2 decimal places for dollar amount output

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

cout.precision(2);

// Greet user

cout << "\nWelcome to the Roots 'n Stuff Vending Machine!\n";

cout << "Please enter your name (no spaces): ";

cin >> customerName;

cout << "Glad to meet you, " << customerName << "!\n";

// Let user repeatedly make selections from the menu

// until s/he chooses to 'quit'

do

{

cout << "\nHere's what we have for you today:\n";

cout << " 1. " << MENU_ITEM_1 << " ($"

<< MENU_ITEM_1_PRICE << ")"

<< ((numMenu1 <= 0)? OUT_OF_STOCK : "") << endl;

cout << " 2. " << MENU_ITEM_2 << " ($"

<< MENU_ITEM_2_PRICE << ")"

<< ((numMenu2 <= 0)? OUT_OF_STOCK : "") << endl;

cout << " 3. " << MENU_ITEM_3 << " ($"

<< MENU_ITEM_3_PRICE << ")"

<< ((numMenu3 <= 0)? OUT_OF_STOCK : "") << endl;

cout << " 4. " << MENU_ITEM_4 << " ($"

<< MENU_ITEM_4_PRICE << ")"

<< ((numMenu4 <= 0)? OUT_OF_STOCK : "") << endl;

cout << " 5. " << MENU_ITEM_5 << " ($"

<< MENU_ITEM_5_PRICE << ")"

<< ((numMenu5 <= 0)? OUT_OF_STOCK : "") << endl;

cout << " 6. " << MENU_ITEM_QUIT << endl;

// Get menu selection

do

{

cout << "Please make a selection (1-"

<< NUM_MENU_ITEMS << "): ";

cin >> menuChoice;

invalidEntry = (menuChoice < 1) ||

(menuChoice > NUM_MENU_ITEMS);

if (invalidEntry)

cout << "Invalid entry! " << TRY_AGAIN1 << endl;

} while (invalidEntry);

// Update customer's running total and item inventory

switch (menuChoice)

{

case 1: if (numMenu1 <= 0)

cout << ATTENTION_MSG << OUT_OF_STOCK << endl

<< TRY_AGAIN2 << endl;

else

{

cout << "How many of those do you want? ";

cin >> quantity;

if (quantity < 0)

cout << "Invalid entry! " << TRY_AGAIN3

<< endl;

else

{

if (numMenu1 < quantity)

{

quantity = numMenu1;

cout << "\nSorry, we only have "

<< quantity << " of the "

<< MENU_ITEM_1

<< " left to give you.\n";

}

total += quantity * MENU_ITEM_1_PRICE;

numMenu1 -= quantity;

}

}

break;

case 2: if (numMenu2 <= 0)

cout << ATTENTION_MSG << OUT_OF_STOCK << endl

<< TRY_AGAIN2 << endl;

else

{

cout << "How many of those do you want? ";

cin >> quantity;

if (quantity < 0)

cout << "Invalid entry! " << TRY_AGAIN3

<< endl;

else

{

if (numMenu2 < quantity)

{

quantity = numMenu2;

cout << "\nSorry, we only have "

<< quantity << " of the "

<< MENU_ITEM_2

<< " left to give you.\n";

}

total += quantity * MENU_ITEM_2_PRICE;

numMenu2 -= quantity;

}

}

break;

case 3: if (numMenu3 <= 0)

cout << ATTENTION_MSG << OUT_OF_STOCK << endl

<< TRY_AGAIN3 << endl;

else

{

cout << "How many of those do you want? ";

cin >> quantity;

if (quantity < 0)

cout << "Invalid entry! " << TRY_AGAIN3

<< endl;

else

{

if (numMenu3 < quantity)

{

quantity = numMenu3;

cout << "\nSorry, we only have "

<< quantity << " of the "

<< MENU_ITEM_3

<< " left to give you.\n";

}

total += quantity * MENU_ITEM_3_PRICE;

numMenu3 -= quantity;

}

}

break;

case 4: if (numMenu4 <= 0)

cout << ATTENTION_MSG << OUT_OF_STOCK << endl

<< TRY_AGAIN2 << endl;

else

{

cout << "How many of those do you want? ";

cin >> quantity;

if (quantity < 0)

cout << "Invalid entry! " << TRY_AGAIN3

<< endl;

else

{

if (numMenu4 < quantity)

{

quantity = numMenu4;

cout << "\nSorry, we only have "

<< quantity << " of the "

<< MENU_ITEM_4

<< " left to give you.\n";

}

total += quantity * MENU_ITEM_4_PRICE;

numMenu4 -= quantity;

}

}

break;

case 5: if (numMenu5 <= 0)

cout << ATTENTION_MSG << OUT_OF_STOCK << endl

<< TRY_AGAIN2 << endl;

else

{

cout << "How many of those do you want? ";

cin >> quantity;

if (quantity < 0)

cout << "Invalid entry! " << TRY_AGAIN3

<< endl;

else

{

if (numMenu5 < quantity)

{

quantity = numMenu5;

cout << "\nSorry, we only have "

<< quantity << " of the "

<< MENU_ITEM_5

<< " left to give you.\n";

}

total += quantity * MENU_ITEM_5_PRICE;

numMenu5 -= quantity;

}

}

break;

case 6: isTimeToQuit= true;

}

} while (! isTimeToQuit);

// Prompt user for payment, and then output change

if (total > 0)

{

// Apply tax and round up to nearest cent

tax = total * TAX_RATE + 0.005;

total = total + tax;

// Get rid of part of total that is less than 1 cent

total = static_cast<float>

(static_cast<int>(total * 100))/100;

do

{

cout << "\nOK, " << customerName << ", that will be $"

<< total << endl;

cout << "Please enter payment: ";

cin >> amountRendered;

invalidEntry = amountRendered < total;

if (invalidEntry)

cout << "Umm, that isn't enough! "

<< TRY_AGAIN4 << endl;

} while (invalidEntry );

cout << "Your change is $"

<< (amountRendered - total) << endl;

}

// Sign off

cout << "\nStay healthy my friend, and remember to "

<< "eat your roots and vegetables!\n";

return 0;

}