Advertisement
dragonbs

Vending Machine

Feb 2nd, 2023
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. decimal balance = 0;
  2.  
  3. while (true)
  4. {
  5.     string input = Console.ReadLine();
  6.     if (input == "Start")
  7.     {
  8.         break;
  9.     }
  10.     decimal coin = decimal.Parse(input);
  11.     if (coin == 0.1m || coin == 0.2m || coin == 0.5m || coin == 1m || coin == 2m)
  12.     {
  13.         balance += coin;
  14.     }
  15.     else
  16.     {
  17.         Console.WriteLine($"Cannot accept {coin}");
  18.     }
  19. }
  20.  
  21. while (true)
  22. {
  23.     string input = Console.ReadLine();
  24.     if (input == "End")
  25.     {
  26.         break;
  27.     }
  28.     if (input == "Nuts")
  29.     {
  30.         if (balance >= 2)
  31.         {
  32.             balance -= 2;
  33.             Console.WriteLine("Purchased nuts");
  34.         }
  35.         else
  36.         {
  37.             Console.WriteLine("Sorry, not enough money");
  38.         }
  39.     }
  40.     else if (input == "Water")
  41.     {
  42.         if (balance >= 0.7m)
  43.         {
  44.             balance -= 0.7m;
  45.             Console.WriteLine("Purchased water");
  46.         }
  47.         else
  48.         {
  49.             Console.WriteLine("Sorry, not enough money");
  50.         }
  51.     }
  52.     else if (input == "Crisps")
  53.     {
  54.         if (balance >= 1.5m)
  55.         {
  56.             balance -= 1.5m;
  57.             Console.WriteLine("Purchased crisps");
  58.         }
  59.         else
  60.         {
  61.             Console.WriteLine("Sorry, not enough money");
  62.         }
  63.     }
  64.     else if (input == "Soda")
  65.     {
  66.         if (balance >= 0.8m)
  67.         {
  68.             balance -= 0.8m;
  69.             Console.WriteLine("Purchased soda");
  70.         }
  71.         else
  72.         {
  73.             Console.WriteLine("Sorry, not enough money");
  74.         }
  75.     }
  76.     else if (input == "Coke")
  77.     {
  78.         if (balance >= 1m)
  79.         {
  80.             balance -= 1m;
  81.             Console.WriteLine("Purchased coke");
  82.         }
  83.         else
  84.         {
  85.             Console.WriteLine("Sorry, not enough money");
  86.         }
  87.     }
  88.     else
  89.     {
  90.         Console.WriteLine("Invalid product");
  91.     }
  92.  
  93. }
  94. Console.WriteLine($"Change: {balance:F2} ");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement