Advertisement
Guest User

Vending_Machine

a guest
Jan 21st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2.  
  3. namespace VendingMachine
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             double totalPrice = 0;
  10.             string moneyInput = Console.ReadLine();
  11.  
  12.             while (moneyInput != "Start")
  13.             {
  14.                 double moneyInputValue = double.Parse(moneyInput);
  15.                 if (moneyInput == "0,1" || moneyInput == "0,2" || moneyInput == "0,5" || moneyInput == "1" || moneyInput == "2")
  16.                 {
  17.                     totalPrice += moneyInputValue;
  18.                 }
  19.                 else
  20.                 {
  21.                     Console.WriteLine($"Cannot accept {moneyInputValue}");
  22.                 }
  23.                 moneyInput = Console.ReadLine();
  24.             }
  25.  
  26.             string product = Console.ReadLine();
  27.             double productPrice = 0;
  28.            
  29.             while (product != "End")
  30.             {
  31.                 bool verify = false;
  32.                 string purchased = "";
  33.                
  34.                 if (product == "Nuts")
  35.                 {
  36.                     productPrice = 2;
  37.                     purchased = "Purchased nuts";
  38.                 }
  39.                 else if (product == "Water")
  40.                 {
  41.                     productPrice = 0.7;
  42.                     purchased = "Purchased water";
  43.                 }
  44.                 else if (product == "Crisps")
  45.                 {
  46.                     productPrice = 1.5;
  47.                     purchased = "Purchased crisps";
  48.                 }
  49.                 else if (product == "Soda")
  50.                 {
  51.                     productPrice = 0.8;
  52.                     purchased = "Purchased soda";
  53.                 }
  54.                 else if (product == "Coke")
  55.                 {
  56.                     productPrice = 1;
  57.                     purchased = "Purchased coke";
  58.                 }
  59.                 else
  60.                 {
  61.                     Console.WriteLine("Invalid product");
  62.                     verify = true;
  63.                     product = Console.ReadLine();
  64.                 }
  65.  
  66.                 if (verify == false)
  67.                 {
  68.                     if (totalPrice >= productPrice)
  69.                     {
  70.                         Console.WriteLine(purchased);
  71.                         totalPrice -= productPrice;
  72.                         product = Console.ReadLine();
  73.                     }
  74.                     else
  75.                     {
  76.                         Console.WriteLine("Sorry, not enough money");
  77.                         product = Console.ReadLine();
  78.                     }
  79.                 }
  80.             }
  81.             Console.WriteLine($"Change: {totalPrice:F2}");
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement