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 171 172 173
|
/****************************************************************************************
Chapter 11, Programming Challenge 13: Drink Machine Simulator
Written by:
****************************************************************************************/
// **************************************************************************************
// INCLUDES
// **************************************************************************************
// system
//
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;
// **************************************************************************************
// CONSTANTS
// **************************************************************************************
const int NUM_DRINKS = 5;
// **************************************************************************************
// STRUCTURES
// **************************************************************************************
struct Drink
{
string name; // Drink name
double price; // Price of the drink
int num; // Number of cans in the machine
};
Drink Beverage;
// **************************************************************************************
// FUNCTIONS
// **************************************************************************************
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// GetChoice
// This function gets the user's choice and returns that value minus one.
// This adjustment is made because the value will be used as a subscript
// into the Drink array by the transaction function.
//
//
int GetChoice(Drink m[])
{
// local constants
//
// local variables
//
int choice;
// Display a list of drinks preceded by a number. This will be the menu.
// cout << "Put code here to DISPLAY the list of available drinks and prices" << endl;
cout << " Please select a drink by pressing on the corresponding number" << endl;
cout << " 1. Cola: .75cents " << endl;
cout << " 2. Root Beer : .75cents " << endl;
cout << " 3. Lemon Lime: .75cents " << endl;
cout << " 4. Grape Soda : .80cents " << endl;
cout << " 5. Cream Soda : .80cents " << endl;
// Display the last menu item, which is to leave the drink machine.
//
cout << (NUM_DRINKS + 1) << " Leave the drink machine\n\n";
// Get the user's choice.
//
cin >> choice;
// Validate the choice.
//
if (choice > 6 || choice < 1)
{
cout << " Please choose a valid option. ";
}
// Return the choice
//
return NUM_DRINKS; /* THIS TEMPLATE JUST RETURNS THE QUIT CHOICE*/
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// ProcessTransaction
// Process the user's choice and update the earnings variable
//
//
void ProcessTransaction(Drink m[], int choice, double &earnings)
{
float moneyRecieved;
float change;
// If the selected drink is sold out, display a message and get out of this function.
// otherwise
// Get some money from the customer. (ie. have the user input up to 1.00)
//
cout << " Please enter an amount less than $1.00 to pay for your drink. ";
cin >> moneyRecieved;
// Make sure the customer entered at least enough for the selected drink, and no more than $1.00.
//
if (moneyRecieved < 0 || moneyRecieved > 1)
{
cout << " Sorry. This vending machine does not accept money over $1.00. ";
}
// Process the selection and give back any change that is due.
//
change = moneyRecieved - Beverage.price - 1;
// if theMoneyReceived >= thePriceOfTheSelectedItem
{
// Dispense the drink message
// Calculate any change that is due.
// If change is due, give it to the customer.
// Update our earnings.
// Decrease the number of cans of the selected drink currently in the machine.
// Display the number of cans of this drink currently in the machine.
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// main
//
//
int main()
{
// local variables
//
int choice = 0; // Menu choice
double earnings = 0.0; // Drink machine earnings
Drink machine[NUM_DRINKS] = // Array of drinks in the machine
{
// name cost quantityAvailable
// ----------- ---- -----------------
{ "Cola ", 0.75, 20 },
{ "Root Beer", 0.75, 20 },
{ "Lemon-Lime", 0.75, 20 },
{ "Grape Soda", 0.80, 20 },
{ "Cream Soda", 0.80, 20 }
};
// initialization
//
cout << fixed << showpoint << setprecision(2); // Set the floating-point output formatting.
// Display the menu and process the user's choice.
//
do
{
choice = GetChoice(machine); // get the customer's choice
if (choice != NUM_DRINKS)
ProcessTransaction(machine, choice, earnings); // Process the transaction.
} while (choice != NUM_DRINKS);
// Display the machine's total earnings
//
cout << "Total earnings: $" << earnings << endl;
return 0;
}
|