Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace vendingMachine
- {
- class Program
- {
- static void Main()
- {
- string money = Console.ReadLine();
- double sum = 0.0;
- while (money != "Start")
- {
- double coin = double.Parse(money);
- switch (coin)
- {
- case 0.1:
- case 0.2:
- case 0.5:
- case 1:
- case 2:
- sum += coin;
- break;
- default:
- Console.WriteLine($"Cannot accept {coin}");
- break;
- }
- money = Console.ReadLine();
- }
- string product = Console.ReadLine();
- double productPrice = 0;
- bool isValid = true;
- while (product != "End")
- {
- switch (product)
- {
- case "Nuts": productPrice = 2;
- break;
- case "Water": productPrice = 0.7;
- break;
- case "Crisps": productPrice = 1.5;
- break;
- case "Soda": productPrice = 0.8;
- break;
- case "Coke": productPrice = 1;
- break;
- default: Console.WriteLine("Invalid product");
- isValid = false;
- break;
- }
- if (sum >= productPrice && isValid == true)
- {
- Console.WriteLine($"Purchased {product.ToLower()}");
- sum -= productPrice;
- }
- else if (isValid == true)
- {
- Console.WriteLine($"Sorry, not enough money");
- }
- product = Console.ReadLine();
- }
- Console.WriteLine($"Change: {sum:f2}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement