Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Diagnostics;
- using System.Xml.Linq;
- namespace Супермаркет
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Supermarket supermarket = new Supermarket();
- supermarket.Work();
- }
- }
- class Supermarket
- {
- private Client _client = new Client();
- private int _purchasePrice;
- private int _quantityClients;
- private List<Product> _products = new List<Product>();
- private List<Product> _clientBasket = new List<Product>();
- private Queue<Client> _clients;
- public Supermarket()
- {
- _clients = new Queue<Client>(_quantityClients);
- _clientBasket = _client.ReturnClientBasket();
- _products.Add(new Product(0, "Пакет", 10));
- _products.Add(new Product(1, "Бананы", 85));
- _products.Add(new Product(2, "Молоко", 65));
- _products.Add(new Product(3, "Пиво", 50));
- _products.Add(new Product(4, "Курица", 320));
- _products.Add(new Product(5, "Сосиски", 125));
- _products.Add(new Product(6, "Кола", 75));
- }
- public void Work()
- {
- Console.WriteLine("Введите количество посетителей:");
- _quantityClients = Utility.ReturnInputNumber();
- for (int i = 0; i < _quantityClients; i++)
- {
- AddClient();
- FillBasket();
- RemoveUnnecessaryProduct();
- InspectionBasketForAbsenceProduct();
- ShowSale();
- Console.ForegroundColor = ConsoleColor.DarkYellow;
- Console.WriteLine("\nНажмите любую клавишу для вызова следующего клиента");
- Console.ForegroundColor = ConsoleColor.White;
- Console.ReadKey();
- Console.Clear();
- _purchasePrice = 0;
- _clients.Dequeue();
- }
- }
- private void AddClient()
- {
- _clients.Enqueue(new Client());
- }
- private bool CanPay()
- {
- while (_clients.Peek().Money < _purchasePrice)
- {
- Console.ForegroundColor = ConsoleColor.DarkGreen;
- Console.Write($"\nСумма покупки: {_purchasePrice}");
- Console.ForegroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.DarkYellow;
- Console.Write($"\nДеньги покупателя: {_clients.Peek().Money}\n");
- Console.ForegroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Денег не хватает, Галя! Отмена!");
- Console.ForegroundColor = ConsoleColor.White;
- return false;
- }
- return true;
- }
- private void RemoveUnnecessaryProduct()
- {
- if (CanPay() == false)
- {
- RemoveRandomProduct();
- RemoveUnnecessaryProduct();
- }
- }
- private void InspectionBasketForAbsenceProduct()
- {
- if (_clientBasket.Count == 0)
- {
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.WriteLine("\nЖалобную книгу мне! (Клиент передумал покупать в этом магазине)");
- Console.ForegroundColor = ConsoleColor.White;
- return;
- }
- }
- private void ShowSale()
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write($"\nСумма покупки: {_purchasePrice}");
- Console.ForegroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.DarkYellow;
- Console.Write($"\nДеньги покупателя: {_clients.Peek().Money}");
- Console.ForegroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("\nДенег хватает\n");
- Console.ForegroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.DarkCyan;
- Console.WriteLine("Купленные товары:\n");
- Console.ForegroundColor = ConsoleColor.White;
- ShowProductBasket();
- }
- private void RemoveRandomProduct()
- {
- int minRandomNumber = 0;
- int randomProduct = Utility.GenerateRandomNumber(minRandomNumber, _clientBasket.Count);
- Console.Write("Удаленный товар: ");
- ShowProduct(randomProduct);
- _purchasePrice -= _clientBasket[randomProduct].Price;
- _clientBasket.RemoveAt(randomProduct);
- }
- private void ShowProduct(int foundProduct)
- {
- Console.WriteLine($"ID: {_clientBasket[foundProduct].Id} Название: {_clientBasket[foundProduct].Title} Цена: {_clientBasket[foundProduct].Price} Руб.");
- }
- private void ShowProductBasket()
- {
- for (int i = 0; i < _clientBasket.Count; i++)
- {
- ShowProduct(i);
- }
- }
- private void FillBasket()
- {
- int minQuantityPurchases = 2;
- int maxQuantityPurchases = 10;
- int minIndexProduct = 0;
- int quantityPurchases = Utility.GenerateRandomNumber(minQuantityPurchases, maxQuantityPurchases);
- _clientBasket = new List<Product>(quantityPurchases);
- for (int i = 0; i < quantityPurchases; i++)
- {
- int randomIndexOfProduct = Utility.GenerateRandomNumber(minIndexProduct, _products.Count);
- _clientBasket.Add(_products[randomIndexOfProduct]);
- _purchasePrice += _products[randomIndexOfProduct].Price;
- ShowProduct(i);
- }
- }
- }
- class Client
- {
- private List<Product> _basket = new List<Product>();
- public Client()
- {
- int minCashInPocket = 100;
- int maxCashInPocket = 1200;
- Money = Utility.GenerateRandomNumber(minCashInPocket, maxCashInPocket);
- }
- public int Money { get; private set; }
- public List<Product> ReturnClientBasket()
- {
- return new List<Product>(_basket);
- }
- }
- class Product
- {
- public Product(int id, string name, int price)
- {
- Id = id;
- Title = name;
- Price = price;
- }
- public int Id { get; private set; }
- public string Title { get; private set; }
- public int Price { get; private set; }
- }
- class Utility
- {
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int min, int max)
- {
- return s_random.Next(min, max);
- }
- public static int ReturnInputNumber()
- {
- int number;
- while (int.TryParse(Console.ReadLine(), out number) == false)
- {
- Console.WriteLine("Введено не число, попробуйте еще раз: ");
- }
- return number;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement