Advertisement
Rodunskiy

Untitled

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