Advertisement
Rodunskiy

Untitled

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