Advertisement
Wolfscot

07.Vending Machine

Jan 20th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _7VendingMachine
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. string money = Console.ReadLine();
  10. decimal totalSum = 0;
  11.  
  12. while (money != "Start")
  13. {
  14. switch (money)
  15. {
  16. case "0.1":
  17. case "0.2":
  18. case "0.5":
  19. case "1":
  20. case "2":
  21. totalSum += decimal.Parse(money);
  22. break;
  23.  
  24. default:
  25. Console.WriteLine($"Cannot accept {money}");
  26. break;
  27. }
  28. money = Console.ReadLine();
  29. }
  30. string product = Console.ReadLine();
  31. decimal productPrice = 0;
  32.  
  33. while (product != "End")
  34. {
  35. switch (product)
  36. {
  37. case "Nuts":
  38. productPrice = 2.0m;
  39. break;
  40. case "Water":
  41. productPrice = 0.7m;
  42. break;
  43. case "Crisps":
  44. productPrice = 1.5m;
  45. break;
  46. case "Soda":
  47. productPrice = 0.8m;
  48. break;
  49. case "Coke":
  50. productPrice = 1.0m;
  51. break;
  52. default:
  53. Console.WriteLine("Invalid product");
  54. break;
  55. }
  56. if (totalSum >=productPrice && totalSum >0 && productPrice >0)
  57. {
  58. Console.WriteLine($"Purchased {product.ToLower()}");
  59. totalSum = totalSum - productPrice;
  60. productPrice = 0;
  61. }
  62. else if (productPrice >0)
  63. {
  64. Console.WriteLine("Sorry, not enough money");
  65. productPrice = 0;
  66. }
  67. product = Console.ReadLine();
  68. }
  69. Console.WriteLine($"Change: {totalSum:F2}");
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement