Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- class vendingMachine
- {
- public:
- //attributes
- int numberofcansofcoke;
- int numberofcansofsprite;
- int numberofcansofdrpepper;
- int numberofcansofwater;
- float priceofcoke;
- float priceofsprite;
- float priceofdrpepper;
- float priceofwater;
- //constructor
- vendingMachine()
- {
- numberofcansofcoke = 10;
- numberofcansofsprite = 15;
- numberofcansofdrpepper = 10;
- numberofcansofwater = 20;
- priceofcoke = 1.25;
- priceofsprite = 1.25;
- priceofdrpepper = 1.50;
- priceofwater = 1.00;
- }
- //functions
- void dispensecoke()
- {
- float userGivenAmount;
- cout << "Price of Coke: " << priceofcoke << endl;
- cout << "Enter cash" << endl;
- cin >> userGivenAmount;
- if (userGivenAmount >= priceofcoke)
- {
- if (numberofcansofcoke > 0)
- {
- cout << " Grab your can. And oh, don't forget your change: " << userGivenAmount - priceofcoke << endl;
- numberofcansofcoke--;
- }
- else
- {
- cout << " Sorry, ran out of coke." << endl;
- }
- }
- else
- {
- cout << "Insufficient cash for coke." << endl;
- }
- }
- void dispensesprite()
- {
- float userGivenAmount;
- cout << "Enter cash" << endl;
- cin >> userGivenAmount;
- if (userGivenAmount >= priceofsprite)
- {
- if (numberofcansofsprite > 0)
- {
- cout << " Grab your can. And oh, don't forget your change: " << userGivenAmount - priceofsprite << endl;
- numberofcansofsprite--;
- }
- else
- {
- cout << " Sorry, ran out of sprite." << endl;
- }
- }
- else
- {
- cout << "Insufficient cash for sprite." << endl;
- }
- }
- void dispensedrpepper()
- {
- float userGivenAmount;
- cout << "Enter cash" << endl;
- cin >> userGivenAmount;
- if (userGivenAmount >= priceofdrpepper)
- {
- if (numberofcansofdrpepper > 0)
- {
- cout << " Grab your can. And oh, don't forget your change: " << userGivenAmount - priceofdrpepper << endl;
- numberofcansofdrpepper--;
- }
- else
- {
- cout << " Sorry, ran out of Dr. Pepper." << endl;
- }
- }
- else
- {
- cout << "Insufficient cash for Dr. Pepper." << endl;
- }
- }
- void dispensewater()
- {
- float userGivenAmount;
- cout << "Enter cash" << endl;
- cin >> userGivenAmount;
- if (userGivenAmount >= priceofwater)
- {
- if (numberofcansofwater > 0)
- {
- cout << " Grab your bottle. And oh, don't forget your change: " << userGivenAmount - priceofwater << endl;
- numberofcansofwater--;
- }
- else
- {
- cout << " Sorry, ran out of water." << endl;
- }
- }
- else
- {
- cout << "Insufficient cash for water." << endl;
- }
- }
- void addcan()
- {
- int canadded;
- int choice;
- cout << "Press 1 to add Coke" << endl;
- cout << "Press 2 to add Sprite" << endl;
- cout << "Press 3 to add Dr. Pepper" << endl;
- cout << "Press 4 to add Water" << endl;
- cout << "Press anything to return to main menu." << endl;
- cin >> choice;
- switch (choice)
- {
- case 1:
- cout << "There are currently " << numberofcansofcoke << " left." << endl;
- cout << "How many do you wish to add?" << endl;
- cin >> canadded;
- numberofcansofcoke = numberofcansofcoke + canadded;
- break;
- case 2:
- cout << "There are currently " << numberofcansofsprite << " left." << endl;
- cout << "How many do you wish to add?" << endl;
- cin >> canadded;
- numberofcansofsprite = numberofcansofsprite + canadded;
- break;
- case 3:
- cout << "There are currently " << numberofcansofdrpepper << " left." << endl;
- cout << "How many do you wish to add?" << endl;
- cin >> canadded;
- numberofcansofdrpepper = numberofcansofdrpepper + canadded;
- break;
- case 4:
- cout << "There are currently " << numberofcansofwater << " left." << endl;
- cout << "How many do you wish to add?" << endl;
- cin >> canadded;
- numberofcansofwater = numberofcansofwater + canadded;
- break;
- default:
- exit(1);
- }
- }
- void pricechange()
- {
- float newprice;
- int choice;
- cout << "Press 1 to change price of Coke" << endl;
- cout << "Press 2 to change price of Sprite" << endl;
- cout << "Press 3 to change price ofDr. Pepper" << endl;
- cout << "Press 4 to change price of Water" << endl;
- cout << "Press anything to return to main menu." << endl;
- cin >> choice;
- switch (choice)
- {
- case 1:
- cout << "Current price is: " << priceofcoke << endl;
- cout << "Enter new price for Coke." << endl;
- cin >> newprice;
- priceofcoke = newprice;
- break;
- case 2:
- cout << "Current price is: " << priceofsprite << endl;
- cout << "Enter new price for Sprite" << endl;
- cin >> newprice;
- priceofsprite = newprice;
- break;
- case 3:
- cout << "Current price is: " << priceofdrpepper << endl;
- cout << "Enter new price for Dr. Pepper." << endl;
- cin >> newprice;
- priceofdrpepper = newprice;
- break;
- case 4:
- cout << "Current price is: " << priceofwater << endl;
- cout << "Enter new price for Water." << endl;
- cin >> newprice;
- priceofwater = newprice;
- break;
- default:
- break;
- }
- }
- };
- int main()
- {
- int choice;
- vendingMachine myMachine;
- while (1)
- {
- cout << "Press 1 for Coke." << endl;
- cout << "Press 2 for Sprite." << endl;
- cout << "Press 3 for Dr. Pepper." << endl;
- cout << "Press 4 for Water." << endl;
- cout << "Press 5 to add cans and bottles." << endl;
- cout << "Press 6 to change prices." << endl;
- cout << "Press anything else to leave the machine." << endl;
- cin >> choice;
- switch (choice)
- {
- case 1:
- myMachine.dispensecoke();
- break;
- case 2:
- myMachine.dispensesprite();
- break;
- case 3:
- myMachine.dispensedrpepper();
- break;
- case 4:
- myMachine.dispensewater();
- break;
- case 5:
- myMachine.addcan();
- break;
- case 6:
- myMachine.pricechange();
- break;
- default: cout << "Bye-Bye" << endl;
- exit(1);
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement