SaNik74

Supermarket

Aug 31st, 2024 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. using System.ComponentModel.DataAnnotations;
  2.  
  3. namespace Supermarket
  4. {
  5. internal class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Supermarket supermarket = new Supermarket();
  10.  
  11. supermarket.Work();
  12. }
  13. }
  14.  
  15. class Supermarket
  16. {
  17. private int _money = 0;
  18. private List<Product> _products = new List<Product>();
  19. private Queue<Client> _clients = new Queue<Client>();
  20.  
  21. public Supermarket()
  22. {
  23. _products.Add(new Product("Кофе", 2500));
  24. _products.Add(new Product("Хлопья", 50));
  25. _products.Add(new Product("Сахар", 100));
  26. _products.Add(new Product("Печенье", 150));
  27. _products.Add(new Product("Хлеб", 100));
  28. _products.Add(new Product("Сок", 200));
  29. }
  30.  
  31. public void Work()
  32. {
  33. CreateQueue();
  34.  
  35. while (_clients.Count > 0)
  36. {
  37. Console.WriteLine($"Добро пожаловать в симулятор супермаркета.\n" +
  38. $"Баланс магазина {_money}\n" +
  39. $"Ассортимент магазина:");
  40.  
  41. ShowProducts();
  42.  
  43. Console.WriteLine("Список клиентов в очереди.");
  44.  
  45. ShowClients();
  46.  
  47. Console.WriteLine("\nНажмите на любую клавишу для осуществления первой покупки.");
  48. Console.ReadKey();
  49.  
  50. ServiceFirstClientInQueue();
  51.  
  52. Console.ReadKey();
  53. Console.Clear();
  54. }
  55.  
  56. Console.WriteLine($"Рабочий день закончен. Баланс магазина {_money}");
  57. Console.ReadKey();
  58. }
  59.  
  60. private void ServiceFirstClientInQueue()
  61. {
  62. int moneyNeed;
  63.  
  64. Client firstClientInQueue = _clients.Peek();
  65.  
  66. firstClientInQueue.ThrowProductIfMoneyNotEnough();
  67.  
  68. moneyNeed = firstClientInQueue.CalculateCartCost();
  69.  
  70. firstClientInQueue.AddProductsInBag();
  71.  
  72. firstClientInQueue.DeductMoney(moneyNeed);
  73.  
  74. TakeMoney(moneyNeed);
  75.  
  76. _clients.Dequeue();
  77. }
  78.  
  79. private void CreateQueue()
  80. {
  81. Random random = new Random();
  82.  
  83. int numberOfMinClients = 1;
  84. int numberOfMaxClients = 25;
  85.  
  86. int clientsInQueue = UserUnits.GenerateRandomNumber(numberOfMinClients, numberOfMaxClients);
  87.  
  88. for (int i = 0; i < clientsInQueue; i++)
  89. {
  90. AddClientInQueue();
  91. }
  92.  
  93. foreach (Client client in _clients)
  94. {
  95. client.AddProductsInCart(new List<Product>(_products));
  96. client.GiveRandomBalance();
  97. }
  98. }
  99.  
  100. private void AddClientInQueue()
  101. {
  102. _clients.Enqueue(new Client());
  103. }
  104.  
  105. private void ShowProducts()
  106. {
  107. foreach (Product product in _products)
  108. {
  109. Console.WriteLine($"{product.Name} - {product.Price} руб.");
  110. }
  111. }
  112.  
  113. private void ShowClients()
  114. {
  115. int numberClientInQueue = 1;
  116.  
  117. foreach (Client client in _clients)
  118. {
  119. Console.WriteLine($"{numberClientInQueue} клиент. Баланс {client.Money}");
  120.  
  121. numberClientInQueue++;
  122. }
  123. }
  124.  
  125. private void TakeMoney(int money)
  126. {
  127. _money += money;
  128. }
  129. }
  130.  
  131. class Product
  132. {
  133. public Product(string name, int price)
  134. {
  135. Name = name;
  136. Price = price;
  137. }
  138.  
  139. public string Name { get; private set; }
  140. public int Price { get; private set; }
  141. }
  142.  
  143. class Client
  144. {
  145. private int _money;
  146. private List<Product> _productsInBag = new List<Product>();
  147. private List<Product> _productsInCart = new List<Product>();
  148.  
  149. public int Money => _money;
  150.  
  151. public int CountProducts => _productsInBag.Count;
  152.  
  153. public void AddProductsInBag()
  154. {
  155. int numberOfProducts = CountProducts;
  156.  
  157. for (int i = 0; numberOfProducts > i; i++)
  158. {
  159. ShowInfo();
  160.  
  161. Console.WriteLine("Нажмите на любую клавишу, чтобы переложить товар.\n");
  162. Console.ReadKey();
  163.  
  164. _productsInBag.Add(Transfer());
  165.  
  166. Console.WriteLine();
  167. Console.WriteLine("Продукты в сумке:\n");
  168.  
  169. ShowBag();
  170.  
  171. Console.ReadKey();
  172. }
  173. }
  174.  
  175. public void GiveRandomBalance()
  176. {
  177. int minMoney = 500;
  178. int maxMoney = 4000;
  179.  
  180. _money = UserUnits.GenerateRandomNumber(minMoney, maxMoney);
  181. }
  182.  
  183. public void AddProductsInCart(List<Product> products)
  184. {
  185. int minCountProductsInCart = 1;
  186. int maxCountProductsInCart = products.Count;
  187. int firstIndex = 0;
  188.  
  189. int countProductsInCart = UserUnits.GenerateRandomNumber(minCountProductsInCart, maxCountProductsInCart);
  190.  
  191. for (int i = 0; i < countProductsInCart; i++)
  192. {
  193. int productIndex = UserUnits.GenerateRandomNumber(firstIndex, products.Count);
  194.  
  195. _productsInCart.Add(products[productIndex]);
  196. }
  197. }
  198.  
  199. public void ThrowProductIfMoneyNotEnough()
  200. {
  201. while (_money < CalculateCartCost())
  202. {
  203. ShowInfo();
  204.  
  205. Console.WriteLine("Покупателю не хватает денег для всех покупок.\n" +
  206. "Нажмите любую клавишу, чтобы выложить случайный товар из корзины.\n\n");
  207. Console.ReadKey();
  208.  
  209. ThrowRandomProductOutCart();
  210. }
  211. }
  212.  
  213. public int CalculateCartCost()
  214. {
  215. int cartCost = 0;
  216.  
  217. foreach (Product product in _productsInCart)
  218. {
  219. cartCost += product.Price;
  220. }
  221.  
  222. return cartCost;
  223. }
  224.  
  225. public void DeductMoney(int money)
  226. {
  227. _money -= money;
  228. }
  229.  
  230. private void ShowInfo()
  231. {
  232. Console.WriteLine($"Баланс {_money}");
  233.  
  234. Console.WriteLine("Продукты в корзине:");
  235.  
  236. ShowCart();
  237.  
  238. Console.WriteLine();
  239. }
  240.  
  241. private void ShowBag()
  242. {
  243. foreach (Product product in _productsInBag)
  244. {
  245. Console.Write($"{product.Name} ||");
  246. }
  247. }
  248.  
  249. private void ShowCart()
  250. {
  251. int lastIndexInCart = _productsInCart.Count - 1;
  252.  
  253. foreach (Product product in _productsInCart)
  254. {
  255. Console.Write($"{product.Name} ||");
  256. }
  257. }
  258.  
  259. private void ThrowRandomProductOutCart()
  260. {
  261. int minIndex = 0;
  262. int maxIndex = _productsInCart.Count - 1;
  263. int randomIndex = UserUnits.GenerateRandomNumber(minIndex, maxIndex);
  264.  
  265. if (_productsInCart.Count > 0)
  266. {
  267. _productsInCart.RemoveAt(randomIndex);
  268. }
  269. }
  270.  
  271. private Product Transfer()
  272. {
  273. Product product = null;
  274. int firstIndex = 0;
  275.  
  276. if (_productsInCart.Count > 0)
  277. {
  278. product = _productsInCart[firstIndex];
  279.  
  280. _productsInCart.RemoveAt(firstIndex);
  281. return product;
  282. }
  283. else
  284. {
  285. return null;
  286. }
  287. }
  288. }
  289.  
  290. class UserUnits
  291. {
  292. private static Random s_random = new Random();
  293.  
  294. public static int GenerateRandomNumber(int minNumber, int maxNumber)
  295. {
  296. return s_random.Next(minNumber, maxNumber);
  297. }
  298. }
  299. }
  300.  
Advertisement
Add Comment
Please, Sign In to add comment