Advertisement
Tervel

Vending Machine

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