lovelyvook

Unit_45

Aug 2nd, 2024 (edited)
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Ijunior
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Supermarket supermarket = new Supermarket();
  11.             supermarket.Work();
  12.         }
  13.     }
  14.  
  15.     class Supermarket
  16.     {
  17.         private List<Product> _products;
  18.         private Queue<Customer> _customers;
  19.         private int _money;
  20.  
  21.         public Supermarket()
  22.         {
  23.             _products = new List<Product>();
  24.             FillProductList();
  25.             _customers = new Queue<Customer>();
  26.             AddCustomers();
  27.             _money = 0;
  28.         }
  29.  
  30.         public void Work()
  31.         {
  32.             const string CommandServeCustomer = "1";
  33.             const string CommandAddCustomers = "2";
  34.             const string CommandExit = "3";
  35.  
  36.             bool isWork = true;
  37.  
  38.             while (isWork)
  39.             {
  40.                 Console.WriteLine($"Заработано: {_money}\n");
  41.                 Console.WriteLine($"В очереди клиентов: {_customers.Count}\n");
  42.  
  43.                 Console.Write($"{CommandServeCustomer} - рассчитать клиента" +
  44.                               $"\n{CommandAddCustomers} - добавить клиентов" +
  45.                               $"\n{CommandExit} - выйти" +
  46.                               $"\nВведите номер: ");
  47.  
  48.                 switch (Console.ReadLine())
  49.                 {
  50.                     case CommandServeCustomer:
  51.                         ServeCustomer();
  52.                         break;
  53.  
  54.                     case CommandAddCustomers:
  55.                         AddCustomers();
  56.                         break;
  57.  
  58.                     case CommandExit:
  59.                         isWork = false;
  60.                         break;
  61.  
  62.                     default:
  63.                         Console.WriteLine("Некорректный ввод");
  64.                         break;
  65.                 }
  66.  
  67.                 Console.Clear();
  68.             }
  69.         }
  70.  
  71.         private void FillProductList()
  72.         {
  73.             List<string> names = new List<string>()
  74.             {
  75.  
  76.                 "шоколад 'Twix'", "шоколад 'Bounty'", "шоколад 'Snickers'",
  77.                 "торт 'Полет'", "торт 'Медовик'", "торт 'Наполеон'",
  78.                 "зефир", "пастила", "халва", "конфеты",
  79.                 "чипсы 'Lays'", "чипсы 'Cheetos'", "чипсы 'Pringles'"
  80.             };
  81.  
  82.             for (int i = 0; i < names.Count; i++)
  83.             {
  84.                 _products.Add(new Product(names[i], GenerateCost()));
  85.             }
  86.         }
  87.  
  88.         private void AddCustomers()
  89.         {
  90.             int countCustomers = 10;
  91.  
  92.             for (int i = 0; i < countCustomers; i++)
  93.             {
  94.                 List<Product> cart = new List<Product>();
  95.                 CreateCart(cart);
  96.                 _customers.Enqueue(new Customer(cart, GenerateMoney()));
  97.             }
  98.         }
  99.  
  100.         private void CreateCart(List<Product> cart)
  101.         {
  102.             int minIndex = 0;
  103.             int maxIndex = _products.Count - 1;
  104.             int minCountProducts = 1;
  105.             int maxCountProducts = 10;
  106.             int countProducts = Utils.GetRandomNumber(minCountProducts, maxCountProducts);
  107.  
  108.             for (int i = 0; i < countProducts; i++)
  109.             {
  110.                 cart.Add(_products[Utils.GetRandomNumber(minIndex, maxIndex)]);
  111.             }
  112.         }
  113.  
  114.         private void ServeCustomer()
  115.         {
  116.             if (_customers.Count > 0)
  117.             {
  118.                 Customer customer = _customers.Dequeue();
  119.                 customer.ShowInfo();
  120.  
  121.                 Console.WriteLine("Нажмите любую клавишу, чтобы рассчитать клиента");
  122.                 Console.ReadLine();
  123.  
  124.                 while (customer.IsMoneyEnough() == false)
  125.                 {
  126.                     customer.ShowInfo();
  127.                     customer.RemoveRandomProduct();
  128.                 }
  129.  
  130.                 int sum = customer.Pay();
  131.                 _money += sum;
  132.  
  133.                 customer.FillBag();
  134.                 customer.ClearCart();
  135.             }
  136.             else
  137.             {
  138.                 Console.WriteLine("Клиентов нет");
  139.             }
  140.         }
  141.  
  142.         private int GenerateMoney()
  143.         {
  144.             int minMoney = 500;
  145.             int maxMoney = 1000;
  146.  
  147.             return Utils.GetRandomNumber(minMoney, maxMoney);
  148.         }
  149.  
  150.         private int GenerateCost()
  151.         {
  152.             int minMoney = 50;
  153.             int maxMoney = 200;
  154.  
  155.             return Utils.GetRandomNumber(minMoney, maxMoney);
  156.         }
  157.     }
  158.  
  159.     class Product
  160.     {
  161.         private string _name;
  162.  
  163.         public Product(string name, int cost)
  164.         {
  165.             _name = name;
  166.             Cost = cost;
  167.         }
  168.  
  169.         public int Cost { get; }
  170.  
  171.         public void ShowInfo()
  172.         {
  173.             Console.WriteLine($"{_name} - {Cost} монет");
  174.         }
  175.     }
  176.  
  177.     class Customer
  178.     {
  179.         private List<Product> _cart;
  180.         private List<Product> _bag;
  181.         private int _money;
  182.  
  183.         public Customer(List<Product> cart, int money)
  184.         {
  185.             _cart = cart;
  186.             _bag = new List<Product>();
  187.             _money = money;
  188.         }
  189.  
  190.         public void ShowInfo()
  191.         {
  192.             Console.Clear();
  193.             Console.WriteLine("У покупателя: \n");
  194.  
  195.             foreach (Product product in _cart)
  196.             {
  197.                 product.ShowInfo();
  198.             }
  199.  
  200.             Console.WriteLine("\nСумма товаров: " + CountProductsSum() + "\n");
  201.         }
  202.  
  203.         public int Pay()
  204.         {
  205.             int sum = CountProductsSum();
  206.             _money -= sum;
  207.  
  208.             return sum;
  209.         }
  210.  
  211.         public int CountProductsSum()
  212.         {
  213.             int sum = 0;
  214.  
  215.             foreach (Product product in _cart)
  216.             {
  217.                 sum += product.Cost;
  218.             }
  219.  
  220.             return sum;
  221.         }
  222.  
  223.         public void FillBag()
  224.         {
  225.             for (int i = 0; i < _cart.Count; i++)
  226.             {
  227.                 _bag.Add(_cart[i]);
  228.             }
  229.         }
  230.  
  231.         public void ClearCart()
  232.         {
  233.             _cart.Clear();
  234.         }
  235.  
  236.         public bool IsMoneyEnough()
  237.         {
  238.             return _money >= CountProductsSum();
  239.         }
  240.  
  241.         public void RemoveRandomProduct()
  242.         {
  243.             int minIndex = 0;
  244.             int maxIndex = _cart.Count - 1;
  245.  
  246.             Product removedProduct = _cart[Utils.GetRandomNumber(minIndex, maxIndex)];
  247.             _cart.Remove(removedProduct);
  248.  
  249.             Console.Write("У покупателя не хватает денег. Он удаляет из корзины: ");
  250.             removedProduct.ShowInfo();
  251.             Console.ReadKey();
  252.         }
  253.     }
  254.  
  255.     class Utils
  256.     {
  257.         private static Random s_random = new Random();
  258.  
  259.         public static int GetRandomNumber(int minValue, int maxValue)
  260.         {
  261.             return s_random.Next(minValue, maxValue);
  262.         }
  263.     }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment