Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace vendingMachine
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input;
- double coinsInserted;
- bool coins;
- double sumCoins = 0.0;
- double costSum = 0.0;
- double change = 0.0;
- do
- {
- input = Console.ReadLine();
- coins = double.TryParse(input, out coinsInserted);
- if (coins)
- {
- switch (coinsInserted)
- {
- case 0.1:
- case 0.2:
- case 0.5:
- case 1:
- case 2:
- break;
- default:
- Console.WriteLine("Cannot accept {0}", coinsInserted);
- continue;
- }
- sumCoins += coinsInserted;
- }
- } while (coins);
- if (input == "Start")
- {
- bool end = false;
- do
- {
- input = Console.ReadLine();
- double productPrice = 0.0;
- switch (input)
- {
- case "Nuts":
- productPrice = 2.0;
- break;
- case "Water":
- productPrice = 0.7;
- break;
- case "Crisps":
- productPrice = 1.5;
- break;
- case "Soda":
- productPrice = 0.8;
- break;
- case "Coke":
- productPrice = 1.0;
- break;
- case "End":
- end = true;
- break;
- default:
- Console.WriteLine("Invalid product");
- continue;
- }
- if (end)
- {
- continue;
- }
- costSum += productPrice;
- if (costSum > sumCoins)
- {
- Console.WriteLine("Sorry, not enough money");
- costSum -= productPrice;
- continue;
- }
- Console.WriteLine($"Purchased {input.ToLower()}");
- } while (!end);
- change = sumCoins - costSum;
- Console.WriteLine($"Change: {change:F2}");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment