Advertisement
Torgach

tempMagazine

Apr 14th, 2021
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.19 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 supermarket
  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;
  21.  
  22.         public Supermarket()
  23.         {
  24.             _clients = new Queue<Client>();
  25.         }
  26.  
  27.         public void Work()
  28.         {
  29.             bool isRun = true;
  30.             SetQueue();
  31.  
  32.             while (isRun)
  33.             {
  34.                 Console.Write("[1] - Задать очередь\n[2] - Обслужить клиента\n[3] - Выход\n" +
  35.                     "Ввод: ");
  36.  
  37.                 switch (Console.ReadLine())
  38.                 {
  39.                     case "1":
  40.                         SetQueue();
  41.                         break;
  42.                     case "2":
  43.                         ServeCustomers();
  44.                         break;
  45.                     case "3":
  46.                         isRun = false;
  47.                         break;
  48.                     default:
  49.                         Console.WriteLine("Ошибка!");
  50.                         break;
  51.                 }
  52.             }
  53.         }
  54.  
  55.         private void SetQueue()
  56.         {
  57.             Console.Write("Задайте число клиентов или оставьте по умолчанию.\nВвод: ");
  58.             if (int.TryParse(Console.ReadLine(), out int number) == false)
  59.             {
  60.                 Console.WriteLine("Ошибка, устанавливаем по умолчанию...");
  61.                 number = 2;
  62.             }
  63.  
  64.             for (int i = 0; i < number; i++)
  65.             {
  66.                 Client newClient = new Client();
  67.                 _clients.Enqueue(newClient);
  68.             }
  69.             Console.Clear();
  70.         }
  71.  
  72.         private void ServeCustomers()
  73.         {
  74.             if (_clients.Count == 0)
  75.             {
  76.                 Console.WriteLine("Задайте число клиентов!");
  77.                 return;
  78.             }
  79.  
  80.             Console.WriteLine("\nОбслуживаем клиента...");
  81.  
  82.             while(_clients.Count > 0)
  83.             {
  84.                 Console.WriteLine("Деньги клиента: " + _clients.Peek().CurrentMoney);
  85.                 _clients.Peek().TryToPay();
  86.  
  87.                 Console.ReadKey(true);
  88.                 _clients.Dequeue();
  89.             }
  90.         }
  91.     }
  92.  
  93.     class Client
  94.     {
  95.         private List<int> _goods;
  96.         private static Random rand = new Random();
  97.  
  98.         public int CurrentMoney { get; private set; }
  99.  
  100.         public Client()
  101.         {
  102.             CurrentMoney = rand.Next(600, 5000);
  103.             _goods = new List<int>();
  104.  
  105.             for (int i = 0; i < rand.Next(1, 10); i++)
  106.             {
  107.                 _goods.Add(rand.Next(100, 1000));
  108.             }
  109.         }
  110.  
  111.         public void TryToPay()
  112.         {
  113.             while(_goods.Count > 0)
  114.             {
  115.                 Console.WriteLine("Сумма итоговой покупки: " + GetSum());
  116.                 if (CurrentMoney > GetSum())
  117.                 {
  118.                     Console.WriteLine("Коризна оплачена.\n");
  119.                     return;
  120.                 }
  121.                 else
  122.                 {
  123.                     Console.WriteLine("Не хватает денег заплатить за товар! Освобождаем корзину...");
  124.                     DeleteGoods();
  125.                 }
  126.             }
  127.  
  128.             if(_goods.Count == 0)
  129.             {
  130.                 Console.WriteLine("Покупка НЕ оплачена, клиенту приходится уйти из очереди!\n");
  131.             }
  132.         }
  133.  
  134.         public int GetSum()
  135.         {
  136.             return _goods.Sum();
  137.         }
  138.  
  139.         private void DeleteGoods()
  140.         {
  141.             int number = rand.Next(0, _goods.Count);
  142.  
  143.             Console.WriteLine("Убираем товар со стоимостью " + _goods[number] + "\n");
  144.             _goods.RemoveAt(number);
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement