Advertisement
Zaksofon

Untitled

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