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