Advertisement
illiden

Supermarket

Jul 15th, 2020
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 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 CSLight
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Supermarket supermarket = new Supermarket();
  14.             supermarket.Work();
  15.         }
  16.     }
  17.  
  18.     class Supermarket
  19.     {
  20.         private Queue<Client> _clients = new Queue<Client>();
  21.         private int _money = 0;
  22.  
  23.         public Supermarket()
  24.         {
  25.             Random random = new Random();
  26.             for (int i = 0; i < random.Next(10, 15); i++)
  27.             {
  28.                 _clients.Enqueue(new Client(random));
  29.             }
  30.         }
  31.  
  32.         public void Work()
  33.         {
  34.             while (_clients.Count > 0)
  35.             {
  36.                 Console.WriteLine($"У вас в очереди {_clients.Count} клиентов, а в кассе {_money} монет. Нажмите любую клавишу, чтобы принять клиента");
  37.                 Console.ReadKey();
  38.                 Client client = _clients.Dequeue();
  39.                 Console.WriteLine($"Подошел клиент с {client.GetBasketSize()} товарами");
  40.                 while (!client.CheckSolvency())
  41.                 {
  42.                     Console.WriteLine($"Вы пробили все товары на сумму {client.CalculateCost()} монет, но у клиента не хватает денег. Нажмите любую клавишу, чтобы попросить клиента выложить любой товар");
  43.                     Console.ReadKey();
  44.                     client.RefuseProduct();
  45.                     Console.WriteLine($"У клиента осталось {client.GetBasketSize()} товаров");
  46.                 }
  47.                 int money = client.ToPay();
  48.                 _money += money;
  49.                 Console.WriteLine($"Клиент оплатил все товары. Вы получили {money} монет. Нажмите любую клавишу, чтобы выдать чек и попрощаться");
  50.                 Console.ReadKey();
  51.                 Console.Clear();
  52.             }
  53.         }
  54.     }
  55.  
  56.     class Client
  57.     {
  58.         private List<Product> _products = new List<Product>();
  59.         private int _money;
  60.         private int _totalCost;
  61.         private Random _random;
  62.  
  63.         public Client(Random random)
  64.         {
  65.             _random = random;
  66.             _money = _random.Next(500, 1000);
  67.             for (int i = 0; i < _random.Next(5, 10); i++)
  68.             {
  69.                 _products.Add(new Product(_random));
  70.             }
  71.         }
  72.  
  73.         public int CalculateCost()
  74.         {
  75.             _totalCost = 0;
  76.             for (int i = 0; i < _products.Count; i++)
  77.             {
  78.                 _totalCost += _products[i].Cost;
  79.             }
  80.             return _totalCost;
  81.         }
  82.  
  83.         public bool CheckSolvency()
  84.         {
  85.             CalculateCost();
  86.             if (_totalCost <= _money)
  87.             {
  88.                 return true;
  89.             }
  90.             else
  91.             {
  92.                 return false;
  93.             }
  94.         }
  95.  
  96.         public int ToPay()
  97.         {
  98.             _money -= _totalCost;
  99.             return _totalCost;
  100.         }
  101.  
  102.         public void RefuseProduct()
  103.         {
  104.             int randomNumber = _random.Next(0, _products.Count);
  105.             _products.RemoveAt(randomNumber);
  106.         }
  107.  
  108.         public int GetBasketSize()
  109.         {
  110.             return _products.Count;
  111.         }
  112.     }
  113.  
  114.     class Product
  115.     {
  116.         public int Cost { get; private set; }
  117.  
  118.         public Product(Random random)
  119.         {
  120.             Cost = random.Next(100, 200);
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement