Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal class Program
- {
- private static void Main(string[] args)
- {
- Supermarket supermarket = new Supermarket();
- supermarket.Work();
- }
- }
- class Product
- {
- public Product(string name, decimal price)
- {
- Name = name;
- Price = price;
- }
- public string Name { get; private set; }
- public decimal Price { get; private set; }
- }
- class Customer
- {
- private List<Product> _shoppingCart = new List<Product>();
- public Customer(string name, decimal money, List<Product> products)
- {
- Name = name;
- Money = money;
- _shoppingCart = products;
- }
- public string Name { get; private set; }
- public decimal Money { get; private set; }
- public decimal GetTotalCost()
- {
- decimal totalCost = 0;
- foreach (Product product in _shoppingCart)
- {
- totalCost += product.Price;
- }
- return totalCost;
- }
- public void RemoveRandomProduct()
- {
- if (_shoppingCart.Count > 0)
- {
- int index = UserUtils.GenerateRandom(_shoppingCart.Count - 1);
- _shoppingCart.RemoveAt(index);
- }
- }
- public void Pay(decimal amount)
- {
- Money -= amount;
- }
- }
- class Supermarket
- {
- private Queue<Customer> _customers;
- private List<Product> _products;
- public Supermarket()
- {
- _customers = new Queue<Customer>();
- _products = new List<Product>
- {
- new Product("Молоко", 50),
- new Product("Хлеб", 30),
- new Product("Яйца", 70),
- new Product("Мясо", 200),
- new Product("Овощи", 80)
- };
- }
- public void Work()
- {
- Console.WriteLine("Добро пожаловать в супермаркет!");
- Console.WriteLine("Введите количество клиентов: ");
- if (int.TryParse(Console.ReadLine(), out int numCustomers))
- {
- CreateQueue(numCustomers);
- Console.WriteLine("\nНачинаем обслуживание клиентов:");
- ServeClients();
- }
- else
- {
- Console.WriteLine("Ошибка ввода!");
- }
- }
- private void CreateQueue(int numCustomers)
- {
- int _minCustomersBalance = 150;
- int _maxCustomersBalance = 1000;
- for (int i = 1; i <= numCustomers; i++)
- {
- List<Product> customerProducts = GetRandomProducts();
- Customer customer = new Customer($"Клиент {i}", UserUtils.GenerateRandom(_maxCustomersBalance, _minCustomersBalance), customerProducts);
- _customers.Enqueue(customer);
- }
- }
- private void ServeClients()
- {
- while (_customers.Count > 0)
- {
- Customer customer = _customers.Dequeue();
- Console.WriteLine($"\nТекущий клиент: {customer.Name}");
- Console.WriteLine($"Деньги клиента: {customer.Money}");
- decimal totalCost = customer.GetTotalCost();
- Console.WriteLine($"Сумма покупки: {totalCost}");
- while (customer.Money < totalCost)
- {
- customer.RemoveRandomProduct();
- totalCost = customer.GetTotalCost();
- Console.WriteLine($"Не хватает денег. Удален рандомный товар. Новая сумма покупки: {totalCost}");
- }
- if (totalCost == 0)
- {
- Console.WriteLine("Клиент не смог ничего купить.");
- }
- else
- {
- customer.Pay(totalCost);
- Console.WriteLine($"Оплата прошла успешно. Денег осталось: {customer.Money}");
- }
- }
- }
- private List<Product> GetRandomProducts()
- {
- List<Product> randomProducts = new List<Product>();
- int numProducts = UserUtils.GenerateRandom(_products.Count, 1);
- for (int i = 0; i < numProducts; i++)
- {
- Product product = _products[UserUtils.GenerateRandom(_products.Count - 1)];
- randomProducts.Add(product);
- }
- return randomProducts;
- }
- }
- class UserUtils
- {
- private static Random s_random = new Random();
- public static int GenerateRandom(int maxValue, int minValue = 0)
- {
- return s_random.Next(minValue, maxValue + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement