Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace DZSuperMarket
- {
- class Program
- {
- static void Main(string[] args)
- {
- int clientsNumber = 10;
- SuperMarket superMarket = new SuperMarket();
- for (int i = 0; i < clientsNumber; i++)
- superMarket.AddClient(new Client());
- superMarket.Serve();
- }
- }
- class SuperMarket
- {
- private Queue<Client> _clients;
- private int _servedClientsCount;
- private int _balance;
- private Product[] _showcase;
- public SuperMarket()
- {
- _clients = new Queue<Client>();
- _showcase = new Product[]
- {
- new Product(80, "Хлеб"),
- new Product(250, "Сосиски"),
- new Product(120, "Шпроты"),
- new Product(100, "Кола"),
- new Product(70, "Чипсы"),
- new Product(90, "Санитайзер"),
- new Product(300, "Имбирь")
- };
- }
- public void AddClient(Client client)
- {
- client.FillCart((IReadOnlyList<Product>)_showcase);
- _clients.Enqueue(client);
- }
- public void Serve()
- {
- while(_clients.Count != 0)
- {
- Client servingClient = _clients.Dequeue();
- _servedClientsCount++;
- Console.WriteLine($"Обслуживаем клиента номер {_servedClientsCount}");
- int cartCost = servingClient.GetCartCost();
- Console.WriteLine($"Стоимость товаров в корзине: {cartCost}");
- cartCost = servingClient.PayForPurchases(cartCost);
- TakeMoney(cartCost);
- Console.WriteLine($"В кассе: {_balance}");
- Console.WriteLine();
- }
- }
- public void TakeMoney(int money)
- {
- _balance += money;
- }
- }
- class Product
- {
- public Product(int price, string name)
- {
- Price = price;
- Name = name;
- }
- public int Price { get; private set; }
- public string Name { get; private set; }
- }
- class Cart
- {
- private List<Product> _products;
- public Cart()
- {
- _products = new List<Product>();
- }
- public int GetCost()
- {
- int cost = 0;
- for (int i = 0; i < _products.Count; i++)
- {
- cost += _products[i].Price;
- }
- return cost;
- }
- public Product ExcludeProduct()
- {
- Random random = new Random();
- int excludingProductNumber = random.Next(_products.Count);
- Product excludingProduct = _products[excludingProductNumber];
- _products.RemoveAt(excludingProductNumber);
- return excludingProduct;
- }
- public void AddProduct(Product newProduct)
- {
- _products.Add(newProduct);
- }
- }
- class Client
- {
- private static Random _random;
- private Cart _shoppingCart;
- private int _money;
- private int _minBalance = 900;
- private int _maxBalance = 1900;
- private int _productsNumber = 10;
- static Client()
- {
- _random = new Random();
- }
- public Client()
- {
- _money = _random.Next(_minBalance, _maxBalance);
- _shoppingCart = new Cart();
- }
- public int GetCartCost()
- {
- return _shoppingCart.GetCost();
- }
- public void FillCart(IReadOnlyList<Product> products)
- {
- for (int i = 0; i < _productsNumber; i++)
- {
- Product desiredProduct = products[_random.Next(products.Count)];
- _shoppingCart.AddProduct(desiredProduct);
- }
- }
- public int PayForPurchases(int price)
- {
- Console.WriteLine($"У клиента {_money} денег");
- while (price > _money)
- {
- Product excludingProduct = _shoppingCart.ExcludeProduct();
- Console.WriteLine($"Из корзины убран товар {excludingProduct.Name} стоимостью {excludingProduct.Price}");
- price -= excludingProduct.Price;
- }
- Console.WriteLine($"Оплата товаров стоимостью: {price}");
- return price;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement