Advertisement
Rodunskiy

Untitled

Jul 29th, 2023
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using System.Security.Cryptography;
  5. using System.Security.Policy;
  6.  
  7. class Program
  8. {
  9.     static void Main(string[] args)
  10.     {
  11.         Supermarket supermarket = new Supermarket();
  12.  
  13.         supermarket.SellingProducts();
  14.     }
  15. }
  16.  
  17. class Supermarket
  18. {
  19.     Random random = new Random();
  20.  
  21.     private int _supermarketMoney = 0;
  22.  
  23.     Basket basket = new Basket();
  24.  
  25.     private Queue<Buyer> _buyers = new Queue<Buyer>();
  26.     private List<Product> _products = new List<Product>();
  27.  
  28.     public Supermarket()
  29.     {
  30.         _products.Add(new Product("Банан", 10));
  31.         _products.Add(new Product("Яблоко", 5));
  32.         _products.Add(new Product("Груша", 15));
  33.         _products.Add(new Product("Апельсин", 20));
  34.         _products.Add(new Product("Арбуз", 30));
  35.  
  36.         CreateNewBuyer(5);
  37.     }
  38.  
  39.     public void CreateNewBuyer(int count)
  40.     {
  41.         int minMoney = 30;
  42.         int maxMoney = 51;
  43.  
  44.         for (int i = 0; i < count; i++)
  45.         {
  46.             _buyers.Enqueue(new Buyer(random.Next(minMoney,maxMoney)));
  47.         }
  48.     }
  49.  
  50.     public void GiveProducts()
  51.     {
  52.         Product product = null;
  53.  
  54.         int _maxbuyCount = 6;
  55.         int _minbuyCount = 2;
  56.         int productCount = random.Next(_minbuyCount,_maxbuyCount);
  57.        
  58.         for (int i = 0; i < productCount; i++)
  59.         {
  60.             int randomIndex = random.Next(_products.Count);
  61.             product = _products[randomIndex];
  62.             basket.TakeProducts(product);
  63.         }
  64.     }
  65.  
  66.     public void SellingProducts()
  67.     {
  68.         bool isBuy = true;
  69.         int productsAllPrice;
  70.  
  71.         Console.WriteLine($"Сегодня супермаркет заработал {_supermarketMoney} монет.");
  72.  
  73.         Buyer buyer = _buyers.Dequeue();
  74.         GiveProducts();
  75.         productsAllPrice = basket.AllPrice();
  76.  
  77.         Console.WriteLine($"К кассе подошёл покупатель. У него {buyer.Money} монет. \nОн хочет купить продукты на сумму {productsAllPrice} монет.");
  78.  
  79.         while (isBuy)
  80.         {
  81.             if (buyer.Money < productsAllPrice)
  82.             {
  83.                 Console.WriteLine("У покупателя недостаточно монет! Нужно убрать ненужные товары из корзины чтобы хватило монет.");
  84.  
  85.                 basket.ShowProducts();
  86.                 basket.RemoveProductInBascet();
  87.                 productsAllPrice = basket.AllPrice();
  88.             }
  89.             else
  90.             {
  91.                 Console.WriteLine("Довольный покупатель уходит со своими товарами.");
  92.                 isBuy = false;
  93.             }
  94.  
  95.             Console.ReadKey();
  96.         }
  97.     }
  98. }
  99.  
  100. class Basket
  101. {
  102.     Random random = new Random();
  103.  
  104.     private int _productsAllPrice = 0;
  105.  
  106.     private List<Product> _productsInBasket = new List<Product>();
  107.  
  108.     public void TakeProducts(Product product)
  109.     {
  110.         _productsInBasket.Add(product);
  111.     }
  112.  
  113.     public int AllPrice()
  114.     {
  115.         _productsAllPrice = 0;
  116.  
  117.         foreach (Product product in _productsInBasket)
  118.         {
  119.             _productsAllPrice += product.Price;
  120.         }
  121.  
  122.         return _productsAllPrice;
  123.     }
  124.  
  125.     public void ShowProducts()
  126.     {
  127.         foreach (Product product in _productsInBasket)
  128.         {
  129.             Console.WriteLine($"{product.Name}");
  130.         }
  131.     }
  132.  
  133.     public void RemoveProductInBascet()
  134.     {
  135.         Product product = null;
  136.  
  137.         int randomIndex = random.Next(_productsInBasket.Count);
  138.  
  139.         product = _productsInBasket[randomIndex];        
  140.  
  141.         _productsInBasket.Remove(product);
  142.     }
  143. }
  144.  
  145. class Buyer
  146. {
  147.     public Buyer(int money)
  148.     {
  149.         Money = money;
  150.     }
  151.  
  152.     public int Money { get; private set; }
  153. }
  154.  
  155. class Product
  156. {
  157.     public Product(string name, int price)
  158.     {
  159.         Name = name;
  160.         Price = price;
  161.     }
  162.  
  163.     public string Name { get; private set; }
  164.     public int Price { get; private set; }
  165. }
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement