anizko

07. Vending Machine

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