lovelyvook

Unit_42

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