Advertisement
OldBeliver

Market_01

May 24th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace FoodMarket_01
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Cashdesk cashdesk = new Cashdesk();
  14.             cashdesk.Work();
  15.         }
  16.     }
  17.  
  18.     class Cashdesk
  19.     {
  20.         private Queue<Shopper> _shoppers;
  21.        
  22.         public Cashdesk()
  23.         {  
  24.             int shoppersCount = 10;
  25.             _shoppers = new Queue<Shopper>();
  26.             Random rand = new Random();
  27.  
  28.             CreateNewShopper(shoppersCount, rand);
  29.         }
  30.  
  31.         private void CreateNewShopper(int count, Random rand)
  32.         {
  33.             int lowMoney = 3000;
  34.             int maxMoney = 4000;
  35.  
  36.             for (int i = 0; i < count; i++)
  37.             {
  38.                 _shoppers.Enqueue(new Shopper(rand.Next(lowMoney, maxMoney), rand));
  39.             }
  40.         }
  41.  
  42.         public void Work()
  43.         {
  44.             string splitbar = "-------------------------------------";
  45.            
  46.             while (_shoppers.Count > 0)
  47.             {
  48.                 Console.WriteLine(splitbar);
  49.                 Console.WriteLine($"количество покупателей в очереди: {_shoppers.Count}");
  50.                 Console.WriteLine(splitbar);
  51.                 Shopper shopper = _shoppers.Dequeue();
  52.                 Console.WriteLine($"у кассы покупатель с {shopper.Money:# ###} монетами");
  53.                 Console.WriteLine(splitbar);
  54.                 Console.WriteLine($"у него в корзине: ");
  55.                 shopper.ViewCart();
  56.                 Console.WriteLine(splitbar);
  57.                 Console.WriteLine($"общая стоимость товаров в тележке: {shopper.CheckBill(): # ###} монет");
  58.                
  59.                 while(shopper.CheckBill() > shopper.Money)
  60.                 {
  61.                     int deficit = shopper.CheckBill() - shopper.Money;
  62.                     Console.WriteLine($"\nпокупателю нехватает {deficit:# ###} монет.\nнажмите чтобы удалить случайный товар из корзины.");
  63.                     Console.ReadKey();
  64.                     shopper.PutOut();
  65.                     Console.Clear();
  66.                     shopper.ViewCart();
  67.                 }
  68.  
  69.                 Console.WriteLine($"\nДовольный покупатель оплатил по чеку.");
  70.  
  71.                 Console.ReadKey();
  72.                 Console.Clear();
  73.             }
  74.  
  75.             Console.WriteLine($"покупатели закончились, можно сделать перерыв на чай");
  76.             Console.WriteLine($"нажмите любую чтобы завершить программу");
  77.             Console.ReadKey();
  78.         }
  79.     }
  80.  
  81.     class Shopper
  82.     {
  83.         private Cart _cart;
  84.         public int Money { get; private set; }
  85.        
  86.         public Shopper(int money, Random rand)
  87.         {
  88.             Money = money;
  89.             _cart = new Cart();
  90.  
  91.             _cart.CreateNewCart(rand);
  92.         }
  93.  
  94.         public void ViewCart()
  95.         {  
  96.             _cart.ViewProductsInCart();
  97.         }
  98.  
  99.         public void PutOut()
  100.         {
  101.             _cart.PutOutProduct();
  102.         }
  103.  
  104.         public int CheckBill()
  105.         {
  106.             return _cart.SumProducts();
  107.         }
  108.     }
  109.  
  110.     class Cart
  111.     {
  112.         private List<Product> _products;
  113.         private List<Product> _productsInCart;
  114.  
  115.         public Cart()
  116.         {
  117.             _products = new List<Product>();
  118.             _productsInCart = new List<Product>();
  119.  
  120.             LoadProducts();
  121.         }
  122.  
  123.         public void CreateNewCart(Random rand)
  124.         {
  125.             _productsInCart = new List<Product>();
  126.             int limit = 16;
  127.  
  128.             int size = rand.Next(1, limit);
  129.  
  130.             for (int i = 0; i < size; i++)
  131.             {
  132.                 _productsInCart.Add(_products[rand.Next(0, _products.Count)]);
  133.             }
  134.         }
  135.  
  136.         public void ViewProductsInCart()
  137.         {
  138.             for (int i = 0; i < _productsInCart.Count; i++)
  139.             {
  140.                 Console.Write($"{i + 1}. ");
  141.                 _productsInCart[i].ShowProduct();
  142.             }
  143.         }
  144.  
  145.         public int SumProducts()
  146.         {
  147.             int sum = 0;
  148.  
  149.             for (int i = 0; i < _productsInCart.Count; i++)
  150.             {
  151.                 sum += _productsInCart[i].Price;
  152.             }
  153.  
  154.             return sum;
  155.         }
  156.  
  157.         public void PutOutProduct()
  158.         {
  159.             Random rand = new Random();
  160.             int count = _productsInCart.Count;
  161.             int index = rand.Next(0, count);
  162.  
  163.             _productsInCart.RemoveAt(index);
  164.         }
  165.  
  166.         private void LoadProducts()
  167.         {
  168.             _products.Add(new Product("Ломанный грош юбилейный", 1000));
  169.             _products.Add(new Product("Стельки с подогревом", 255));
  170.             _products.Add(new Product("Электроластик", 160));
  171.             _products.Add(new Product("Накладка на руль", 318));
  172.             _products.Add(new Product("Лампа для шкафа", 466));
  173.             _products.Add(new Product("Плоская фляга", 495));
  174.             _products.Add(new Product("Круговой нож", 433));
  175.             _products.Add(new Product("Карты", 340));
  176.             _products.Add(new Product("Мини бритва", 264));
  177.             _products.Add(new Product("Спирограф", 431));
  178.             _products.Add(new Product("Спортивная бутылка", 418));
  179.             _products.Add(new Product("Карманные шахматы", 344));
  180.             _products.Add(new Product("Щетка-ролик", 402));
  181.             _products.Add(new Product("Зонт", 492));
  182.             _products.Add(new Product("Ножницы с лазерным прицелом", 356));
  183.             _products.Add(new Product("Шампур-вилка", 351));
  184.             _products.Add(new Product("RGB-лампа", 244));
  185.             _products.Add(new Product("Складное ведёрко", 486));
  186.             _products.Add(new Product("Слайсер для ананаса", 275));
  187.             _products.Add(new Product("Ледоступы", 450));
  188.             _products.Add(new Product("Дорожные столовые приборы", 235));
  189.             _products.Add(new Product("Газовый паяльник", 476));
  190.             _products.Add(new Product("Губная гармошка", 286));
  191.             _products.Add(new Product("Водонепроницаемая флешка", 395));
  192.             _products.Add(new Product("Дозатор для напитков", 210));
  193.             _products.Add(new Product("Умный кошелёк", 603));
  194.             _products.Add(new Product("Комплект для выжигания", 883));
  195.             _products.Add(new Product("Дерево Бонсай", 1200));
  196.             _products.Add(new Product("Бумага мягкая", 68));
  197.             _products.Add(new Product("Атомная батарейка", 999));
  198.         }
  199.     }
  200.  
  201.     class Product
  202.     {
  203.         public string Name { get; private set; }
  204.         public int Price { get; private set; }
  205.  
  206.         public Product(string name, int price)
  207.         {
  208.             Name = name;
  209.             Price = price;
  210.         }
  211.  
  212.         public void ShowProduct()
  213.         {
  214.             Console.WriteLine($"{Name} - {Price} монет");
  215.         }
  216.     }
  217. }
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement