Dr_Max_Experience

Task 9

May 10th, 2022 (edited)
1,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ООП
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Supermarket supermarket = new Supermarket();
  14.             supermarket.Work();
  15.         }
  16.     }
  17.  
  18.     class Supermarket
  19.     {
  20.         private int _money;
  21.         private Queue<Buyer> _buyers = new Queue<Buyer>();
  22.  
  23.         public Supermarket()
  24.         {
  25.             int countBuyers = 10;
  26.  
  27.             for (int i = 0; i < countBuyers; i++)
  28.             {
  29.                 _buyers.Enqueue(new Buyer());
  30.             }
  31.         }
  32.  
  33.         public void Work()
  34.         {
  35.             while(_buyers.Count > 0)
  36.             {
  37.                 ShowInfo();
  38.                 _money += _buyers.Dequeue().BuyProducts();
  39.  
  40.                 Console.ReadKey();
  41.             }
  42.  
  43.             Console.Write($"Магазин заработал за сегодня = {_money}$");
  44.         }
  45.  
  46.         private void ShowInfo()
  47.         {
  48.             Console.Clear();
  49.             Console.WriteLine($"Итого магазин заработал = {_money}$\nКлиентов в очереди: {_buyers.Count}\nНажмите любую клавишу, чтобы обслужить клиента");
  50.             Console.ReadKey();
  51.         }
  52.     }
  53.  
  54.     class Buyer
  55.     {
  56.         private int _money;
  57.         private List<Product> _cart = new List<Product>();
  58.  
  59.         public Buyer()
  60.         {
  61.             Random random = new Random();
  62.             int minMoney = 400;
  63.             int maxMoney = 1500;
  64.             _money = random.Next(minMoney, maxMoney);
  65.  
  66.             int minCountProducts = 4;
  67.             int maxCountProducts = 12;
  68.             int countProducts = random.Next(minCountProducts, maxCountProducts);
  69.  
  70.             for (int i = 0; i < countProducts; i++)
  71.             {
  72.                 _cart.Add(new Product());
  73.             }
  74.         }
  75.  
  76.         public int BuyProducts()
  77.         {
  78.             bool isBuy = true;
  79.  
  80.             while (isBuy)
  81.             {
  82.                 Console.Clear();
  83.                 Console.WriteLine($"У клиента = {_money}$\nУ клиента: {_cart.Count} штук товара, на сумму {Sum()}$");
  84.                 Console.ReadKey();
  85.  
  86.                 if (_money >= Sum())
  87.                 {
  88.                     Console.WriteLine($"У покупателя достаточно денег, магазин заработал {Sum()}$");
  89.                     return Sum();
  90.                 }
  91.                 else
  92.                 {
  93.                     Console.WriteLine("У покупателя недостаточно денег, 1 товар был убран их корзины!");
  94.                     RemoveProduct();
  95.                 }
  96.  
  97.                 Console.ReadKey();
  98.             }
  99.  
  100.             return 0;
  101.         }
  102.  
  103.         private void RemoveProduct()
  104.         {
  105.             Random random = new Random();
  106.             int randomIndex = random.Next(0, _cart.Count);
  107.             _cart.RemoveAt(randomIndex);
  108.         }
  109.  
  110.         private int Sum()
  111.         {
  112.             int sum = 0;
  113.            
  114.             for (int i = 0; i < _cart.Count; i++)
  115.             {
  116.                 sum += _cart[i].Price;
  117.             }
  118.  
  119.             return sum;
  120.         }
  121.     }
  122.    
  123.     class Product
  124.     {
  125.         public int Price { get; private set; }
  126.  
  127.         public Product()
  128.         {
  129.             Random random = new Random();
  130.             int minPrise = 50;
  131.             int maxPrise = 201;
  132.             Price = random.Next(minPrise, maxPrise);
  133.         }
  134.     }
  135. }
Add Comment
Please, Sign In to add comment