/
Fleksey
/
CoffeeMachine
Обзор
Документация
Войти
/
Fleksey
/
CoffeeMachine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ConsoleApp2/Program.cs
196 строк
6 KB
Andrew Khoryakov
[work]
29 ноя 2025, 16:04
29 ноя 2025, 16:04
dd8381a
Код
Авторство
О чём код?
using System; namespace ConsoleApp2; class Program { static int userMoney = 0; static CoffeeShop shop = new CoffeeShop(); static int coffeeIndex = 0; static int syrupIndex = 0; static void Main() { while (true) { Console.Clear(); Console.WriteLine("=== КОФЕ МАШИНА ==="); Console.WriteLine($"Баланс: {userMoney}"); Console.WriteLine("1. Купить кофе"); Console.WriteLine("2. Пополнить баланс"); Console.WriteLine("3. Выход"); Console.Write("Выбор: "); string choice = Console.ReadLine(); switch (choice) { case "1": BuyCoffee(); break; case "2": RefillBalance(); break; case "3": return; default: Console.WriteLine("Ошибка"); Console.ReadKey(); break; } } } static void BuyCoffee() { //КОФЕ Console.Clear(); Console.WriteLine("=== КОФЕ ==="); for (int i = 0; i < shop.Coffees.Count; i++) { var c = shop.Coffees[i]; Console.WriteLine($"{i + 1}. {c.Name} — {c.Count} — {c.BasePrice}"); } Console.Write("Выбор: "); if (!int.TryParse(Console.ReadLine(), out int coffeeChoice) || coffeeChoice < 1 || coffeeChoice > shop.Coffees.Count) { Console.WriteLine("Ошибка"); Console.ReadKey(); return; } Coffee selectedCoffee = shop.Coffees[coffeeChoice - 1]; if (selectedCoffee.Count <= 0) { Console.WriteLine("Нет в наличии"); Console.ReadKey(); return; } if (userMoney < selectedCoffee.BasePrice) { Console.WriteLine("Недостаточно денег"); Console.ReadKey(); return; } coffeeIndex = coffeeChoice - 1; // selectedCoffee.Count--; Console.WriteLine("Кофе куплен!"); Console.ReadKey(); Console.Clear(); Console.WriteLine("=== СИРОП ==="); for (int i = 0; i < shop.Syrups.Count; i++) { var c = shop.Syrups[i]; Console.WriteLine($"{i + 1}. {c.Name} — {c.Count} — {c.Price}"); } Console.WriteLine("Чтобы не добавлять сироп - жми Enter"); Console.Write("Выбор: "); var input = Console.ReadLine(); switch (input) { case null: case "": syrupIndex = -1; break; default: if (!int.TryParse(input, out int syrupChoice) || syrupChoice < 1 || syrupChoice > shop.Syrups.Count) { Console.WriteLine("Ошибка"); Console.ReadKey(); return; } Syrup selectedSyrup = shop.Syrups[syrupChoice - 1]; if (selectedSyrup.Count <= 0) { Console.WriteLine("Нет в наличии"); Console.ReadKey(); return; } if (userMoney < selectedSyrup.Price) { Console.WriteLine("Недостаточно денег"); Console.ReadKey(); return; } syrupIndex = syrupChoice - 1; // selectedSyrup.Count--; Console.WriteLine("Сироп добавлен!"); Console.ReadKey(); break; } Order(); } static void RefillBalance() { Console.Clear(); Console.Write("Введите сумму: "); if (!int.TryParse(Console.ReadLine(), out int money) || money <= 0) { Console.WriteLine("Ошибка"); Console.ReadKey(); return; } userMoney += money; Console.WriteLine($"Баланс обновлён, на счет {userMoney}"); Console.ReadKey(); } static void Order() { int total = 0; Console.Write("=== ВАШ ЗАКАЗ СОБРАН ==="); Console.WriteLine($"Выбраны следующие позиции:"); Console.WriteLine($"- {shop.Coffees[coffeeIndex].Name}, цена: {shop.Coffees[coffeeIndex].BasePrice}"); if (syrupIndex == -1) { total = shop.Coffees[coffeeIndex].BasePrice; Console.WriteLine($"Стоимость: {total}, на вашем счету: {userMoney}"); } else { Console.WriteLine($"- {shop.Syrups[syrupIndex].Name}, цена: {shop.Syrups[coffeeIndex].Price}"); total = shop.Coffees[coffeeIndex].BasePrice + shop.Syrups[coffeeIndex].Price; Console.WriteLine($"Стоимость: {total}, на вашем счету: {userMoney}"); } if (userMoney >= total) { userMoney -= total; Console.WriteLine("Успешно"); Console.WriteLine($"Сдача составляет: {userMoney}"); Console.ReadKey(); shop.Coffees[coffeeIndex].Count -= 1; if (syrupIndex != -1) { shop.Syrups[syrupIndex].Count -= 1; } } else { Console.WriteLine("Недостаточно средств!"); Console.ReadKey(); } } }