silvana1303

vending machine

Jun 19th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using System;
  2.  
  3. namespace whileloop
  4. {
  5.     class Program
  6.     {
  7.         static void Main()
  8.         {
  9.             string command = Console.ReadLine();
  10.             double coinsSum = 0;
  11.  
  12.             while (command != "Start")
  13.             {
  14.                 double coins = double.Parse(command);
  15.  
  16.                 if (coins != 0.1 && coins != 0.2 && coins != 0.5 && coins != 1.0 && coins != 2.0)
  17.                 {
  18.                     Console.WriteLine($"Cannot accept {coins}");
  19.                 }
  20.                 else
  21.                 {
  22.                     coinsSum += coins;
  23.                 }
  24.  
  25.                
  26.  
  27.                 command = Console.ReadLine();
  28.             }
  29.  
  30.             string input = Console.ReadLine();
  31.             double price = 0;
  32.  
  33.             while (input != "End")
  34.             {
  35.                 if (input == "Nuts")
  36.                 {
  37.                     price = 2.0;
  38.  
  39.                     if (price <= coinsSum)
  40.                     {
  41.                         Console.WriteLine("Purchased nuts");
  42.                         coinsSum -= price;
  43.                     }
  44.                     else
  45.                     {
  46.                         Console.WriteLine("Sorry, not enough money.");
  47.                     }
  48.                    
  49.                 }
  50.                 else if (input == "Water")
  51.                 {
  52.                     price = 0.7;
  53.  
  54.                     if (price <= coinsSum)
  55.                     {
  56.                         Console.WriteLine("Purchased water");
  57.                         coinsSum -= price;
  58.                     }
  59.                     else
  60.                     {
  61.                         Console.WriteLine("Sorry, not enough money.");
  62.                     }
  63.  
  64.                 }
  65.                 else if (input == "Crisps")
  66.                 {
  67.                     price = 1.5;
  68.  
  69.                     if (price <= coinsSum)
  70.                     {
  71.                         Console.WriteLine("Purchased crisps");
  72.                         coinsSum -= price;
  73.                     }
  74.                     else
  75.                     {
  76.                         Console.WriteLine("Sorry, not enough money.");
  77.                      
  78.                     }
  79.  
  80.                 }
  81.                 else if (input == "Soda")
  82.                 {
  83.                     price = 0.8;
  84.  
  85.                     if (price <= coinsSum)
  86.                     {
  87.                         Console.WriteLine("Purchased soda");
  88.                         coinsSum -= price;
  89.                     }
  90.                     else
  91.                     {
  92.                         Console.WriteLine("Sorry, not enough money.");
  93.    
  94.                     }
  95.  
  96.                 }
  97.                 else if (input == "Coke")
  98.                 {
  99.                     price = 1.0;
  100.  
  101.                     if (price <= coinsSum)
  102.                     {
  103.                         Console.WriteLine("Purchased coke");
  104.                         coinsSum -= price;
  105.                     }
  106.                     else
  107.                     {
  108.                         Console.WriteLine("Sorry, not enough money.");
  109.                     }
  110.  
  111.                 }
  112.                 else
  113.                 {
  114.                     Console.WriteLine("Invalid product");
  115.                 }
  116.  
  117.                 input = Console.ReadLine();
  118.             }
  119.  
  120.             Console.WriteLine($"Change: {coinsSum:f2}");
  121.         }
  122.     }
  123. }
Add Comment
Please, Sign In to add comment