Advertisement
Guest User

Homework 7.Vending machine

a guest
Jan 22nd, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. using System;
  2.  
  3. namespace vending_machine
  4. {
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. string money = string.Empty;
  10. double moneySum = 0;
  11. do
  12. {
  13. money = Console.ReadLine();
  14. if (money != "Start")
  15. {
  16. if(money=="0.1"||money=="0.2"||money=="0.5"||money=="1"||money=="2")
  17. moneySum += double.Parse(money);
  18. else Console.WriteLine("Cannot accept {0}",money);
  19. }
  20. } while (money!="Start");
  21.  
  22. string choice = string.Empty;
  23. do
  24. {
  25. choice = Console.ReadLine();
  26. if (choice != "End")
  27. {
  28. switch (choice)
  29. {
  30. case "Nuts":
  31. {
  32. moneySum -= 2;
  33. if (moneySum < 0)
  34. {
  35. Console.WriteLine("Sorry, not enough money");
  36. moneySum += 2;
  37. }
  38. else Console.WriteLine("Purchased {0}", choice.ToLower());
  39. }
  40. break;
  41. case "Water":
  42. {
  43. moneySum -= 0.7;
  44. if (moneySum < 0)
  45. {
  46. Console.WriteLine("Sorry, not enough money");
  47. moneySum += 0.7;
  48. }
  49. else Console.WriteLine("Purchased {0}", choice.ToLower());
  50. }
  51. break;
  52. case "Crisps":
  53. {
  54. moneySum -= 1.5;
  55. if (moneySum < 0)
  56. {
  57. Console.WriteLine("Sorry, not enough money");
  58. moneySum += 1.5;
  59. }
  60. else Console.WriteLine("Purchased {0}", choice.ToLower());
  61. }
  62. break;
  63. case "Soda":
  64. {
  65. moneySum -= 0.8;
  66. if (moneySum < 0)
  67. {
  68. Console.WriteLine("Sorry, not enough money");
  69. moneySum += 0.8;
  70. }
  71. else Console.WriteLine("Purchased {0}", choice.ToLower());
  72. }
  73. break;
  74. case "Coke":
  75. {
  76. moneySum -= 1;
  77. if (moneySum < 0)
  78. {
  79. Console.WriteLine("Sorry, not enough money");
  80. moneySum += 1;
  81. }
  82. else Console.WriteLine("Purchased {0}", choice.ToLower());
  83. }
  84. break;
  85. default:
  86. Console.WriteLine("Invalid product");
  87. break;
  88. }
  89.  
  90. }
  91. } while (choice!="End");
  92. Console.WriteLine("Change: {0:F2}",moneySum);
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement