Advertisement
holllowknight

DZSuperMarket

Jul 25th, 2020 (edited)
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace DZSuperMarket
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int clientsNumber = 10;
  11.             SuperMarket superMarket = new SuperMarket();
  12.  
  13.             for (int i = 0; i < clientsNumber; i++)
  14.                 superMarket.AddClient(new Client());
  15.  
  16.             superMarket.Serve();
  17.         }
  18.     }
  19.  
  20.     class SuperMarket
  21.     {
  22.         private Queue<Client> _clients;
  23.         private int _servedClientsCount;
  24.         private int _balance;
  25.         private Product[] _showcase;
  26.  
  27.         public SuperMarket()
  28.         {
  29.             _clients = new Queue<Client>();
  30.             _showcase = new Product[]
  31.             {
  32.                 new Product(80, "Хлеб"),
  33.                 new Product(250, "Сосиски"),
  34.                 new Product(120, "Шпроты"),
  35.                 new Product(100, "Кола"),
  36.                 new Product(70, "Чипсы"),
  37.                 new Product(90, "Санитайзер"),
  38.                 new Product(300, "Имбирь")
  39.             };
  40.         }
  41.  
  42.         public void AddClient(Client client)
  43.         {
  44.             client.FillCart((IReadOnlyList<Product>)_showcase);
  45.             _clients.Enqueue(client);
  46.         }
  47.  
  48.         public void Serve()
  49.         {
  50.             while(_clients.Count != 0)
  51.             {
  52.                 Client servingClient = _clients.Dequeue();
  53.                 _servedClientsCount++;
  54.                 Console.WriteLine($"Обслуживаем клиента номер {_servedClientsCount}");
  55.                 int cartCost = servingClient.GetCartCost();
  56.                 Console.WriteLine($"Стоимость товаров в корзине: {cartCost}");
  57.                 cartCost = servingClient.PayForPurchases(cartCost);
  58.                 TakeMoney(cartCost);
  59.                 Console.WriteLine($"В кассе: {_balance}");
  60.                 Console.WriteLine();
  61.             }
  62.         }
  63.  
  64.         public void TakeMoney(int money)
  65.         {
  66.             _balance += money;
  67.         }
  68.     }
  69.  
  70.     class Product
  71.     {
  72.         public Product(int price, string name)
  73.         {
  74.             Price = price;
  75.             Name = name;
  76.         }
  77.  
  78.         public int Price { get; private set; }
  79.         public string Name { get; private set; }
  80.     }
  81.  
  82.     class Cart
  83.     {
  84.         private List<Product> _products;
  85.  
  86.         public Cart()
  87.         {
  88.             _products = new List<Product>();
  89.         }
  90.  
  91.         public int GetCost()
  92.         {
  93.             int cost = 0;
  94.  
  95.             for (int i = 0; i < _products.Count; i++)
  96.             {
  97.                 cost += _products[i].Price;
  98.             }
  99.  
  100.             return cost;
  101.         }
  102.  
  103.         public Product ExcludeProduct()
  104.         {
  105.             Random random = new Random();
  106.             int excludingProductNumber = random.Next(_products.Count);
  107.             Product excludingProduct = _products[excludingProductNumber];
  108.             _products.RemoveAt(excludingProductNumber);
  109.             return excludingProduct;
  110.         }
  111.  
  112.         public void AddProduct(Product newProduct)
  113.         {
  114.             _products.Add(newProduct);
  115.         }
  116.     }
  117.  
  118.     class Client
  119.     {
  120.         private static Random _random;
  121.         private Cart _shoppingCart;
  122.         private int _money;
  123.         private int _minBalance = 900;
  124.         private int _maxBalance = 1900;
  125.         private int _productsNumber = 10;
  126.  
  127.         static Client()
  128.         {
  129.             _random = new Random();
  130.         }
  131.  
  132.         public Client()
  133.         {
  134.             _money = _random.Next(_minBalance, _maxBalance);
  135.             _shoppingCart = new Cart();
  136.         }
  137.  
  138.         public int GetCartCost()
  139.         {
  140.             return _shoppingCart.GetCost();
  141.         }
  142.  
  143.         public void FillCart(IReadOnlyList<Product> products)
  144.         {
  145.             for (int i = 0; i < _productsNumber; i++)
  146.             {
  147.                 Product desiredProduct = products[_random.Next(products.Count)];
  148.                 _shoppingCart.AddProduct(desiredProduct);
  149.             }
  150.         }
  151.  
  152.         public int PayForPurchases(int price)
  153.         {
  154.             Console.WriteLine($"У клиента {_money} денег");
  155.  
  156.             while (price > _money)
  157.             {
  158.                 Product excludingProduct = _shoppingCart.ExcludeProduct();
  159.                 Console.WriteLine($"Из корзины убран товар {excludingProduct.Name} стоимостью {excludingProduct.Price}");
  160.                 price -= excludingProduct.Price;
  161.             }
  162.  
  163.             Console.WriteLine($"Оплата товаров стоимостью: {price}");
  164.             return price;
  165.         }
  166.     }
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement