Advertisement
Rodunskiy

Untitled

Aug 15th, 2023
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 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 static Random  random = new Random();
  98.  
  99.     private int _productsAllPrice = 0;
  100.  
  101.     private List<Product> _productsInBasket = new List<Product>();
  102.  
  103.     public void TakeProduct(Product product)
  104.     {
  105.         _productsInBasket.Add(product);
  106.     }
  107.  
  108.     public int GiveAllPrice()
  109.     {
  110.         _productsAllPrice = 0;
  111.  
  112.         foreach (Product product in _productsInBasket)
  113.         {
  114.             _productsAllPrice += product.Price;
  115.         }
  116.  
  117.         return _productsAllPrice;
  118.     }
  119.  
  120.     public void ShowProducts()
  121.     {
  122.         foreach (Product product in _productsInBasket)
  123.         {
  124.             Console.WriteLine($"{product.Name}");
  125.         }
  126.     }
  127.  
  128.     public void RemoveRandomProduct()
  129.     {
  130.         Product product = null;
  131.  
  132.         int randomIndex = random.Next(_productsInBasket.Count);
  133.  
  134.         product = _productsInBasket[randomIndex];        
  135.  
  136.         _productsInBasket.Remove(product);
  137.     }
  138. }
  139.  
  140. class Buyer
  141. {
  142.     public Buyer(int money)
  143.     {
  144.         Money = money;
  145.     }
  146.  
  147.     public int Money { get; private set; }
  148. }
  149.  
  150. class Product
  151. {
  152.     public Product(string name, int price)
  153.     {
  154.         Name = name;
  155.         Price = price;
  156.     }
  157.  
  158.     public string Name { get; private set; }
  159.     public int Price { get; private set; }
  160. }
  161.  
  162. class UserUtils
  163. {
  164.     private static Random _random = new Random();
  165.  
  166.     public static int GetRandomNumber(int min, int max)
  167.     {
  168.         return _random.Next(min, max + 1);
  169.     }
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement