Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Program
- {
- static void Main(string[] args)
- {
- Supermarket supermarket = new Supermarket();
- supermarket.SellProducts();
- }
- }
- class Supermarket
- {
- private int _supermarketMoney = 0;
- private Basket basket = new Basket();
- private Queue<Buyer> _buyers = new Queue<Buyer>();
- private List<Product> _products = new List<Product>();
- public Supermarket()
- {
- _products.Add(new Product("Банан", 10));
- _products.Add(new Product("Яблоко", 5));
- _products.Add(new Product("Груша", 15));
- _products.Add(new Product("Апельсин", 20));
- _products.Add(new Product("Арбуз", 30));
- CreateNewBuyers(5);
- }
- public void CreateNewBuyers(int count)
- {
- int minMoney = 30;
- int maxMoney = 50;
- for (int i = 0; i < count; i++)
- {
- _buyers.Enqueue(new Buyer(UserUtils.GetRandomNumber(minMoney,maxMoney)));
- }
- }
- public void FillBasket()
- {
- Product product = null;
- int _maxBuyCount = 5;
- int _minBuyCount = 2;
- int productCount = UserUtils.GetRandomNumber(_minBuyCount, _maxBuyCount);
- for (int i = 0; i < productCount; i++)
- {
- int randomIndex = UserUtils.GetRandomNumber(0, _products.Count - 1);
- product = _products[randomIndex];
- basket.TakeProduct(product);
- }
- }
- public void SellProducts()
- {
- bool isWorking = true;
- int productsAllPrice;
- Console.WriteLine($"Сегодня супермаркет заработал {_supermarketMoney} монет.");
- Buyer buyer = _buyers.Dequeue();
- FillBasket();
- productsAllPrice = basket.GiveAllPrice();
- Console.WriteLine($"К кассе подошёл покупатель. У него {buyer.Money} монет. \nОн хочет купить продукты на сумму {productsAllPrice} монет.");
- while (isWorking)
- {
- if (buyer.Money < productsAllPrice)
- {
- Console.WriteLine("У покупателя недостаточно монет! Нужно убрать ненужные товары из корзины чтобы хватило монет.");
- basket.ShowProducts();
- basket.RemoveRandomProduct();
- productsAllPrice = basket.GiveAllPrice();
- }
- else
- {
- Console.WriteLine("Довольный покупатель уходит со своими товарами.");
- isWorking = false;
- }
- Console.ReadKey();
- }
- }
- }
- class Basket
- {
- private int _productsTotalPrice = 0;
- private List<Product> _productsInBasket = new List<Product>();
- public void TakeProduct(Product product)
- {
- _productsInBasket.Add(product);
- }
- public int GiveAllPrice()
- {
- _productsTotalPrice = 0;
- foreach (Product product in _productsInBasket)
- {
- _productsTotalPrice += product.Price;
- }
- return _productsTotalPrice;
- }
- public void ShowProducts()
- {
- foreach (Product product in _productsInBasket)
- {
- Console.WriteLine($"{product.Name}");
- }
- }
- public void RemoveRandomProduct()
- {
- Product product = null;
- int randomIndex = UserUtils.GetRandomNumber(0, _productsInBasket.Count);
- product = _productsInBasket[randomIndex];
- _productsInBasket.Remove(product);
- }
- }
- class Buyer
- {
- public Buyer(int money)
- {
- Money = money;
- }
- public int Money { get; private set; }
- }
- class Product
- {
- public Product(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- }
- class UserUtils
- {
- private static Random _random = new Random();
- public static int GetRandomNumber(int min, int max)
- {
- return _random.Next(min, max + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement