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)
- {
- int clientsCount = 10;
- Queue<Client> clients = new Queue<Client>();
- for (int i = 0; i < clientsCount; i++)
- {
- clients.Enqueue(new Client());
- }
- Store store = new Store(clients);
- store.ServeClient();
- }
- }
- static class UserUtils
- {
- private static readonly Random _random = new Random();
- public static int GenerateRandomNumber(int minValue, int maxValue)
- {
- return _random.Next(minValue, maxValue);
- }
- }
- class Client
- {
- private readonly ProductCart _productCart = new ProductCart();
- private readonly int _money;
- public Client(int minimumMoney = 10, int maximumMoney = 20)
- {
- _money = UserUtils.GenerateRandomNumber(minimumMoney, maximumMoney + 1);
- }
- public bool IsSolvency()
- {
- return _money >= _productCart.GetTotalCost();
- }
- public void RemoveRandomProduct()
- {
- int randomIndexNumber = UserUtils.GenerateRandomNumber(0, _productCart.GetProductCount());
- _productCart.RemoveProduct(randomIndexNumber);
- }
- public bool IsSufficiencyProductsInCart()
- {
- int howManyProductsShouldBeInTheCart = 5;
- return _productCart.GetProductCount() == howManyProductsShouldBeInTheCart;
- }
- public void FillCart(Product product)
- {
- int minChance = 1;
- int maxChance = 100;
- int buyingChance = 40;
- int randomRoll = UserUtils.GenerateRandomNumber(minChance, maxChance + 1);
- if (buyingChance > randomRoll)
- {
- _productCart.AddProduct(new Product(product));
- }
- }
- public void ShowInfo()
- {
- Console.WriteLine("Количество денег у клиента - " + _money + ". Его корзина: ");
- _productCart.ShowInfo();
- }
- }
- class ProductCart
- {
- private readonly List<Product> _products = new List<Product>();
- public ProductCart() { }
- public void AddProduct(Product product)
- {
- _products.Add(product);
- }
- public void RemoveProduct(int indexNumber)
- {
- _products.RemoveAt(indexNumber);
- }
- public int GetTotalCost()
- {
- int totalCost = 0;
- foreach (Product product in _products)
- {
- totalCost += product.Price;
- }
- return totalCost;
- }
- public int GetProductCount()
- {
- return _products.Count;
- }
- public void ShowInfo()
- {
- int counter = 1;
- foreach (Product product in _products)
- {
- Console.Write(counter++ + " ");
- product.ShowInfo();
- }
- }
- }
- class Store
- {
- private readonly List<Product> _productList = new List<Product>();
- private readonly Queue<Client> _clients = new Queue<Client>();
- public Store(Queue<Client> clients)
- {
- _clients = clients;
- FillProductList();
- }
- public void ServeClient()
- {
- Console.WriteLine("Добро пожаловать в \"Администрирование супермаркетом\"");
- Console.WriteLine("Какие товары есть у нас в магазине?");
- ShowAllProductList();
- System.Threading.Thread.Sleep(1000);
- Console.WriteLine("Открываем кассу магазина");
- OpenCheckout();
- Console.WriteLine("Клиентов больше не осталось. Магазин закрывается!");
- }
- public void OpenCheckout()
- {
- int counter = 0;
- while (_clients.Count > 0)
- {
- Client client = _clients.Dequeue();
- counter++;
- SingleClientService(client, counter);
- }
- }
- public void SingleClientService(Client client, int counter)
- {
- bool isSuffeciencyProductsInCart = false;
- System.Threading.Thread.Sleep(1000);
- Console.WriteLine("\n\nКлиент номер " + counter);
- Console.WriteLine("Заполняем корзину...");
- while (isSuffeciencyProductsInCart == false)
- {
- for (int i = 0; i < _productList.Count; i++)
- {
- if (isSuffeciencyProductsInCart == false)
- {
- client.FillCart(_productList[i]);
- isSuffeciencyProductsInCart = client.IsSufficiencyProductsInCart();
- }
- else
- {
- i = _productList.Count;
- }
- }
- }
- client.ShowInfo();
- System.Threading.Thread.Sleep(1000);
- PaymentTime(client);
- }
- public void PaymentTime(Client client)
- {
- bool isCanPurchase = client.IsSolvency();
- Console.WriteLine("Пробуем оплатить");
- System.Threading.Thread.Sleep(1000);
- if (isCanPurchase)
- {
- Console.WriteLine("У клиента оплата прошла успешно, ушёл довольным.");
- }
- else
- {
- while (isCanPurchase == false)
- {
- Console.WriteLine("У клиента номер не достаточно денег. Выбрасываем товар.");
- client.RemoveRandomProduct();
- isCanPurchase = client.IsSolvency();
- System.Threading.Thread.Sleep(500);
- }
- Console.WriteLine("Клиент номер наконец смог оплатить свой товар. Ушёл грустным...");
- }
- System.Threading.Thread.Sleep(1000);
- }
- public void ShowAllProductList()
- {
- int counter = 1;
- Console.WriteLine("Показываю весь список товаров:");
- foreach (Product product in _productList)
- {
- Console.Write(counter + " ");
- product.ShowInfo();
- counter++;
- }
- }
- private void FillProductList()
- {
- _productList.Add(new Product("Яблоки", 4, "Свежие, вкусные, сочные", "Франция"));
- _productList.Add(new Product("Груши", 6, "Формой похож на лампочку", "Польша"));
- _productList.Add(new Product("Бульба", 3, "Вырасчаная настаящыми беларусами", "Беларусь"));
- _productList.Add(new Product("Клубника", 11, "Красная свежая клубника из Вьетнама", "Вьетнам"));
- _productList.Add(new Product("Брокколи", 7, "Зеленые, как маленькие деревья", "Германия"));
- _productList.Add(new Product("Презервативы", 15, "Свежие, вкусные, сочные", "Китай"));
- _productList.Add(new Product("Капуста", 2, "Капуста. Да, просто капуста", "Россия"));
- _productList.Add(new Product("Бананы", 5, "Вкусные, только съешь меня", "Кипр"));
- }
- }
- class Product
- {
- public Product(string name, int price, string description, string manufacturer)
- {
- Name = name;
- Price = price;
- Description = description;
- ManufacturerCountry = manufacturer;
- }
- public Product(Product product)
- {
- Name = product.Name;
- Price = product.Price;
- Description = product.Description;
- ManufacturerCountry = product.ManufacturerCountry;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- public string Description { get; private set; }
- public string ManufacturerCountry { get; private set; }
- public void ShowInfo()
- {
- Console.WriteLine($"Имя - {Name}, цена - {Price}, Описание - {Description}, Страна изготовитель - {ManufacturerCountry}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement