NikaBang

ДЗ: Супермаркет

Mar 10th, 2026 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.02 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         Supermarket supermarket = new Supermarket();
  10.         supermarket.Run();
  11.     }
  12. }
  13.  
  14. public static class UserUtils
  15. {
  16.     private static Random s_random = new Random();
  17.  
  18.     public static int GenerateRandomNumber(int minRandom, int maxRandom)
  19.     {
  20.         return s_random.Next(minRandom, maxRandom + 1);
  21.     }
  22.  
  23.     public static int GenerateRandomNumber(int maxRandom)
  24.     {
  25.         return s_random.Next(maxRandom);
  26.     }
  27. }
  28.  
  29. class Supermarket
  30. {
  31.     private int _money;
  32.     private List<Product> _products = new List<Product>();
  33.     private Queue<Client> _clientsQueue = new Queue<Client>();
  34.  
  35.     public Supermarket()
  36.     {
  37.         _products.Add(new Product("Хлеб", 50));
  38.         _products.Add(new Product("Молоко", 80));
  39.         _products.Add(new Product("Сыр", 200));
  40.         _products.Add(new Product("Яблоко", 30));
  41.         _products.Add(new Product("Шоколад", 120));
  42.     }
  43.  
  44.  
  45.     public void Run()
  46.     {
  47.         bool isRun = true;
  48.  
  49.         Console.WriteLine("=== Супермаркет открыт ===");
  50.  
  51.         FillQueue();
  52.  
  53.         while (isRun)
  54.         {
  55.             if (_clientsQueue.Count == 0)
  56.             {
  57.                 Console.WriteLine("Очередь пуста!");
  58.                 isRun = false;
  59.             }
  60.             else
  61.             {
  62.                 ServeNextClient();
  63.             }
  64.  
  65.             Console.ReadKey();
  66.         }
  67.  
  68.         Console.WriteLine($"Выручка за день: {_money} рублей.");
  69.         Console.WriteLine("=== Супермаркет закрыт ===");
  70.         Console.ReadKey();
  71.     }
  72.  
  73.     private void FillQueue()
  74.     {
  75.         int maxClients = 10;
  76.         int clients = UserUtils.GenerateRandomNumber(maxClients);
  77.  
  78.         for (int i = 0; i < clients; i++)
  79.         {
  80.             _clientsQueue.Enqueue(CreateRandomClient());
  81.         }
  82.     }
  83.  
  84.     private void ServeNextClient()
  85.     {
  86.         Client client = _clientsQueue.Dequeue();
  87.  
  88.         int totalPrice = client.GetTotalPriceCart();
  89.  
  90.         Console.WriteLine($"\nОбслуживаем клиента. (деньги: {client.Money})");
  91.        
  92.         client.ShowCart();
  93.  
  94.         Console.WriteLine($"Сумма корзины: {totalPrice}");
  95.  
  96.         while (totalPrice > client.Money)
  97.         {
  98.             Console.WriteLine($"У покупателя не хватает денег, убираем случайный продукт.");
  99.            
  100.             client.RemoveRandomProductOnCart();
  101.             totalPrice = client.GetTotalPriceCart();
  102.  
  103.             Console.WriteLine($"Сумма корзины: {totalPrice}");
  104.         }
  105.  
  106.         if (totalPrice <= client.Money && totalPrice > 0)
  107.         {
  108.             IncreaseMoney(totalPrice);
  109.             client.BayProducts();
  110.  
  111.             Console.WriteLine($"Оплатил {totalPrice} руб. Остаток: {client.Money}");
  112.         }
  113.         else
  114.         {
  115.             Console.WriteLine("Ничего не купил.");
  116.         }
  117.     }
  118.  
  119.     private void IncreaseMoney(int money)
  120.     {
  121.         _money += money;
  122.     }
  123.  
  124.     private Client CreateRandomClient()
  125.     {
  126.         int minProductsCount = 1;
  127.         int maxProductsCount = 10;
  128.         int minMoney = 100;
  129.         int maxMoney = 500;
  130.  
  131.         int money = UserUtils.GenerateRandomNumber(minMoney, maxMoney);
  132.  
  133.         Client client = new Client(money);
  134.  
  135.         int productsCount = UserUtils.GenerateRandomNumber(minProductsCount, maxProductsCount);
  136.  
  137.         for (int i = 0; i < productsCount; i++)
  138.         {
  139.             Product randomProduct = _products[UserUtils.GenerateRandomNumber(_products.Count)];
  140.             client.AddProductInCart(randomProduct);
  141.         }
  142.  
  143.         return client;
  144.     }
  145. }
  146.  
  147. class Product
  148. {
  149.     public Product(string name, int price)
  150.     {
  151.         Name = name;
  152.         Price = price;
  153.     }
  154.  
  155.     public string Name { get; private set; }
  156.     public int Price { get; private set; }
  157. }
  158.  
  159. class Client
  160. {
  161.     private List<Product> _cart = new List<Product>();
  162.     private List<Product> _bag = new List<Product>();
  163.  
  164.     public Client(int money)
  165.     {
  166.         Money = money;
  167.     }
  168.  
  169.     public int Money { get; private set; }
  170.  
  171.     public void AddProductInCart(Product product)
  172.     {
  173.         _cart.Add(product);
  174.     }
  175.  
  176.     public int GetTotalPriceCart()
  177.     {
  178.         int totalPrice = _cart.Sum(price => price.Price);
  179.         return totalPrice;
  180.     }
  181.  
  182.     public void ShowCart()
  183.     {
  184.         foreach (Product product in _cart)
  185.         {
  186.             Console.Write(product.Name + ", ");
  187.         }
  188.     }
  189.  
  190.     public void RemoveRandomProductOnCart()
  191.     {
  192.         _cart.RemoveAt(UserUtils.GenerateRandomNumber(_cart.Count));
  193.     }
  194.  
  195.     public void BayProducts()
  196.     {
  197.         Money -= GetTotalPriceCart();
  198.         MoveFromCartToBag();
  199.         _cart.Clear();
  200.     }
  201.  
  202.     private void MoveFromCartToBag()
  203.     {
  204.         _bag.AddRange(_cart);
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment