Advertisement
MaoChessy

Task - 34

Nov 14th, 2020 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace C_sharp_Light
  5. {
  6.     class Program
  7.     {
  8.         public static void Main()
  9.         {
  10.             new Shop().Work();
  11.         }
  12.     }
  13.     public static class RandomStatic
  14.     {
  15.         static private Random _rand = new Random();
  16.         static public int GetNext(int min, int max)
  17.         {
  18.             return _rand.Next(min, max);
  19.         }
  20.     }
  21.     public static class Messager
  22.     {
  23.         static public void ShowMessageWithColor(string message, ConsoleColor color, bool delay)
  24.         {
  25.             ConsoleColor defaultColor = Console.ForegroundColor;
  26.             Console.ForegroundColor = color;
  27.             Console.WriteLine(message);
  28.             Console.ForegroundColor = defaultColor;
  29.             if (delay)
  30.                 Console.ReadKey();
  31.         }
  32.     }
  33.     public class Shop
  34.     {
  35.         private int _money = 0;
  36.         private Queue<Buyer> _buyers = new Queue<Buyer>();
  37.         public Shop()
  38.         {
  39.             for (int i = 0; i < 20; i++)
  40.             {
  41.                 _buyers.Enqueue(new Buyer());
  42.             }
  43.         }
  44.         public void Work()
  45.         {
  46.             while (_buyers.Count > 0)
  47.             {
  48.                 Console.Clear();
  49.                 Messager.ShowMessageWithColor($"Покупателей осталось - {_buyers.Count} \nДенег у магазина - {_money}\n", ConsoleColor.Yellow, false);
  50.                 _money += _buyers.Dequeue().GetPay();
  51.                 Console.Write($"\n\nEnter - вызов следующего покупателя");
  52.                 Console.ReadKey(true);
  53.             }
  54.         }
  55.     }
  56.     public class Buyer
  57.     {
  58.         private int _money;
  59.         private List<Item> _purchases = new List<Item>();
  60.         public Buyer()
  61.         {
  62.             _money = RandomStatic.GetNext(200, 1300);
  63.             int amountPurchases = RandomStatic.GetNext(1, 10);
  64.             for (int i = 0; i < amountPurchases; i++)
  65.             {
  66.                 _purchases.Add(new Item());
  67.             }
  68.         }
  69.         public int GetPay()
  70.         {
  71.             while (_purchases.Count > 0)
  72.             {
  73.                 int costAllProduscts = GetCostAllProducts();
  74.                 Messager.ShowMessageWithColor($"Цена покупоков составляет - {costAllProduscts}", ConsoleColor.White, false);
  75.                 if (ChekSolvency())
  76.                 {
  77.                     _money -= costAllProduscts;
  78.                     Messager.ShowMessageWithColor($"Покупатель сделал покупку на {costAllProduscts} денег", ConsoleColor.Red, false);
  79.                     return costAllProduscts;
  80.                 }
  81.                 else
  82.                 {
  83.                     RemoveProducts();
  84.                 }
  85.             }
  86.             Messager.ShowMessageWithColor("Покупатель ничего не купил.", ConsoleColor.Red, false);
  87.             return 0;
  88.         }
  89.         private bool ChekSolvency()
  90.         {
  91.             return GetCostAllProducts() <= _money;
  92.         }
  93.         private void RemoveProducts()
  94.         {
  95.             while (!ChekSolvency())
  96.             {
  97.                 int indexProductForRemove = RandomStatic.GetNext(0, _purchases.Count);
  98.                 Messager.ShowMessageWithColor($"Покупатель выложил из корзины - {_purchases[indexProductForRemove].GetInfo()}", ConsoleColor.Yellow, false);
  99.                 _purchases.RemoveAt(indexProductForRemove);
  100.             }
  101.         }
  102.         private int GetCostAllProducts()
  103.         {
  104.             int result = 0;
  105.             for (int i = 0; i < _purchases.Count; i++)
  106.             {
  107.                 result += _purchases[i].Cost;
  108.             }
  109.             return result;
  110.         }
  111.     }
  112.     public class Item
  113.     {
  114.         public int Cost { get; private set; }
  115.         public string Name { get; private set; }
  116.         public Item()
  117.         {
  118.             Cost = RandomStatic.GetNext(140, 500);
  119.             Name = new string[] { "Хлеб", "Молоко", "Мясо", "Шоколад", "Кофе" }[RandomStatic.GetNext(0, 5)];
  120.         }
  121.         public string GetInfo()
  122.         {
  123.             return $"{Name} за {Cost}";
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement