Advertisement
TeT91

ДЗ Супермаркет

Jun 6th, 2024 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSLight
  5. {
  6. internal class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. Supermarket shop = new Supermarket();
  11. shop.ServeQueue();
  12. }
  13. }
  14.  
  15. class Supermarket
  16. {
  17. private Queue<Buyer> _buyers = new Queue<Buyer>();
  18. private List<Product> _products = new List<Product>();
  19.  
  20. private int _money = 0;
  21.  
  22. public Supermarket()
  23. {
  24. InitProducts();
  25. InitBuyers();
  26. }
  27.  
  28. private void InitProducts()
  29. {
  30. int maxProductPrice = 100;
  31. int minProductPrice = 10;
  32. _products.Add(new Product("Product1", UserUtils.GetRandomNumber(minProductPrice, maxProductPrice)));
  33. _products.Add(new Product("Product2", UserUtils.GetRandomNumber(minProductPrice, maxProductPrice)));
  34. _products.Add(new Product("Product3", UserUtils.GetRandomNumber(minProductPrice, maxProductPrice)));
  35. _products.Add(new Product("Product4", UserUtils.GetRandomNumber(minProductPrice, maxProductPrice)));
  36. _products.Add(new Product("Product5", UserUtils.GetRandomNumber(minProductPrice, maxProductPrice)));
  37. _products.Add(new Product("Product6", UserUtils.GetRandomNumber(minProductPrice, maxProductPrice)));
  38. _products.Add(new Product("Product7", UserUtils.GetRandomNumber(minProductPrice, maxProductPrice)));
  39. _products.Add(new Product("Product8", UserUtils.GetRandomNumber(minProductPrice, maxProductPrice)));
  40. _products.Add(new Product("Product9", UserUtils.GetRandomNumber(minProductPrice, maxProductPrice)));
  41. _products.Add(new Product("Product10", UserUtils.GetRandomNumber(minProductPrice, maxProductPrice)));
  42. }
  43.  
  44. public void ServeQueue()
  45. {
  46. while (_buyers.Count > 0)
  47. {
  48. Trade();
  49. }
  50.  
  51. Console.WriteLine("Очередь закончилась");
  52. }
  53.  
  54. private void InitBuyers()
  55. {
  56. int minBuyers = 7;
  57. int maxBuyers = 20;
  58. int buyers = UserUtils.GetRandomNumber(minBuyers, maxBuyers);
  59.  
  60. for (int i = 0; i < buyers; i++)
  61. {
  62. _buyers.Enqueue(CreateBuyer());
  63. }
  64. }
  65.  
  66. private Buyer CreateBuyer()
  67. {
  68. Buyer buyer = new Buyer();
  69.  
  70. for (int i = 0; i < buyer.ProductsNeededCout; i++)
  71. {
  72. int productId = UserUtils.GetRandomNumber(0, _products.Count - 1);
  73. buyer.AddProductToBasket(_products[productId]);
  74. }
  75.  
  76. return buyer;
  77. }
  78.  
  79. private void Trade()
  80. {
  81. bool isTrading = true;
  82. Buyer buyer = _buyers.Dequeue();
  83.  
  84. Console.WriteLine($"В очереди еще {_buyers.Count} человек");
  85.  
  86. while (isTrading)
  87. {
  88. Console.WriteLine($"У покупателя {buyer.Money} денег");
  89. Console.WriteLine("Его корзина:");
  90. buyer.ShowProductsInBasket();
  91. Console.WriteLine($"Общая стоимость {buyer.CalculateTotalPrice()}");
  92.  
  93. if (buyer.TryPayProducts())
  94. {
  95. buyer.ExchangeProductFromBasketToInventory();
  96. _money += buyer.CalculateTotalPrice();
  97. isTrading = false;
  98. }
  99. else
  100. {
  101. buyer.RemoveRandomProduct();
  102. }
  103.  
  104. Console.ReadKey();
  105. }
  106.  
  107. if (buyer.GetProductsInInventoryCount() == 0)
  108. {
  109. Console.WriteLine("У покупателя не хватло денег ни на что");
  110. }
  111.  
  112. Console.WriteLine("Покупатель ушел");
  113. }
  114. }
  115.  
  116. class Buyer
  117. {
  118. private List<Product> _basket = new List<Product>();
  119. private List<Product> _inventory = new List<Product>();
  120.  
  121. private int _minProductsNeededCount = 1;
  122. private int _maxProductsNeededCount = 10;
  123.  
  124. public Buyer()
  125. {
  126. int maxPossibleMoney = 350;
  127. int minPossibleMoney = 10;
  128. Money = UserUtils.GetRandomNumber(minPossibleMoney, maxPossibleMoney);
  129.  
  130. ProductsNeededCout = UserUtils.GetRandomNumber(_minProductsNeededCount, _maxProductsNeededCount);
  131. }
  132.  
  133. public int Money { get; private set; }
  134.  
  135. public int ProductsNeededCout { get; private set; }
  136.  
  137. public void RemoveRandomProduct()
  138. {
  139. if (_basket.Count > 0)
  140. {
  141. _basket.RemoveAt(UserUtils.GetRandomNumber(0, _basket.Count - 2));
  142. }
  143. else
  144. {
  145. _basket.Clear();
  146. }
  147. }
  148.  
  149. public void ShowProductsInBasket()
  150. {
  151. foreach (Product product in _basket)
  152. {
  153. Console.WriteLine($"{product.Name} - {product.Price}");
  154. }
  155. }
  156.  
  157. public bool TryPayProducts()
  158. {
  159. if (CalculateTotalPrice() > Money)
  160. {
  161. return false;
  162. }
  163. else
  164. {
  165. Money -= CalculateTotalPrice();
  166. return true;
  167. }
  168. }
  169.  
  170. public void ExchangeProductFromBasketToInventory()
  171. {
  172. Product tempProduct;
  173.  
  174. foreach (Product product in _basket)
  175. {
  176. tempProduct = product;
  177. _inventory.Add(tempProduct);
  178. }
  179.  
  180. _basket.Clear();
  181. Console.WriteLine(_inventory.Count);
  182. }
  183.  
  184. public int GetProductsInInventoryCount()
  185. {
  186. return _inventory.Count;
  187. }
  188.  
  189. public void AddProductToBasket(Product product)
  190. {
  191. _basket.Add(product);
  192. }
  193.  
  194. public int CalculateTotalPrice()
  195. {
  196. int totalPrice = 0;
  197.  
  198. foreach (Product product in _basket)
  199. {
  200. totalPrice += product.Price;
  201. }
  202.  
  203. return totalPrice;
  204. }
  205. }
  206.  
  207. class Product
  208. {
  209. public Product(string name, int price)
  210. {
  211. Name = name;
  212. Price = price;
  213. }
  214.  
  215. public string Name { get; private set; }
  216. public int Price { get; private set; }
  217. }
  218.  
  219. class UserUtils
  220. {
  221. private static Random s_random = new Random();
  222.  
  223. public static int GetRandomNumber(int minValue, int maxValue)
  224. {
  225. return s_random.Next(minValue, maxValue + 1);
  226. }
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement