svephoto

Vending Machine [C#]

Aug 9th, 2021
1,203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. using System;
  2.  
  3. namespace VendingMachine
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string command = Console.ReadLine();
  10.             double sum = 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 | coins == 2)
  17.                 {
  18.                     sum += coins;
  19.                 }
  20.                 else
  21.                 {
  22.                     Console.WriteLine($"Cannot accept {coins}");
  23.                 }
  24.              
  25.                 command = Console.ReadLine();
  26.             }
  27.          
  28.             command = Console.ReadLine();
  29.  
  30.             while (command != "End")
  31.             {                
  32.                 double productPrice = 0;
  33.  
  34.                 if (command == "Nuts")
  35.                 {
  36.                     productPrice = 2;
  37.                 }
  38.                 else if (command == "Water")
  39.                 {
  40.                     productPrice = 0.7;
  41.                 }
  42.                 else if (command == "Crisps")
  43.                 {
  44.                     productPrice = 1.5;
  45.                 }
  46.                 else if (command == "Soda")
  47.                 {
  48.                     productPrice = 0.8;
  49.                 }
  50.                 else if (command == "Coke")
  51.                 {
  52.                     productPrice = 1.0;
  53.                 }
  54.                 else
  55.                 {
  56.                     Console.WriteLine("Invalid product");
  57.                     command = Console.ReadLine();
  58.                     continue;
  59.                 }
  60.  
  61.                 if (sum >= productPrice)
  62.                 {
  63.                     sum -= productPrice;
  64.                     Console.WriteLine($"Purchased {command.ToLower()}");
  65.                 }
  66.                 else
  67.                 {
  68.                     Console.WriteLine("Sorry, not enough money");
  69.                 }              
  70.                            
  71.                 command = Console.ReadLine();
  72.             }
  73.          
  74.             Console.WriteLine($"Change: {sum:f2}");
  75.         }
  76.     }
  77. }
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment