hw6_fncts.cpp

/*

Programmer: Jennifer Leopold

File: hw6_functs.cpp

Purpose: This file contains the function definitions for

the HW #6 vending machine program.

*/

#include "hw6_functs.h"

int myRand(const int low, const int high)

{

return((rand() % (high - low + 1)) + low);

}

string greetUser()

{

string customerName;

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";

return(customerName);

}

void signOff(const string customerName)

{

cout << "\nStay healthy, " << customerName

<< ", and remember to "

<< "eat plenty of roots and vegetables!\n";

return;

}

void displayMenu(const int numMenu1, const int numMenu2,

const int numMenu3, const int numMenu4,

const int numMenu5)

{

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;

return;

}

int getMenuSelection()

{

int userMenuChoice, randMenuChoice;

bool invalidEntry;

// Loop until a valid menu selection is chosen

do

{

// Let user make a selection

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

<< NUM_MENU_ITEMS << "): ";

cin >> userMenuChoice;

invalidEntry = (userMenuChoice < 1) ||

(userMenuChoice > NUM_MENU_ITEMS);

if (invalidEntry)

cout << "Invalid entry! "

<< "That's not on the menu!\n";

else

{

// Then program overrides by randomly making a selection,

// possibly an invalid selection

randMenuChoice = myRand(1, NUM_MENU_ITEMS + 1);

if (randMenuChoice == userMenuChoice)

cout << "Fine! ...but you're going to be sorry\n";

else cout << randomMenuChoiceMsg(randMenuChoice) << endl;

invalidEntry = (randMenuChoice > NUM_MENU_ITEMS);

}

} while (invalidEntry);

return(randMenuChoice);

}

string randomMenuChoiceMsg(const int menuChoice)

{

string msg;

switch(rand() % NUM_MENU_SELECTION_COMMENTS)

{

case 0 : msg = "No, you really want: ";

break;

case 1 : msg = "As my uncle Hal would say, \"I can't do";

msg.append(" that for you, Dave.\" You want ");

break;

case 2 : msg = "Only a loooooser would want THAT. ";

msg.append("Try this instead: ");

break;

case 3 : msg = "WRONG! I'm sure you meant to pick ";

break;

case 4 : msg = "PPpfffffttt. I think you need ";

break;

case 5 : msg = "Only idiots choose that. ";

msg.append("Better to have some ");

default: msg = "Tss, you must be Homer! Only he'd want ";

msg.append("that. Lisa's preference is ");

}

switch (menuChoice)

{

case 1 : msg.append(MENU_ITEM_1);

break;

case 2 : msg.append(MENU_ITEM_2);

break;

case 3 : msg.append(MENU_ITEM_3);

break;

case 4 : msg.append(MENU_ITEM_4);

break;

case 5 : msg.append(MENU_ITEM_5);

break;

case 6 : msg.append(MENU_ITEM_QUIT);

break;

default: msg.append("something we don't have");

}

return(msg);

}

int getQuantityInput(const int numAvailable,

const string itemName)

{

int userQuantity, quantity = 0;

if (numAvailable <= 0)

cout << ATTENTION_MSG << OUT_OF_STOCK << endl

<< TRY_AGAIN2 << endl;

else

{

// Let user enter quantity

cout << "How many " << itemName << " do you want? ";

cin >> userQuantity;

if (userQuantity <= 0)

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

else

{

// Program overrides by randomly choosing a quantity

quantity = myRand(1, QUANTITY_MULTIPLIER * userQuantity);

commentOnQuantity(quantity, userQuantity);

if (numAvailable < quantity)

{

quantity = numAvailable;

cout << "\nSorry, we only have "

<< quantity << " of the " << itemName

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

}

}

}

return(quantity);

}

void commentOnQuantity(const int computerQty, const int userQty)

{

if (computerQty == userQty)

cout << "I can do that.\n";

else if (computerQty > userQty)

cout << "Naw naw naw, you need "

<< computerQty << ".\n";

else cout << "That's waayyy too much for you. "

<< "You want " << computerQty << ".\n";

return;

}

void applyTax(float &total)

{

float tax, taxRate;

// Tax rate depends on subtotal amount

if (total < SMALL_PURCHASE_LIMIT)

taxRate = TAX_RATE_SMALL_PURCHASES;

else if (total <= MED_PURCHASE_LIMIT)

taxRate = TAX_RATE_MED_PURCHASES;

else taxRate = TAX_RATE_LARGE_PURCHASES;

// Round up tax to nearest cent

tax = total * taxRate + 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;

return;

}

void getPaid(const float total, const string customerName)

{

float amountRendered;

bool invalidEntry;

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;

return;

}