Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class Program
- {
- static void Main(string[] args)
- {
- Supermarket supermarket = new Supermarket();
- supermarket.Run();
- }
- }
- public static class UserUtils
- {
- private static Random s_random = new Random();
- public static int GenerateRandomNumber(int minRandom, int maxRandom)
- {
- return s_random.Next(minRandom, maxRandom + 1);
- }
- public static int GenerateRandomNumber(int maxRandom)
- {
- return s_random.Next(maxRandom);
- }
- }
- class Supermarket
- {
- private int _money;
- private List<Product> _products = new List<Product>();
- private Queue<Client> _clientsQueue = new Queue<Client>();
- public Supermarket()
- {
- _products.Add(new Product("Хлеб", 50));
- _products.Add(new Product("Молоко", 80));
- _products.Add(new Product("Сыр", 200));
- _products.Add(new Product("Яблоко", 30));
- _products.Add(new Product("Шоколад", 120));
- }
- public void Run()
- {
- bool isRun = true;
- Console.WriteLine("=== Супермаркет открыт ===");
- FillQueue();
- while (isRun)
- {
- if (_clientsQueue.Count == 0)
- {
- Console.WriteLine("Очередь пуста!");
- isRun = false;
- }
- else
- {
- ServeNextClient();
- }
- Console.ReadKey();
- }
- Console.WriteLine($"Выручка за день: {_money} рублей.");
- Console.WriteLine("=== Супермаркет закрыт ===");
- Console.ReadKey();
- }
- private void FillQueue()
- {
- int maxClients = 10;
- int clients = UserUtils.GenerateRandomNumber(maxClients);
- for (int i = 0; i < clients; i++)
- {
- _clientsQueue.Enqueue(CreateRandomClient());
- }
- }
- private void ServeNextClient()
- {
- Client client = _clientsQueue.Dequeue();
- int totalPrice = client.GetTotalPriceCart();
- Console.WriteLine($"\nОбслуживаем клиента. (деньги: {client.Money})");
- client.ShowCart();
- Console.WriteLine($"Сумма корзины: {totalPrice}");
- while (totalPrice > client.Money)
- {
- Console.WriteLine($"У покупателя не хватает денег, убираем случайный продукт.");
- client.RemoveRandomProductOnCart();
- totalPrice = client.GetTotalPriceCart();
- Console.WriteLine($"Сумма корзины: {totalPrice}");
- }
- if (totalPrice <= client.Money && totalPrice > 0)
- {
- IncreaseMoney(totalPrice);
- client.BayProducts();
- Console.WriteLine($"Оплатил {totalPrice} руб. Остаток: {client.Money}");
- }
- else
- {
- Console.WriteLine("Ничего не купил.");
- }
- }
- private void IncreaseMoney(int money)
- {
- _money += money;
- }
- private Client CreateRandomClient()
- {
- int minProductsCount = 1;
- int maxProductsCount = 10;
- int minMoney = 100;
- int maxMoney = 500;
- int money = UserUtils.GenerateRandomNumber(minMoney, maxMoney);
- Client client = new Client(money);
- int productsCount = UserUtils.GenerateRandomNumber(minProductsCount, maxProductsCount);
- for (int i = 0; i < productsCount; i++)
- {
- Product randomProduct = _products[UserUtils.GenerateRandomNumber(_products.Count)];
- client.AddProductInCart(randomProduct);
- }
- return client;
- }
- }
- 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 List<Product> _cart = new List<Product>();
- private List<Product> _bag = new List<Product>();
- public Client(int money)
- {
- Money = money;
- }
- public int Money { get; private set; }
- public void AddProductInCart(Product product)
- {
- _cart.Add(product);
- }
- public int GetTotalPriceCart()
- {
- int totalPrice = _cart.Sum(price => price.Price);
- return totalPrice;
- }
- public void ShowCart()
- {
- foreach (Product product in _cart)
- {
- Console.Write(product.Name + ", ");
- }
- }
- public void RemoveRandomProductOnCart()
- {
- _cart.RemoveAt(UserUtils.GenerateRandomNumber(_cart.Count));
- }
- public void BayProducts()
- {
- Money -= GetTotalPriceCart();
- MoveFromCartToBag();
- _cart.Clear();
- }
- private void MoveFromCartToBag()
- {
- _bag.AddRange(_cart);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment