Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Home_Work
- {
- class Program
- {
- static void Main()
- {
- Supermarket supermarket = new Supermarket();
- supermarket.Work();
- }
- }
- class Supermarket
- {
- private List<Product> _assortment;
- private Queue<Client> _clients;
- private int _cash;
- public Supermarket()
- {
- _assortment = new List<Product>();
- _clients = new Queue<Client>();
- TakeDeliveryOfProducts();
- GenerateQueue();
- }
- public void Work()
- {
- while (_clients.Count > 0)
- {
- Client client = _clients.Dequeue();
- bool isPaidOfPurchase = false;
- while (isPaidOfPurchase == false)
- {
- ShowInfo();
- client.ShowInfo();
- Console.WriteLine();
- isPaidOfPurchase = TrySell(client);
- Console.ReadKey();
- Console.Clear();
- }
- }
- Console.WriteLine("Пока магазин работал, вы заработали " + _cash + " рублей.");
- Console.WriteLine("Вы хорошо поработали, идите домой.");
- }
- private bool TrySell(Client client)
- {
- if (client.IsEmptyBasket == true)
- {
- Console.WriteLine("Клиент отказался от всех продуктов и ушёл.");
- return true;
- }
- else if (client.IsSolvent == true)
- {
- Console.WriteLine("Клиент оплачивает покупку на " + client.Purchase + " рублей.");
- _cash += client.Buy();
- Console.WriteLine("У клиента осталось " + client.Money + " рублей.");
- return true;
- }
- else
- {
- Console.WriteLine("Клиенту не хватает денег, что бы оплатить покупку.");
- client.RemoveProductFromBasket();
- return false;
- }
- }
- private void ShowInfo()
- {
- int topPosition = 0;
- int leftPosition = 50;
- Console.SetCursorPosition(leftPosition, topPosition++);
- Console.WriteLine("В очереди " + _clients.Count + " клиентов.");
- Console.SetCursorPosition(leftPosition, topPosition);
- Console.WriteLine("В кассе " + _cash + " рублей.");
- }
- private void TakeDeliveryOfProducts()
- {
- _assortment.Add(new Product("Апельсин", 40));
- _assortment.Add(new Product("Молоко", 80));
- _assortment.Add(new Product("Пепси", 120));
- _assortment.Add(new Product("Крем для загара", 80));
- _assortment.Add(new Product("Шашлык из свинины", 100));
- _assortment.Add(new Product("Яблоко", 30));
- _assortment.Add(new Product("Гель для душа", 70));
- _assortment.Add(new Product("Творог", 60));
- _assortment.Add(new Product("Гречка", 65));
- _assortment.Add(new Product("Рис", 55));
- _assortment.Add(new Product("Сникерс", 50));
- _assortment.Add(new Product("Кит-кат", 90));
- }
- private void GenerateQueue()
- {
- int countClients = 20;
- for (int i = 0; i < countClients; i++)
- {
- _clients.Enqueue(new Client(_assortment));
- }
- }
- }
- class Client
- {
- private static Random _random;
- private List<Product> _basket;
- private int _money;
- private int _purchase;
- static Client()
- {
- _random = new Random();
- }
- public Client(List<Product> assortment)
- {
- int maxMoney = 1000;
- int minMoney = 100;
- _money = _random.Next(minMoney, maxMoney);
- _basket = new List<Product>();
- FillBasket(assortment);
- }
- public int Money => _money;
- public int Purchase => _purchase;
- public bool IsSolvent => _money >= _purchase;
- public bool IsEmptyBasket => _basket.Count <= 0;
- public void ShowInfo()
- {
- int index = 1;
- SumPurchaseBasket();
- Console.SetCursorPosition(0, 0);
- Console.WriteLine("Кол-во денег у клиента: " + _money);
- Console.WriteLine("Товары в корзине:");
- foreach (Product product in _basket)
- {
- Console.WriteLine(index + ". " + product.Name);
- index++;
- }
- Console.WriteLine("Сумма всех товаров: " + _purchase);
- }
- public void RemoveProductFromBasket()
- {
- int randomProductIndex = _random.Next(_basket.Count);
- Console.WriteLine("Клиент убрал из корзины " + _basket[randomProductIndex].Name);
- _basket.RemoveAt(randomProductIndex);
- }
- public int Buy()
- {
- _money -= _purchase;
- return _purchase;
- }
- private void SumPurchaseBasket()
- {
- _purchase = 0;
- foreach(Product product in _basket)
- {
- _purchase += product.Price;
- }
- }
- private void FillBasket(List<Product> assortment)
- {
- int minCountProduct = 3;
- int maxCountProduct = 10;
- int countProductInBacket = _random.Next(minCountProduct, maxCountProduct);
- for(int i = 0; i < countProductInBacket; i++)
- {
- _basket.Add(assortment[_random.Next(assortment.Count)]);
- }
- }
- }
- class Product
- {
- private string _name;
- private int _price;
- public Product(string name, int price)
- {
- _name = name;
- _price = price;
- }
- public string Name => _name;
- public int Price => _price;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement