using System; using System.Collections.Generic; using System.Drawing; using System.Reflection; internal class Program { static void Main(string[] args) { Shop shop = new Shop(); shop.Work(); } } public class Product { public Product(string name, int price) { Name = name; Price = price; } public string Name { get; private set; } public int Price { get; private set; } public override string ToString() { return $"{Name}. Цена: {Price} руб."; } } public class Person { protected List _products; public Person(int money, string role) { Money = money; Role = role; } public string Role { get; private set; } public int Money { get; protected set; } public void ShowInfo() { Console.WriteLine($"{Role}. Денег: {Money} руб.\n\nТовары: "); if (_products.Count > 0) { foreach (Product product in _products) { Console.WriteLine(product); } } else { Console.WriteLine("Пусто."); } Console.ReadKey(); } } public class Seller : Person { public Seller(int money, string role) : base(money, role) { _products = new List { new Product("Яблоко", 50), new Product("Банан", 40), new Product("Молоко", 80), new Product("Хлеб", 30), new Product("Сыр", 150), new Product("Колбаса", 200), new Product("Шоколад", 100), new Product("Кофе", 300), new Product("Чай", 120), new Product("Яйца", 90) }; } public bool CanSell(out Product product, Buyer buyer) { product = SetProduct(); if (buyer.CanAfford(product)) return true; else return false; } public void AddMoney(int money) { Money += money; } private Product SetProduct() { Random random = new Random(); return _products[random.Next(_products.Count)]; } } public class Buyer : Person { public Buyer(int money, string role) : base(money, role) { _products = new List(); } public void Buy(Product product) { Money -= product.Price; _products.Add(product); } public bool CanAfford(Product product) { return Money >= product.Price; } } public class Shop { private Seller _seller = new Seller(10000, "Продавец"); private Buyer _buyer = new Buyer(1000, "Покупатель"); public void Work() { const string CommandShowInfoSeller = "1"; const string CommandSellInfoBuyer = "2"; const string CommandConductSale = "3"; const string CommandExit = "Exit"; bool isRun = true; while (isRun) { Console.Clear(); Console.WriteLine($"{CommandShowInfoSeller} - Информация о продавце.\n" + $"{CommandSellInfoBuyer} - Информация о покупателе.\n" + $"{CommandConductSale} - Провести продажу.\n" + $"{CommandExit} - Выход."); switch (Console.ReadLine()) { case CommandShowInfoSeller: ShowInfoSeller(); break; case CommandSellInfoBuyer: ShowInfoBuyer(); break; case CommandConductSale: ConductSale(); break; case CommandExit: isRun = false; break; default: Console.WriteLine("Ошибка ввода"); Console.ReadKey(); break; } } Console.WriteLine("Программа завершена."); Console.ReadKey(); } private void ConductSale() { if (_seller.CanSell(out Product product, _buyer)) { _seller.AddMoney(product.Price); _buyer.Buy(product); Console.WriteLine($"{_buyer.Role} купил {product.Name}."); Console.ReadKey(); } else { Console.WriteLine($"У {_buyer.Role} не достаточно денег."); Console.ReadKey(); } } private void ShowInfoSeller() { _seller.ShowInfo(); } private void ShowInfoBuyer() { _buyer.ShowInfo(); } }