Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Ijunior
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Supermarket supermarket = new Supermarket();
- supermarket.Work();
- }
- }
- class Supermarket
- {
- private List<Product> _products;
- private Queue<Customer> _customers;
- private int _money;
- public Supermarket()
- {
- _products = new List<Product>();
- FillProductList();
- _customers = new Queue<Customer>();
- AddCustomers();
- _money = 0;
- }
- public void Work()
- {
- const string CommandServeCustomer = "1";
- const string CommandAddCustomers = "2";
- const string CommandExit = "3";
- bool isWork = true;
- while (isWork)
- {
- Console.WriteLine($"Заработано: {_money}\n");
- Console.WriteLine($"В очереди клиентов: {_customers.Count}\n");
- Console.Write($"{CommandServeCustomer} - рассчитать клиента" +
- $"\n{CommandAddCustomers} - добавить клиентов" +
- $"\n{CommandExit} - выйти" +
- $"\nВведите номер: ");
- switch (Console.ReadLine())
- {
- case CommandServeCustomer:
- ServeCustomer();
- break;
- case CommandAddCustomers:
- AddCustomers();
- break;
- case CommandExit:
- isWork = false;
- break;
- default:
- Console.WriteLine("Некорректный ввод");
- break;
- }
- Console.Clear();
- }
- }
- private void FillProductList()
- {
- List<string> names = new List<string>()
- {
- "шоколад 'Twix'", "шоколад 'Bounty'", "шоколад 'Snickers'",
- "торт 'Полет'", "торт 'Медовик'", "торт 'Наполеон'",
- "зефир", "пастила", "халва", "конфеты",
- "чипсы 'Lays'", "чипсы 'Cheetos'", "чипсы 'Pringles'"
- };
- for (int i = 0; i < names.Count; i++)
- {
- _products.Add(new Product(names[i], GenerateCost()));
- }
- }
- private void AddCustomers()
- {
- int countCustomers = 10;
- for (int i = 0; i < countCustomers; i++)
- {
- List<Product> cart = new List<Product>();
- CreateCart(cart);
- _customers.Enqueue(new Customer(cart, GenerateMoney()));
- }
- }
- private void CreateCart(List<Product> cart)
- {
- int minIndex = 0;
- int maxIndex = _products.Count - 1;
- int minCountProducts = 1;
- int maxCountProducts = 10;
- int countProducts = Utils.GetRandomNumber(minCountProducts, maxCountProducts);
- for (int i = 0; i < countProducts; i++)
- {
- cart.Add(_products[Utils.GetRandomNumber(minIndex, maxIndex)]);
- }
- }
- private void ServeCustomer()
- {
- if (_customers.Count > 0)
- {
- Customer customer = _customers.Dequeue();
- customer.ShowInfo();
- Console.WriteLine("Нажмите любую клавишу, чтобы рассчитать клиента");
- Console.ReadLine();
- while (customer.IsMoneyEnough() == false)
- {
- customer.ShowInfo();
- customer.RemoveRandomProduct();
- }
- int sum = customer.Pay();
- _money += sum;
- customer.FillBag();
- customer.ClearCart();
- }
- else
- {
- Console.WriteLine("Клиентов нет");
- }
- }
- private int GenerateMoney()
- {
- int minMoney = 500;
- int maxMoney = 1000;
- return Utils.GetRandomNumber(minMoney, maxMoney);
- }
- private int GenerateCost()
- {
- int minMoney = 50;
- int maxMoney = 200;
- return Utils.GetRandomNumber(minMoney, maxMoney);
- }
- }
- class Product
- {
- private string _name;
- public Product(string name, int cost)
- {
- _name = name;
- Cost = cost;
- }
- public int Cost { get; }
- public void ShowInfo()
- {
- Console.WriteLine($"{_name} - {Cost} монет");
- }
- }
- class Customer
- {
- private List<Product> _cart;
- private List<Product> _bag;
- private int _money;
- public Customer(List<Product> cart, int money)
- {
- _cart = cart;
- _bag = new List<Product>();
- _money = money;
- }
- public void ShowInfo()
- {
- Console.Clear();
- Console.WriteLine("У покупателя: \n");
- foreach (Product product in _cart)
- {
- product.ShowInfo();
- }
- Console.WriteLine("\nСумма товаров: " + CountProductsSum() + "\n");
- }
- public int Pay()
- {
- int sum = CountProductsSum();
- _money -= sum;
- return sum;
- }
- public int CountProductsSum()
- {
- int sum = 0;
- foreach (Product product in _cart)
- {
- sum += product.Cost;
- }
- return sum;
- }
- public void FillBag()
- {
- for (int i = 0; i < _cart.Count; i++)
- {
- _bag.Add(_cart[i]);
- }
- }
- public void ClearCart()
- {
- _cart.Clear();
- }
- public bool IsMoneyEnough()
- {
- return _money >= CountProductsSum();
- }
- public void RemoveRandomProduct()
- {
- int minIndex = 0;
- int maxIndex = _cart.Count - 1;
- Product removedProduct = _cart[Utils.GetRandomNumber(minIndex, maxIndex)];
- _cart.Remove(removedProduct);
- Console.Write("У покупателя не хватает денег. Он удаляет из корзины: ");
- removedProduct.ShowInfo();
- Console.ReadKey();
- }
- }
- class Utils
- {
- private static Random s_random = new Random();
- public static int GetRandomNumber(int minValue, int maxValue)
- {
- return s_random.Next(minValue, maxValue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment