Teo_Yanchev

7.VendingMachine - C# Fundamentals

Sep 17th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. using System;
  2.  
  3. namespace demo
  4. {
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. decimal customerMoney = 0m;
  10. string output = string.Empty;
  11. string notEnoughMoney = "Sorry, not enought money";
  12.  
  13. string commandStart = Console.ReadLine();
  14. while (true)
  15. {
  16. decimal money = decimal.Parse(commandStart);
  17. bool acceptedCoins = (money == 0.1m || money == 0.2m || money == 0.5m
  18. || money == 1.00m || money == 2.00m);
  19.  
  20. if (acceptedCoins)
  21. {
  22. customerMoney += money;
  23. }
  24.  
  25. else
  26. {
  27. Console.WriteLine($"Cannot accept {money}");
  28. }
  29. commandStart = Console.ReadLine();
  30. if (commandStart == "Start")
  31. {
  32. break;
  33. }
  34. }
  35.  
  36. string commandEnd = Console.ReadLine();
  37. while (commandEnd != "End")
  38. {
  39. string product = commandEnd;
  40. //bool invalidProduct = (product != "Nuts" || product != "Water" ||
  41. //product != "Crisps" || product != "Soda" || product != "Coke");
  42. bool nuts = product == "Nuts";
  43. bool water = product == "Water";
  44. bool crisps = product == "Crisps";
  45. bool soda = product == "Soda";
  46. bool coke = product == "Coke";
  47.  
  48. if (nuts)
  49. {
  50. if (customerMoney < 2.00m)
  51. {
  52. Console.WriteLine(notEnoughMoney);
  53. commandEnd = Console.ReadLine();
  54. continue;
  55. }
  56.  
  57. else
  58. {
  59. Console.WriteLine("Purchased nuts");
  60. customerMoney -= 2.00m;
  61. }
  62.  
  63. }
  64.  
  65. else if (water)
  66. {
  67. if (customerMoney < 0.70m)
  68. {
  69. Console.WriteLine(notEnoughMoney);
  70. commandEnd = Console.ReadLine();
  71. continue;
  72. }
  73.  
  74. else
  75. {
  76. Console.WriteLine("Purchased water");
  77. customerMoney -= 0.70m;
  78. }
  79. }
  80.  
  81. else if (crisps)
  82. {
  83. if (customerMoney < 1.50m)
  84. {
  85. Console.WriteLine(notEnoughMoney);
  86. commandEnd = Console.ReadLine();
  87. continue;
  88. }
  89.  
  90. else
  91. {
  92. Console.WriteLine("Purchased crisps");
  93. customerMoney -= 1.50m;
  94. }
  95. }
  96.  
  97. else if (soda)
  98. {
  99. if (customerMoney < 0.80m)
  100. {
  101. Console.WriteLine(notEnoughMoney);
  102. commandEnd = Console.ReadLine();
  103. continue;
  104. }
  105.  
  106. else
  107. {
  108. Console.WriteLine("Purchased soda");
  109. customerMoney -= 0.80m;
  110. }
  111. }
  112.  
  113. else if (coke)
  114. {
  115. if (customerMoney < 1.00m)
  116. {
  117. Console.WriteLine(notEnoughMoney);
  118. commandEnd = Console.ReadLine();
  119. continue;
  120. }
  121.  
  122. else
  123. {
  124. Console.WriteLine("Purchased coke");
  125. customerMoney -= 1.00m;
  126. }
  127. }
  128.  
  129. else
  130. {
  131. Console.WriteLine("Invalid product");
  132. commandEnd = Console.ReadLine();
  133. continue;
  134. }
  135.  
  136.  
  137.  
  138. commandEnd = Console.ReadLine();
  139. }
  140.  
  141. Console.WriteLine($"Change: {customerMoney}");
  142. }
  143. }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment