Advertisement
TwinFrame

OOP_Shoping

Dec 13th, 2020 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Clight_36_OOP_Shoping
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. Product tomato = new Product("Томаты", 150);
  11. Product cucumber = new Product("Огурцы", 113);
  12. Product chiken = new Product("Курица", 470);
  13. Product chips = new Product("Чипсы", 110);
  14. Product juice = new Product("Сок", 100);
  15. Product potato = new Product("Картошка", 40);
  16. Product iceCream = new Product("Мороженое", 65);
  17. Product apple = new Product("Яблоки", 100);
  18. Product orange = new Product("Апельсины", 200);
  19. Product bubbleGum = new Product("Жевательная резинка", 50);
  20. Product chocolate = new Product("Шоколад", 150);
  21. Product stillWater = new Product("Не газированная вода", 50);
  22. Product soda = new Product("Газированная вода", 50);
  23. Product fish = new Product("Рыба", 230);
  24. List<Product> allProducts = new List<Product> { tomato, cucumber, chiken, juice, potato, iceCream, apple, orange, bubbleGum, chocolate, stillWater, soda, fish };
  25.  
  26. List<string> names = new List<string> { "Иван", "Владимир", "Оксана", "Генадий", "Марина", "Игорь", "Евгений", "Мария", "Надежда", "Руслан", "Сергей", "Ирина" };
  27.  
  28. Shop shop = new Shop("Продукты у Дома", 0, allProducts);
  29. Queue<Client> shopClients = shop.CollectClients(10, names);
  30.  
  31. bool isOpen = true;
  32.  
  33. while (isOpen)
  34. {
  35. Client currentClient = shopClients.Dequeue();
  36.  
  37. Console.Clear();
  38. Console.WriteLine($"Магазин \"{shop.Name}\". Касса: {shop.Cash} руб.");
  39. Console.ForegroundColor = ConsoleColor.DarkGray;
  40. Console.WriteLine("Нажмите любую клавишу, чтобы пробить товары.\n");
  41. Console.ForegroundColor = ConsoleColor.White;
  42. Console.WriteLine($"На кассе {currentClient.Name}, имея в кошельке {currentClient.Wallet} руб.\n");
  43. Console.WriteLine("В корзине:");
  44. currentClient.ShowBasket();
  45. Console.ReadKey();
  46.  
  47. bool isRemoveProducts = false;
  48.  
  49. if (!currentClient.ReadyToBuy)
  50. {
  51. currentClient.RemoveProductsFromBasket();
  52. isRemoveProducts = true;
  53. }
  54.  
  55. if (isRemoveProducts && currentClient.IsBasketEmpty())
  56. {
  57. Console.WriteLine("\nУ клиента оказалось мало денег и он решил ничего не покупать.");
  58. Console.ReadKey();
  59. }
  60. else if(isRemoveProducts)
  61. {
  62. Console.WriteLine($"\nНе на все товары хватило денег. {currentClient.Name} выбрал(а): \n");
  63. currentClient.ShowBasket();
  64.  
  65. shop.MakePurchase(currentClient);
  66.  
  67. Console.ReadKey();
  68. }
  69. else if (currentClient.ReadyToBuy && !isRemoveProducts)
  70. {
  71. shop.MakePurchase(currentClient);
  72.  
  73. Console.WriteLine("\nПокупка прошла успешно.");
  74. Console.ReadKey();
  75. }
  76. else
  77. {
  78. Console.WriteLine("\nПокупка не удалась.");
  79. Console.ReadKey();
  80. }
  81.  
  82. if (shopClients.Count == 0)
  83. {
  84. Console.Clear();
  85. Console.WriteLine($"\nОчередь подошла к концу и магазин закрывается. Касса: {shop.Cash} руб.");
  86. Console.ReadKey();
  87. isOpen = false;
  88. }
  89. }
  90. }
  91. }
  92.  
  93. class Shop
  94. {
  95. public string Name { get; private set; }
  96. public int Cash { get; private set; }
  97. private List<Product> _shopProducts;
  98.  
  99. public Shop(string name, int cash, List<Product> products)
  100. {
  101. Name = name;
  102. Cash = cash;
  103. _shopProducts = products;
  104. }
  105.  
  106. public Queue<Client> CollectClients(int numberClients, List<string> names)
  107. {
  108. if (numberClients <= 0)
  109. numberClients = 1;
  110.  
  111. Queue<Client> currentClients = new Queue<Client>();
  112.  
  113. for (int i = 0; i < numberClients; i++)
  114. {
  115. Client tempClient = new Client(names, _shopProducts);
  116. currentClients.Enqueue(tempClient);
  117. }
  118.  
  119. return currentClients;
  120. }
  121.  
  122. public void MakePurchase(Client client)
  123. {
  124. Cash += client.GetSumPriceProducts();
  125. client.MakePurchase();
  126. }
  127. }
  128.  
  129. class Client
  130. {
  131. public string Name { get; private set; }
  132. public int Wallet { get; private set; }
  133. private Basket _basket;
  134. private Random _random = new Random();
  135. public bool ReadyToBuy { get { return _basket.GetSumPriceProducts() < Wallet; }}
  136.  
  137. public Client(List<string> names, List<Product> products)
  138. {
  139. Name = names[_random.Next(0, (names.Count - 1))];
  140. Wallet = _random.Next(150, 2001);
  141.  
  142. int numberProducts = _random.Next(1, (products.Count + 1));
  143. List<Product> tempProducts = new List<Product>(numberProducts);
  144.  
  145. for (int i = 0; i < numberProducts; i++)
  146. {
  147. int tempNum = _random.Next(0, products.Count);
  148. tempProducts.Add(products[tempNum]);
  149. }
  150.  
  151. _basket = new Basket(tempProducts);
  152. }
  153.  
  154. public void RemoveProductsFromBasket()
  155. {
  156. while (_basket.GetSumPriceProducts() > Wallet && !_basket.IsEmpty())
  157. {
  158. int tempNumberProduct = _random.Next(0, _basket.GetNumberProductsOfBasket());
  159. _basket.RemoveProduct(tempNumberProduct);
  160. }
  161. }
  162.  
  163. public void ShowBasket()
  164. {
  165. _basket.ShowProducts();
  166. }
  167.  
  168. public int GetSumPriceProducts()
  169. {
  170. return _basket.GetSumPriceProducts();
  171. }
  172.  
  173. public void MakePurchase()
  174. {
  175. Wallet -= _basket.GetSumPriceProducts();
  176. _basket.ClearBasket();
  177. }
  178.  
  179. public bool IsBasketEmpty()
  180. {
  181. return _basket.IsEmpty();
  182. }
  183. }
  184.  
  185. class Basket
  186. {
  187. private List<Product> _products;
  188.  
  189. public Basket(List<Product> products)
  190. {
  191. _products = products;
  192. }
  193.  
  194. public void ShowProducts()
  195. {
  196. for (int i = 0; i < _products.Count; i++)
  197. {
  198. if ((i + 1) < 10)
  199. {
  200. Console.Write("0" + (i + 1) + ". ");
  201. _products[i].ShowProduct();
  202. }
  203. else
  204. {
  205. Console.Write((i + 1) + ". ");
  206. _products[i].ShowProduct();
  207. }
  208. }
  209. }
  210.  
  211. public int GetSumPriceProducts()
  212. {
  213. int sum = 0;
  214. foreach (var product in _products)
  215. {
  216. sum += product.Price;
  217. }
  218.  
  219. return sum;
  220. }
  221.  
  222. public bool IsEmpty()
  223. {
  224. return _products.Count == 0;
  225. }
  226.  
  227. public int GetNumberProductsOfBasket()
  228. {
  229. return _products.Count;
  230. }
  231.  
  232. public void RemoveProduct(int numberProduct)
  233. {
  234. _products.RemoveAt(numberProduct);
  235. }
  236.  
  237. public void ClearBasket()
  238. {
  239. _products.Clear();
  240. }
  241. }
  242.  
  243. class Product
  244. {
  245. public string Name { get; private set; }
  246. public int Price { get; private set; }
  247.  
  248. public Product(string name, int price)
  249. {
  250. Name = name;
  251. Price = price;
  252. }
  253.  
  254. public void ShowProduct()
  255. {
  256. Console.WriteLine(Name + ", " + Price + " руб.");
  257. }
  258. }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement