Advertisement
ElenaTodorova

Vending Machine

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