NikaBang

ДЗ: Магазин

Nov 24th, 2025 (edited)
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.60 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Reflection;
  5.  
  6. internal class Program
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.         Shop shop = new Shop();
  11.  
  12.         shop.Work();
  13.     }
  14. }
  15.  
  16. public class Product
  17. {
  18.     public Product(string name, int price)
  19.     {
  20.         Name = name;
  21.         Price = price;
  22.     }
  23.  
  24.     public string Name { get; private set; }
  25.     public int Price { get; private set; }
  26.  
  27.     public override string ToString()
  28.     {
  29.         return $"{Name}. Цена: {Price} руб.";
  30.     }
  31. }
  32.  
  33. public class Person
  34. {
  35.     protected List<Product> Products;
  36.  
  37.     public Person(int money, string role)
  38.     {
  39.         Money = money;
  40.         Role = role;
  41.     }
  42.  
  43.     public string Role { get; private set; }
  44.     public int Money { get; protected set; }
  45.  
  46.     public void ShowInfo()
  47.     {
  48.         Console.WriteLine($"{Role}. Денег: {Money} руб.");
  49.  
  50.         ShowProducts();
  51.  
  52.         Console.ReadKey();
  53.     }
  54.  
  55.     public void ShowProducts()
  56.     {
  57.         Console.WriteLine("Товары: ");
  58.  
  59.         if (Products.Count > 0)
  60.         {
  61.             for (int i = 0; i < Products.Count; i++)
  62.             {
  63.                 Console.WriteLine(i + 1 + ". " + Products[i]);
  64.             }
  65.         }
  66.         else
  67.         {
  68.             Console.WriteLine("Пусто.");
  69.         }
  70.     }
  71. }
  72.  
  73. public class Seller : Person
  74. {
  75.     public Seller(int money, string role) : base(money, role)
  76.     {
  77.         Products = new List<Product>
  78.         {
  79.             new Product("Яблоко", 50),
  80.             new Product("Банан", 40),
  81.             new Product("Молоко", 80),
  82.             new Product("Хлеб", 30),
  83.             new Product("Сыр", 150),
  84.             new Product("Колбаса", 200),
  85.             new Product("Шоколад", 100),
  86.             new Product("Кофе", 300),
  87.             new Product("Чай", 120),
  88.             new Product("Яйца", 90)
  89.         };
  90.     }
  91.  
  92.     public bool TryGetProduct(out Product product)
  93.     {
  94.         product = SetProduct();
  95.  
  96.         if (product != null)
  97.             return true;
  98.         else return false;
  99.     }
  100.  
  101.     public void AddMoney(int money)
  102.     {
  103.         Money += money;
  104.     }
  105.  
  106.     private Product SetProduct()
  107.     {
  108.         ShowProducts();
  109.  
  110.         Console.Write("\nВыбере товар: ");
  111.  
  112.         int ProductNumber = ReadInt();
  113.  
  114.         if (ProductNumber > 0 && ProductNumber < Products.Count)
  115.         {
  116.             return Products[ProductNumber - 1];
  117.         }
  118.  
  119.         return null;
  120.     }
  121.  
  122.     private int ReadInt()
  123.     {
  124.         int number;
  125.  
  126.         while (int.TryParse(Console.ReadLine(), out number) == false)
  127.         {
  128.             Console.Write("Ошибка ввода. Повторите: ");
  129.         }
  130.  
  131.         return number;
  132.     }
  133. }
  134.  
  135. public class Buyer : Person
  136. {
  137.     public Buyer(int money, string role) : base(money, role)
  138.     {
  139.         Products = new List<Product>();
  140.     }
  141.  
  142.     public void Buy(Product product)
  143.     {
  144.         Money -= product.Price;
  145.  
  146.         Products.Add(product);
  147.     }
  148.  
  149.     public bool CanAfford(Product product)
  150.     {
  151.         return Money >= product.Price;
  152.     }
  153. }
  154.  
  155. public class Shop
  156. {
  157.     private Seller _seller = new Seller(10000, "Продавец");
  158.     private Buyer _buyer = new Buyer(1000, "Покупатель");
  159.  
  160.     public void Work()
  161.     {
  162.         const string CommandSellInfoBuyer = "1";
  163.         const string CommandSellInfoSeller = "2";
  164.         const string CommandConductSale = "3";
  165.         const string CommandExit = "Exit";
  166.  
  167.         bool isRun = true;
  168.  
  169.         while (isRun)
  170.         {
  171.             Console.Clear();
  172.             Console.WriteLine($"{CommandSellInfoBuyer} - Информация о покупателе.\n" +
  173.             $"{CommandSellInfoSeller} - Информация о продавце.\n" +
  174.             $"{CommandConductSale} - Провести продажу.\n" +
  175.             $"{CommandExit} - Выход.");
  176.  
  177.             switch (Console.ReadLine())
  178.             {
  179.                 case CommandSellInfoBuyer:
  180.                     ShowInfoBuyer();
  181.                     break;
  182.  
  183.                 case CommandSellInfoSeller:
  184.                     ShowInfoSeller();
  185.                     break;
  186.  
  187.                 case CommandConductSale:
  188.                     ConductSale();
  189.                     break;
  190.  
  191.                 case CommandExit:
  192.                     isRun = false;
  193.                     break;
  194.  
  195.                 default:
  196.                     Console.WriteLine("Ошибка ввода");
  197.                     Console.ReadKey();
  198.                     break;
  199.             }
  200.         }
  201.  
  202.         Console.WriteLine("Программа завершена.");
  203.         Console.ReadKey();
  204.     }
  205.  
  206.     private void ConductSale()
  207.     {
  208.         if (_seller.TryGetProduct(out Product product))
  209.         {
  210.             if (_buyer.CanAfford(product))
  211.             {
  212.                 _seller.AddMoney(product.Price);
  213.                 _buyer.Buy(product);
  214.                 Console.WriteLine($"{_buyer.Role} купил {product.Name}.");
  215.                 Console.ReadKey();
  216.             }
  217.             else
  218.             {
  219.                 Console.WriteLine($"У {_buyer.Role} не достаточно денег.");
  220.                 Console.ReadKey();
  221.             }
  222.         }
  223.         else
  224.         {
  225.             Console.WriteLine("Нет товара под этим номером.");
  226.             Console.ReadKey();
  227.         }
  228.     }
  229.  
  230.     private void ShowInfoSeller()
  231.     {
  232.         _seller.ShowInfo();
  233.     }
  234.  
  235.     private void ShowInfoBuyer()
  236.     {
  237.         _buyer.ShowInfo();
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment