Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.ComponentModel.DataAnnotations;
- namespace Supermarket
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Supermarket supermarket = new Supermarket();
- supermarket.Work();
- }
- }
- class Supermarket
- {
- private int _money = 0;
- private List<Product> _products = new List<Product>();
- private Queue<Client> _clients = new Queue<Client>();
- public Supermarket()
- {
- _products.Add(new Product("Кофе", 2500));
- _products.Add(new Product("Хлопья", 50));
- _products.Add(new Product("Сахар", 100));
- _products.Add(new Product("Печенье", 150));
- _products.Add(new Product("Хлеб", 100));
- _products.Add(new Product("Сок", 200));
- }
- public void Work()
- {
- CreateQueue();
- while (_clients.Count > 0)
- {
- Console.WriteLine($"Добро пожаловать в симулятор супермаркета.\n" +
- $"Баланс магазина {_money}\n" +
- $"Ассортимент магазина:");
- ShowProducts();
- Console.WriteLine("Список клиентов в очереди.");
- ShowClients();
- Console.WriteLine("\nНажмите на любую клавишу для осуществления первой покупки.");
- Console.ReadKey();
- ServiceFirstClientInQueue();
- Console.ReadKey();
- Console.Clear();
- }
- Console.WriteLine($"Рабочий день закончен. Баланс магазина {_money}");
- Console.ReadKey();
- }
- private void ServiceFirstClientInQueue()
- {
- int moneyNeed;
- Client firstClientInQueue = _clients.Peek();
- firstClientInQueue.ThrowProductIfMoneyNotEnough();
- moneyNeed = firstClientInQueue.CalculateCartCost();
- firstClientInQueue.AddProductsInBag();
- firstClientInQueue.DeductMoney(moneyNeed);
- TakeMoney(moneyNeed);
- _clients.Dequeue();
- }
- private void CreateQueue()
- {
- Random random = new Random();
- int numberOfMinClients = 1;
- int numberOfMaxClients = 25;
- int clientsInQueue = UserUnits.GenerateRandomNumber(numberOfMinClients, numberOfMaxClients);
- for (int i = 0; i < clientsInQueue; i++)
- {
- AddClientInQueue();
- }
- foreach (Client client in _clients)
- {
- client.AddProductsInCart(new List<Product>(_products));
- client.GiveRandomBalance();
- }
- }
- private void AddClientInQueue()
- {
- _clients.Enqueue(new Client());
- }
- private void ShowProducts()
- {
- foreach (Product product in _products)
- {
- Console.WriteLine($"{product.Name} - {product.Price} руб.");
- }
- }
- private void ShowClients()
- {
- int numberClientInQueue = 1;
- foreach (Client client in _clients)
- {
- Console.WriteLine($"{numberClientInQueue} клиент. Баланс {client.Money}");
- numberClientInQueue++;
- }
- }
- private void TakeMoney(int money)
- {
- _money += money;
- }
- }
- 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 Client
- {
- private int _money;
- private List<Product> _productsInBag = new List<Product>();
- private List<Product> _productsInCart = new List<Product>();
- public int Money => _money;
- public int CountProducts => _productsInBag.Count;
- public void AddProductsInBag()
- {
- int numberOfProducts = CountProducts;
- for (int i = 0; numberOfProducts > i; i++)
- {
- ShowInfo();
- Console.WriteLine("Нажмите на любую клавишу, чтобы переложить товар.\n");
- Console.ReadKey();
- _productsInBag.Add(Transfer());
- Console.WriteLine();
- Console.WriteLine("Продукты в сумке:\n");
- ShowBag();
- Console.ReadKey();
- }
- }
- public void GiveRandomBalance()
- {
- int minMoney = 500;
- int maxMoney = 4000;
- _money = UserUnits.GenerateRandomNumber(minMoney, maxMoney);
- }
- public void AddProductsInCart(List<Product> products)
- {
- int minCountProductsInCart = 1;
- int maxCountProductsInCart = products.Count;
- int firstIndex = 0;
- int countProductsInCart = UserUnits.GenerateRandomNumber(minCountProductsInCart, maxCountProductsInCart);
- for (int i = 0; i < countProductsInCart; i++)
- {
- int productIndex = UserUnits.GenerateRandomNumber(firstIndex, products.Count);
- _productsInCart.Add(products[productIndex]);
- }
- }
- public void ThrowProductIfMoneyNotEnough()
- {
- while (_money < CalculateCartCost())
- {
- ShowInfo();
- Console.WriteLine("Покупателю не хватает денег для всех покупок.\n" +
- "Нажмите любую клавишу, чтобы выложить случайный товар из корзины.\n\n");
- Console.ReadKey();
- ThrowRandomProductOutCart();
- }
- }
- public int CalculateCartCost()
- {
- int cartCost = 0;
- foreach (Product product in _productsInCart)
- {
- cartCost += product.Price;
- }
- return cartCost;
- }
- public void DeductMoney(int money)
- {
- _money -= money;
- }
- private void ShowInfo()
- {
- Console.WriteLine($"Баланс {_money}");
- Console.WriteLine("Продукты в корзине:");
- ShowCart();
- Console.WriteLine();
- }
- private void ShowBag()
- {
- foreach (Product product in _productsInBag)
- {
- Console.Write($"{product.Name} ||");
- }
- }
- private void ShowCart()
- {
- int lastIndexInCart = _productsInCart.Count - 1;
- foreach (Product product in _productsInCart)
- {
- Console.Write($"{product.Name} ||");
- }
- }
- private void ThrowRandomProductOutCart()
- {
- int minIndex = 0;
- int maxIndex = _productsInCart.Count - 1;
- int randomIndex = UserUnits.GenerateRandomNumber(minIndex, maxIndex);
- if (_productsInCart.Count > 0)
- {
- _productsInCart.RemoveAt(randomIndex);
- }
- }
- private Product Transfer()
- {
- Product product = null;
- int firstIndex = 0;
- if (_productsInCart.Count > 0)
- {
- product = _productsInCart[firstIndex];
- _productsInCart.RemoveAt(firstIndex);
- return product;
- }
- else
- {
- return null;
- }
- }
- }
- class UserUnits
- {
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int minNumber, int maxNumber)
- {
- return s_random.Next(minNumber, maxNumber);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment