Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class vendingMachine
  7. {
  8. public:
  9. //attributes
  10. int numberofcansofcoke;
  11. int numberofcansofsprite;
  12. int numberofcansofdrpepper;
  13. int numberofcansofwater;
  14. float priceofcoke;
  15. float priceofsprite;
  16. float priceofdrpepper;
  17. float priceofwater;
  18.  
  19. //constructor
  20. vendingMachine()
  21. {
  22. numberofcansofcoke = 10;
  23. numberofcansofsprite = 15;
  24. numberofcansofdrpepper = 10;
  25. numberofcansofwater = 20;
  26. priceofcoke = 1.25;
  27. priceofsprite = 1.25;
  28. priceofdrpepper = 1.50;
  29. priceofwater = 1.00;
  30. }
  31.  
  32. //functions
  33. void dispensecoke()
  34. {
  35. float userGivenAmount;
  36. cout << "Price of Coke: " << priceofcoke << endl;
  37. cout << "Enter cash" << endl;
  38. cin >> userGivenAmount;
  39.  
  40. if (userGivenAmount >= priceofcoke)
  41. {
  42. if (numberofcansofcoke > 0)
  43. {
  44. cout << " Grab your can. And oh, don't forget your change: " << userGivenAmount - priceofcoke << endl;
  45. numberofcansofcoke--;
  46. }
  47. else
  48. {
  49. cout << " Sorry, ran out of coke." << endl;
  50. }
  51.  
  52. }
  53. else
  54. {
  55. cout << "Insufficient cash for coke." << endl;
  56. }
  57. }
  58. void dispensesprite()
  59. {
  60. float userGivenAmount;
  61. cout << "Enter cash" << endl;
  62. cin >> userGivenAmount;
  63.  
  64. if (userGivenAmount >= priceofsprite)
  65. {
  66. if (numberofcansofsprite > 0)
  67. {
  68. cout << " Grab your can. And oh, don't forget your change: " << userGivenAmount - priceofsprite << endl;
  69. numberofcansofsprite--;
  70. }
  71. else
  72. {
  73. cout << " Sorry, ran out of sprite." << endl;
  74. }
  75.  
  76. }
  77. else
  78. {
  79. cout << "Insufficient cash for sprite." << endl;
  80. }
  81. }
  82. void dispensedrpepper()
  83. {
  84. float userGivenAmount;
  85. cout << "Enter cash" << endl;
  86. cin >> userGivenAmount;
  87.  
  88. if (userGivenAmount >= priceofdrpepper)
  89. {
  90. if (numberofcansofdrpepper > 0)
  91. {
  92. cout << " Grab your can. And oh, don't forget your change: " << userGivenAmount - priceofdrpepper << endl;
  93. numberofcansofdrpepper--;
  94. }
  95. else
  96. {
  97. cout << " Sorry, ran out of Dr. Pepper." << endl;
  98. }
  99.  
  100. }
  101. else
  102. {
  103. cout << "Insufficient cash for Dr. Pepper." << endl;
  104. }
  105. }
  106. void dispensewater()
  107. {
  108. float userGivenAmount;
  109. cout << "Enter cash" << endl;
  110. cin >> userGivenAmount;
  111.  
  112. if (userGivenAmount >= priceofwater)
  113. {
  114. if (numberofcansofwater > 0)
  115. {
  116. cout << " Grab your bottle. And oh, don't forget your change: " << userGivenAmount - priceofwater << endl;
  117. numberofcansofwater--;
  118. }
  119. else
  120. {
  121. cout << " Sorry, ran out of water." << endl;
  122. }
  123.  
  124. }
  125. else
  126. {
  127. cout << "Insufficient cash for water." << endl;
  128. }
  129. }
  130. void addcan()
  131. {
  132. int canadded;
  133. int choice;
  134.  
  135. cout << "Press 1 to add Coke" << endl;
  136. cout << "Press 2 to add Sprite" << endl;
  137. cout << "Press 3 to add Dr. Pepper" << endl;
  138. cout << "Press 4 to add Water" << endl;
  139. cout << "Press anything to return to main menu." << endl;
  140. cin >> choice;
  141.  
  142. switch (choice)
  143. {
  144. case 1:
  145. cout << "There are currently " << numberofcansofcoke << " left." << endl;
  146. cout << "How many do you wish to add?" << endl;
  147. cin >> canadded;
  148. numberofcansofcoke = numberofcansofcoke + canadded;
  149. break;
  150. case 2:
  151. cout << "There are currently " << numberofcansofsprite << " left." << endl;
  152. cout << "How many do you wish to add?" << endl;
  153. cin >> canadded;
  154. numberofcansofsprite = numberofcansofsprite + canadded;
  155. break;
  156. case 3:
  157. cout << "There are currently " << numberofcansofdrpepper << " left." << endl;
  158. cout << "How many do you wish to add?" << endl;
  159. cin >> canadded;
  160. numberofcansofdrpepper = numberofcansofdrpepper + canadded;
  161. break;
  162. case 4:
  163. cout << "There are currently " << numberofcansofwater << " left." << endl;
  164. cout << "How many do you wish to add?" << endl;
  165. cin >> canadded;
  166. numberofcansofwater = numberofcansofwater + canadded;
  167. break;
  168. default:
  169. exit(1);
  170. }
  171. }
  172. void pricechange()
  173. {
  174. float newprice;
  175. int choice;
  176.  
  177. cout << "Press 1 to change price of Coke" << endl;
  178. cout << "Press 2 to change price of Sprite" << endl;
  179. cout << "Press 3 to change price ofDr. Pepper" << endl;
  180. cout << "Press 4 to change price of Water" << endl;
  181. cout << "Press anything to return to main menu." << endl;
  182. cin >> choice;
  183.  
  184. switch (choice)
  185. {
  186. case 1:
  187. cout << "Current price is: " << priceofcoke << endl;
  188. cout << "Enter new price for Coke." << endl;
  189. cin >> newprice;
  190. priceofcoke = newprice;
  191. break;
  192. case 2:
  193. cout << "Current price is: " << priceofsprite << endl;
  194. cout << "Enter new price for Sprite" << endl;
  195. cin >> newprice;
  196. priceofsprite = newprice;
  197. break;
  198. case 3:
  199. cout << "Current price is: " << priceofdrpepper << endl;
  200. cout << "Enter new price for Dr. Pepper." << endl;
  201. cin >> newprice;
  202. priceofdrpepper = newprice;
  203. break;
  204. case 4:
  205. cout << "Current price is: " << priceofwater << endl;
  206. cout << "Enter new price for Water." << endl;
  207. cin >> newprice;
  208. priceofwater = newprice;
  209. break;
  210. default:
  211. break;
  212. }
  213. }
  214. };
  215.  
  216. int main()
  217. {
  218. int choice;
  219. vendingMachine myMachine;
  220.  
  221. while (1)
  222. {
  223. cout << "Press 1 for Coke." << endl;
  224. cout << "Press 2 for Sprite." << endl;
  225. cout << "Press 3 for Dr. Pepper." << endl;
  226. cout << "Press 4 for Water." << endl;
  227. cout << "Press 5 to add cans and bottles." << endl;
  228. cout << "Press 6 to change prices." << endl;
  229. cout << "Press anything else to leave the machine." << endl;
  230. cin >> choice;
  231.  
  232. switch (choice)
  233. {
  234. case 1:
  235. myMachine.dispensecoke();
  236. break;
  237. case 2:
  238. myMachine.dispensesprite();
  239. break;
  240. case 3:
  241. myMachine.dispensedrpepper();
  242. break;
  243. case 4:
  244. myMachine.dispensewater();
  245. break;
  246. case 5:
  247. myMachine.addcan();
  248. break;
  249. case 6:
  250. myMachine.pricechange();
  251. break;
  252. default: cout << "Bye-Bye" << endl;
  253. exit(1);
  254. }
  255. }
  256. return 1;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement