Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _07._Vending_Machine
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- double coins = 0;
- double currentCoin = 0;
- double price = 0;
- double amount = 0;
- while (input != "Start")
- {
- coins = double.Parse(input);
- if (coins % 0.1 == 0 || coins % 0.2 == 0 || coins % 0.5 == 0 || coins % 1 == 0 || coins % 2 == 0)
- {
- amount += coins;
- }
- else
- {
- Console.WriteLine($"Cannot accept {coins}");
- }
- currentCoin = coins;
- input = Console.ReadLine();
- if (input == "Start")
- {
- string product = Console.ReadLine();
- double currentPrice = 0;
- while (product != "End")
- {
- switch (product)
- {
- case "Nuts":
- price = 2.0;
- amount -= price;
- currentPrice = price;
- break;
- case "Water":
- price = 0.7;
- amount -= price;
- currentPrice = price;
- break;
- case "Crisps":
- price = 1.5;
- amount -= price;
- currentPrice = price;
- break;
- case "Soda":
- price = 0.8;
- amount -= price;
- currentPrice = price;
- break;
- case "Coke":
- price = 1.0;
- amount -= price;
- currentPrice = price;
- break;
- default:
- Console.WriteLine("Invalid product");
- product = Console.ReadLine();
- break;
- }
- if (amount >= 0 && (amount % currentPrice == 0))
- {
- Console.WriteLine($"Purchased {product.ToLower()}");
- product = Console.ReadLine();
- }
- if (amount < 0)
- {
- Console.WriteLine("Sorry, not enough money");
- amount += currentPrice;
- if (amount >= 0.7)
- {
- product = Console.ReadLine();
- }
- }
- }
- Console.WriteLine($"Change: {amount:F2}");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement