Advertisement
RedFlys

Home work - supermarket

Jan 4th, 2022
1,196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.53 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 Home_Work
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             Supermarket supermarket = new Supermarket();
  14.             supermarket.Work();
  15.         }
  16.     }
  17.  
  18.     class Supermarket
  19.     {
  20.         private List<Product> _assortment;
  21.         private Queue<Client> _clients;
  22.         private int _cash;
  23.  
  24.         public Supermarket()
  25.         {
  26.             _assortment = new List<Product>();
  27.             _clients = new Queue<Client>();
  28.  
  29.             TakeDeliveryOfProducts();
  30.             GenerateQueue();
  31.         }
  32.  
  33.         public void Work()
  34.         {
  35.             while (_clients.Count > 0)
  36.             {
  37.                 Client client = _clients.Dequeue();
  38.                 bool isPaidOfPurchase = false;
  39.  
  40.                 while (isPaidOfPurchase == false)
  41.                 {
  42.                     ShowInfo();
  43.                     client.ShowInfo();
  44.                     Console.WriteLine();
  45.  
  46.                     isPaidOfPurchase = TrySell(client);
  47.  
  48.                     Console.ReadKey();
  49.                     Console.Clear();
  50.                 }
  51.             }
  52.  
  53.             Console.WriteLine("Пока магазин работал, вы заработали " + _cash + " рублей.");
  54.             Console.WriteLine("Вы хорошо поработали, идите домой.");
  55.         }
  56.  
  57.         private bool TrySell(Client client)
  58.         {
  59.             if (client.IsEmptyBasket == true)
  60.             {
  61.                 Console.WriteLine("Клиент отказался от всех продуктов и ушёл.");
  62.  
  63.                return true;
  64.             }
  65.             else if (client.IsSolvent == true)
  66.             {
  67.                 Console.WriteLine("Клиент оплачивает покупку на " + client.Purchase + " рублей.");
  68.  
  69.                 _cash += client.Buy();
  70.  
  71.                 Console.WriteLine("У клиента осталось " + client.Money + " рублей.");
  72.  
  73.                 return true;
  74.             }
  75.             else
  76.             {
  77.                 Console.WriteLine("Клиенту не хватает денег, что бы оплатить покупку.");
  78.                 client.RemoveProductFromBasket();
  79.  
  80.                 return false;
  81.             }
  82.         }
  83.  
  84.         private void ShowInfo()
  85.         {
  86.             int topPosition = 0;
  87.             int leftPosition = 50;
  88.  
  89.             Console.SetCursorPosition(leftPosition, topPosition++);
  90.             Console.WriteLine("В очереди " + _clients.Count + " клиентов.");
  91.  
  92.             Console.SetCursorPosition(leftPosition, topPosition);
  93.             Console.WriteLine("В кассе " + _cash + " рублей.");
  94.         }
  95.  
  96.         private void TakeDeliveryOfProducts()
  97.         {
  98.             _assortment.Add(new Product("Апельсин", 40));
  99.             _assortment.Add(new Product("Молоко", 80));
  100.             _assortment.Add(new Product("Пепси", 120));
  101.             _assortment.Add(new Product("Крем для загара", 80));
  102.             _assortment.Add(new Product("Шашлык из свинины", 100));
  103.             _assortment.Add(new Product("Яблоко", 30));
  104.             _assortment.Add(new Product("Гель для душа", 70));
  105.             _assortment.Add(new Product("Творог", 60));
  106.             _assortment.Add(new Product("Гречка", 65));
  107.             _assortment.Add(new Product("Рис", 55));
  108.             _assortment.Add(new Product("Сникерс", 50));
  109.             _assortment.Add(new Product("Кит-кат", 90));
  110.         }
  111.  
  112.         private void GenerateQueue()
  113.         {
  114.             int countClients = 20;
  115.  
  116.             for (int i = 0; i < countClients; i++)
  117.             {
  118.                 _clients.Enqueue(new Client(_assortment));
  119.             }
  120.         }
  121.     }
  122.  
  123.     class Client
  124.     {
  125.         private static Random _random;
  126.  
  127.         private List<Product> _basket;
  128.         private int _money;
  129.         private int _purchase;
  130.  
  131.         static Client()
  132.         {
  133.             _random = new Random();
  134.         }
  135.  
  136.         public Client(List<Product> assortment)
  137.         {
  138.             int maxMoney = 1000;
  139.             int minMoney = 100;
  140.  
  141.             _money = _random.Next(minMoney, maxMoney);
  142.             _basket = new List<Product>();
  143.             FillBasket(assortment);
  144.         }
  145.  
  146.         public int Money => _money;
  147.         public int Purchase => _purchase;
  148.         public bool IsSolvent => _money >= _purchase;
  149.         public bool IsEmptyBasket => _basket.Count <= 0;
  150.  
  151.         public void ShowInfo()
  152.         {
  153.             int index = 1;
  154.             SumPurchaseBasket();
  155.             Console.SetCursorPosition(0, 0);
  156.  
  157.             Console.WriteLine("Кол-во денег у клиента: " + _money);
  158.             Console.WriteLine("Товары в корзине:");
  159.  
  160.             foreach (Product product in _basket)
  161.             {
  162.                 Console.WriteLine(index + ". " + product.Name);
  163.  
  164.                 index++;
  165.             }
  166.  
  167.             Console.WriteLine("Сумма всех товаров: " + _purchase);
  168.         }
  169.  
  170.         public void RemoveProductFromBasket()
  171.         {
  172.             int randomProductIndex = _random.Next(_basket.Count);
  173.  
  174.             Console.WriteLine("Клиент убрал из корзины " + _basket[randomProductIndex].Name);
  175.             _basket.RemoveAt(randomProductIndex);
  176.         }
  177.  
  178.         public int Buy()
  179.         {
  180.             _money -= _purchase;
  181.  
  182.             return _purchase;
  183.         }
  184.  
  185.         private void SumPurchaseBasket()
  186.         {
  187.             _purchase = 0;
  188.  
  189.             foreach(Product product in _basket)
  190.             {
  191.                 _purchase += product.Price;
  192.             }
  193.         }
  194.  
  195.         private void FillBasket(List<Product> assortment)
  196.         {
  197.             int minCountProduct = 3;
  198.             int maxCountProduct = 10;
  199.            
  200.  
  201.             int countProductInBacket = _random.Next(minCountProduct, maxCountProduct);
  202.  
  203.             for(int i = 0; i < countProductInBacket; i++)
  204.             {
  205.                 _basket.Add(assortment[_random.Next(assortment.Count)]);
  206.             }
  207.         }
  208.     }
  209.  
  210.     class Product
  211.     {
  212.         private string _name;
  213.         private int _price;
  214.  
  215.         public Product(string name, int price)
  216.         {
  217.             _name = name;
  218.             _price = price;
  219.         }
  220.  
  221.         public string Name => _name;
  222.         public int Price => _price;
  223.     }    
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement