Radost09

Vending Machine

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