VasilKotsev

07. Vending Machine

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