Advertisement
TeT91

ДЗ Магазин

May 31st, 2024 (edited)
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSLight
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Shop shop = new Shop(new Buyer());
  11.             shop.Trade();
  12.         }
  13.     }
  14.  
  15.     class Shop
  16.     {
  17.         private Seller _seller = new Seller();
  18.         private Buyer _buyer;
  19.  
  20.         public Shop(Buyer buyer)
  21.         {
  22.             _buyer = buyer;
  23.         }
  24.  
  25.         public void Trade()
  26.         {
  27.             const string CommandEndTrade = "Да";
  28.  
  29.             bool isTrading = true;
  30.  
  31.             while (isTrading)
  32.             {
  33.                 ShowSellerInventory();
  34.                 TryExchangeProduct();
  35.                 ShowBuyerInventory();
  36.  
  37.                 Console.WriteLine("Закончить торговлю?");
  38.  
  39.                 string userInput = Console.ReadLine();
  40.  
  41.                 if(userInput == CommandEndTrade)
  42.                 {
  43.                     isTrading = false;
  44.                 }
  45.             }
  46.  
  47.             Console.ReadKey();
  48.         }
  49.  
  50.         public void ShowSellerInventory()
  51.         {
  52.             Console.WriteLine("Товары на прилавке:");
  53.  
  54.             _seller.ShowProducts();
  55.         }
  56.  
  57.         public void ShowBuyerInventory()
  58.         {
  59.             Console.WriteLine("У покупателя " + _buyer.Money + " денег");
  60.             Console.WriteLine("Инвентарь покупателя:");
  61.  
  62.             _buyer.ShowProducts();
  63.         }
  64.  
  65.         public void TryExchangeProduct()
  66.         {
  67.             Console.WriteLine("Выберите продукт для покупки");
  68.  
  69.             if (int.TryParse(Console.ReadLine(), out int id) && id < _seller.GetProductsCount())
  70.             {
  71.                 Product product = _seller.GetProduct(id);
  72.                 int price = product.Price;
  73.  
  74.                 if (_buyer.TryBuyProduct(product))
  75.                 {
  76.                     _buyer.Buy(product);
  77.                     _seller.SellProduct(product);
  78.                     _buyer.DecreaseMoney(price);
  79.                     Console.WriteLine("Покупка завершена");
  80.                 }
  81.                 else
  82.                 {
  83.                     Console.WriteLine("У покупателя не хватает денег");
  84.                 }
  85.             }
  86.         }
  87.     }
  88.  
  89.  
  90.     class Buyer : Person
  91.     {
  92.         public Buyer()
  93.         {
  94.             int startingMoney = 50;
  95.             IncreaseMoney(startingMoney);
  96.         }
  97.  
  98.         public bool TryBuyProduct(Product product)
  99.         {
  100.             return product.Price <= Money;
  101.         }
  102.  
  103.         public void Buy(Product product)
  104.         {
  105.             Bag.AddProduct(product);
  106.             DecreaseMoney(product.Price);
  107.         }
  108.     }
  109.  
  110.     class Seller : Person
  111.     {
  112.         public Seller()
  113.         {
  114.             int fruitsPrice = 10;
  115.             int breadPrice = 5;
  116.             int vegetablePrice = 10;
  117.             int meatPrice = 30;
  118.  
  119.             Bag.AddProduct(new Product("Яблоки", fruitsPrice));
  120.             Bag.AddProduct(new Product("Черешня", fruitsPrice));
  121.             Bag.AddProduct(new Product("Апельсин", fruitsPrice));
  122.             Bag.AddProduct(new Product("Хлеб", breadPrice));
  123.             Bag.AddProduct(new Product("Булочка", breadPrice));
  124.             Bag.AddProduct(new Product("Пирожок", breadPrice));
  125.             Bag.AddProduct(new Product("Помидоры", vegetablePrice));
  126.             Bag.AddProduct(new Product("Огурцы", vegetablePrice));
  127.             Bag.AddProduct(new Product("Мясо", meatPrice));
  128.  
  129.             Console.WriteLine("На прилавке" + Bag.ProductsCount);
  130.         }
  131.  
  132.         public void SellProduct(Product product)
  133.         {
  134.             Bag.RemoveProduct(product);
  135.             IncreaseMoney(product.Price);
  136.         }
  137.     }
  138.  
  139.     class Person
  140.     {
  141.         protected Inventory Bag = new Inventory();
  142.  
  143.         public int Money { get; private set; }
  144.  
  145.         public int GetProductsCount()
  146.         {
  147.             return Bag.ProductsCount;
  148.         }
  149.  
  150.         public void ShowProducts()
  151.         {
  152.             for (int i = 0; i < Bag.ProductsCount; i++)
  153.             {
  154.                 Console.WriteLine($"{i} - {Bag.GetProduct(i).Name} - {Bag.GetProduct(i).Price}");
  155.             }
  156.         }
  157.  
  158.         public void IncreaseMoney(int value)
  159.         {
  160.             if (value > 0)
  161.             {
  162.                 Money += value;
  163.             }
  164.         }
  165.  
  166.         public void DecreaseMoney(int value)
  167.         {
  168.             if (value > 0)
  169.             {
  170.                 Money -= value;
  171.  
  172.                 if (Money < 0)
  173.                 {
  174.                     Money = 0;
  175.                 }
  176.             }
  177.         }
  178.  
  179.         public Product GetProduct(int id)
  180.         {
  181.             return Bag.GetProduct(id);
  182.         }
  183.     }
  184.  
  185.     class Inventory
  186.     {
  187.         private List<Product> _products = new List<Product>();
  188.  
  189.         public int ProductsCount
  190.         {
  191.             get { return _products.Count; }
  192.         }
  193.  
  194.         public void AddProduct(Product product)
  195.         {
  196.             _products.Add(product);
  197.         }
  198.  
  199.         public void RemoveProduct(Product product)
  200.         {
  201.             _products.Remove(product);
  202.         }
  203.  
  204.         public Product GetProduct(int id)
  205.         {
  206.             return _products[id];
  207.         }
  208.     }
  209.  
  210.     class Product
  211.     {
  212.         public Product(string name, int price)
  213.         {
  214.             Name = name;
  215.             Price = price;
  216.         }
  217.  
  218.         public string Name { get; private set; }
  219.         public int Price { get; private set; }
  220.     }
  221. }
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement