Advertisement
Guest User

Vending Mchine

a guest
Sep 23rd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | None | 0 0
  1. using System;
  2.  
  3. namespace vendingMachine
  4. {
  5.     class Program
  6.     {
  7.         static void Main()
  8.         {
  9.             string money = Console.ReadLine();
  10.             double sum = 0.0;
  11.             while (money != "Start")
  12.             {
  13.                 double coin = double.Parse(money);
  14.                 switch (coin)
  15.                 {
  16.                     case 0.1:
  17.                     case 0.2:
  18.                     case 0.5:
  19.                     case 1:
  20.                     case 2:
  21.                         sum += coin;
  22.                         break;
  23.                     default:
  24.                         Console.WriteLine($"Cannot accept {coin}");
  25.                         break;
  26.                 }
  27.                 money = Console.ReadLine();
  28.             }
  29.             string product = Console.ReadLine();
  30.             double productPrice = 0;
  31.             bool isValid = true;
  32.             while (product != "End")
  33.             {
  34.                 switch (product)
  35.                 {
  36.                     case "Nuts": productPrice = 2;
  37.                         break;
  38.                     case "Water": productPrice = 0.7;
  39.                         break;
  40.                     case "Crisps": productPrice = 1.5;
  41.                         break;
  42.                     case "Soda": productPrice = 0.8;
  43.                         break;
  44.                     case "Coke": productPrice = 1;
  45.                         break;
  46.                     default: Console.WriteLine("Invalid product");
  47.                         isValid = false;
  48.                         break;
  49.                 }
  50.                 if (sum >= productPrice && isValid == true)
  51.                 {
  52.                     Console.WriteLine($"Purchased {product.ToLower()}");
  53.                     sum -= productPrice;
  54.                 }
  55.                 else if (isValid == true)
  56.                 {
  57.                     Console.WriteLine($"Sorry, not enough money");
  58.                 }
  59.                 product = Console.ReadLine();
  60.  
  61.             }
  62.             Console.WriteLine($"Change: {sum:f2}");
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement