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
|
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
class coins // Structure for vending machine coins
{
public:
void setCoins(int dollars, int quarters, int dimes, int nickels);
void requestMoney();
bool checkChange(coins buffer, coins cash, coins& returned, int cost);
void dispenseChange(coins buffer, coins& cash, coins returned, int cost);
int getDollar();
int getQuarter();
int getDime();
int getNickel();
private:
int dollar, quarter, dime, nickel;
};
class stock // Structure for vending machine stock
{
public:
void setInventory(int slot1, int slot2, int slot3, int slot4, int slot5, int slot6, int slot7, int slot8, int slot9, int slot10);
void setPrice(int slot1, int slot2, int slot3, int slot4, int slot5, int slot6, int slot7, int slot8, int slot9, int slot10);
void setStockNames(string slot1, string slot2, string slot3, string slot4, string slot5, string slot6, string slot7, string slot8, string slot9, string slot10);
void displayMenu();
void dispenseItem(int item);
int getItem(int item);
string getName(int item);
bool checkStock(int item);
private:
int stockItem[10];
string stockName[10];
};
int main()
{
coins cash, buffer, returned; // Declare variables for the coins struct.
stock inventory, price; // Declare variables for the stock struct.
int choice, item; // Declare the integer "choice" for us in the switch statement
char answer;
cash.setCoins(300,75,30,15); // Call function to set the initial amount of cash in the machine.
inventory.setInventory(3,3,3,3,3,3,3,3,3,3); // Call function to set the amount of inventory for each item.
inventory.setStockNames("a Box of Paper Clips. What were you thinking?","a cup of Coffee.","an Elephant. (Shipping not included)", "some flowers.", "a pint of Guiness. (Must be 21 or older, except in Germany)","a 20oz Pepsi. Goes great with a sandwhich!","a Boston Terrier puppy. Give it lots of hugs!","a ham sandwhich (goes great with a Pepsi!).","a Tickle Me Elmo (watch out for crazy moms...).","an umbrella.");
price.setPrice(15,25,200,75,100,30,150,50,250,125); // Call function to set each item's price
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(2);
do
{
returned.setCoins(0,0,0,0); // Set the returned coins value to 0
price.displayMenu(); // Call function to display the menu
buffer.requestMoney(); // Call function to request user to input money after seeing menu.
cout << "\n\nChoose an item from the menu above (1-10): ";
cin >> choice; // Ask for user's choice from the menu.
item = choice-1; // Set variable 'item' to user's choice(-1).
switch (choice) // Switch statement using the user's choice as argument.
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
{
cout << "\nYou've chosen " << inventory.getName(item) << endl;
if(inventory.checkStock(item)) // If the item is in stock...
{
if(buffer.checkChange(buffer, cash, returned, price.getItem(item))) // check to see if the machine can make change
{
cash.dispenseChange(buffer, cash, returned, price.getItem(item)); // If so, take the user's money and dispense the necessary change...
inventory.dispenseItem(item); // then dispense the item.
}
}
break;
}
default: // Default option if an incorrect choice is made
{
cout << "\nPlease choose a valid item...\n"
<< "Your change has been returned.\n";
cin.clear(); // Clear the cache
cin.ignore(1000,'\n'); // Ignore up to 1000 characters or a new line, whichever comes first.
}
}
cout << "\nDollars left in machine: " << setw(3) << cash.getDollar()/100 << endl // Tell the user how many dollars are left in the machine.
<< "Quarters left in machine: " << setw(2) << cash.getQuarter()/25 << endl // Tell the user how many quarters are left in the machine.
<< "Dimes left in machine: " << setw(5) << cash.getDime()/10 << endl // Tell the user how many dimes are left in the machine.
<< "Nickels left in machine: " << setw(3) << cash.getNickel()/5; // Tell the user how many nickels are left in the machine.
cout << "\n\nWould you like another item? Type Y for yes or N for no: "; // Ask if the user wants to choose another item.
cin >> answer;
}while(answer=='y' || answer=='Y');
cout << endl; // Formatting only
return 0;
}
|