Solution

// Programmer: Jennifer Leopold date: September 17, 2014

// File: hw4.cpp

// Purpose: Simulate a vending machine that dispenses

// medications.

//

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

//

// To execute: ./hw4

#include <iostream>

using namespace std;

int main()

{

const float NAPROSYN_COST = 10.0; // cost of each drug

const float KRYSTEXXA_COST = 3.5;

const float SCHRAUT_COST = 4;

const float XELJANZ_COST = 6.75;

const float QNASL_COST = 12.25;

const float GOLYTELY_COST = 14.33;

const short NAPROSYN_PILLS = 15; // # pills for each drug

const short KRYSTEXXA_PILLS = 30;

const short SCHRAUT_PILLS = 20;

const short XELJANZ_PILLS = 5;

const short QNASL_PILLS = 40;

const short GOLYTELY_PILLS = 1;

float cost; // customer's charges

short numPills = 0; // customer's total pills

float payment; // amount tendered

bool invalidPayment; // true if customer doesn't

// enter valid payment

short pillContainerSize; // std pill container size

// that customer will need

short menuChoice; // vars associated with

bool invalidMenuChoice; // making menu selection

bool isTimeToQuit = false; // true when time to

// terminate program

do

{

// Greet user and get menu selection

//

cout << "\nWelcome to the InstyMeds Vending Machine!\n";

cout << "Here are your choices\n";

cout << " 1. Naprosyn\n";

cout << " 2. Krystexxa\n";

cout << " 3. Schraut!\n";

cout << " 4. Xeljanz\n";

cout << " 5. Qnasl Dipropionate\n";

cout << " 6. GoLytely\n";

cout << " 7. Quit (shut down the machine)\n";

do

{

cout << "Please make a selection (1-7): ";

cin >> menuChoice;

invalidMenuChoice = (menuChoice < 1) || (menuChoice > 7);

if (invalidMenuChoice)

cout << "Invalid entry!\n";

} while (invalidMenuChoice);

// Determine cost and # pills for selection

//

numPills = 0;

switch (menuChoice)

{

case 1: cost = NAPROSYN_COST;

numPills = NAPROSYN_PILLS;

break;

case 2: cost = KRYSTEXXA_COST;

numPills = KRYSTEXXA_PILLS;

break;

case 3: cost = SCHRAUT_COST;

numPills = SCHRAUT_PILLS;

break;

case 4: cost = XELJANZ_COST;

numPills += XELJANZ_PILLS; // will also get 5

case 5: cost = QNASL_COST;

numPills += QNASL_PILLS; // will also get 6

case 6: cost = GOLYTELY_COST;

numPills += GOLYTELY_PILLS;

break;

case 7: isTimeToQuit= true;

}

if (! isTimeToQuit)

{

// Prompt user for payment, and then output change

//

do

{

cout << "\nYour total is $" << cost << endl;

cout << "Please enter payment: ";

cin >> payment;

invalidPayment = payment < cost;

if (invalidPayment)

cout << "That isn't enough!\n";

} while (invalidPayment);

cout << "Your change is $"

<< (payment - cost) << endl;

// Instruct user to prepare appropriately sized

// pill container

//

if (numPills <= 10)

pillContainerSize = 10;

else if ((numPills > 10) && (numPills <= 25))

pillContainerSize = 25;

else if ((numPills > 25) && (numPills <= 50))

pillContainerSize = 50;

else if ((numPills > 50) &&

(numPills <= 100))

pillContainerSize = 100;

else pillContainerSize = 999;

cout << "Medications will be dispensed in 30 seconds.\n";

cout << "You need to have a ";

if (pillContainerSize <= 100)

cout << pillContainerSize << " unit pill container";

else cout << "bucket";

cout << " ready.\n";

}

} while (! isTimeToQuit);

cout << "\nInstyMeds is shutting down. Goodbye!\n";

return 0;

}