Advertisement
TwinFrame

OOP_Shoping_ver2

Dec 15th, 2020 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 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.Basket.IsEmpty)
  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.  
  98. private List<Product> _products;
  99.  
  100. public Shop(string name, int cash, List<Product> products)
  101. {
  102. Name = name;
  103. Cash = cash;
  104. _products = products;
  105. }
  106.  
  107. public Queue<Client> CollectClients(int countClients, List<string> names)
  108. {
  109. if (countClients <= 0)
  110. countClients = 1;
  111.  
  112. Queue<Client> clients = new Queue<Client>();
  113.  
  114. for (int i = 0; i < countClients; i++)
  115. {
  116. Client tempClient = new Client(names, _products);
  117. clients.Enqueue(tempClient);
  118. }
  119.  
  120. return clients;
  121. }
  122.  
  123. public void MakePurchase(Client client)
  124. {
  125. Cash += client.MakePurchase();
  126. }
  127. }
  128.  
  129. class Client
  130. {
  131. private Random _random = new Random();
  132.  
  133. public bool ReadyToBuy { get { return Basket.GetSumPriceProducts() < Wallet; } }
  134. public string Name { get; private set; }
  135. public int Wallet { get; private set; }
  136. public Basket Basket { get; private set; }
  137.  
  138. public Client(List<string> names, List<Product> products)
  139. {
  140. Name = names[_random.Next(0, (names.Count - 1))];
  141. Wallet = _random.Next(150, 2001);
  142.  
  143. int countProductsInBasket = _random.Next(1, (products.Count + 1));
  144.  
  145. for (int i = 0; i < countProductsInBasket; i++)
  146. {
  147. int tempProduct = _random.Next(0, products.Count);
  148. Basket.AddProduct(products[tempProduct]);
  149. }
  150. }
  151.  
  152. public void RemoveProductsFromBasket()
  153. {
  154. while (Basket.GetSumPriceProducts() > Wallet && !Basket.IsEmpty)
  155. {
  156. int tempNumberProduct = _random.Next(0, Basket.GetCountProductsInBasket());
  157. Basket.RemoveProduct(tempNumberProduct);
  158. }
  159. }
  160.  
  161. public void ShowBasket()
  162. {
  163. Basket.ShowProducts();
  164. }
  165.  
  166. public int MakePurchase()
  167. {
  168. int SumPrice = Basket.GetSumPriceProducts();
  169. Wallet -= SumPrice;
  170. Basket.ClearBasket();
  171.  
  172. return SumPrice;
  173. }
  174. }
  175.  
  176. class Basket
  177. {
  178. public bool IsEmpty { get { return _products.Count == 0; } private set { } }
  179.  
  180. private List<Product> _products;
  181.  
  182. public void ShowProducts()
  183. {
  184. for (int i = 0; i < _products.Count; i++)
  185. {
  186. if ((i + 1) < 10)
  187. {
  188. Console.Write("0" + (i + 1) + ". ");
  189. _products[i].ShowProduct();
  190. }
  191. else
  192. {
  193. Console.Write((i + 1) + ". ");
  194. _products[i].ShowProduct();
  195. }
  196. }
  197. }
  198.  
  199. public void AddProduct(Product product)
  200. {
  201. _products.Add(product);
  202. }
  203.  
  204. public int GetSumPriceProducts()
  205. {
  206. int sum = 0;
  207. foreach (var product in _products)
  208. {
  209. sum += product.Price;
  210. }
  211.  
  212. return sum;
  213. }
  214.  
  215. public int GetCountProductsInBasket()
  216. {
  217. return _products.Count;
  218. }
  219.  
  220. public void RemoveProduct(int numberProduct)
  221. {
  222. _products.RemoveAt(numberProduct);
  223. }
  224.  
  225. public void ClearBasket()
  226. {
  227. _products.Clear();
  228. }
  229. }
  230.  
  231. class Product
  232. {
  233. public string Name { get; private set; }
  234. public int Price { get; private set; }
  235.  
  236. public Product(string name, int price)
  237. {
  238. Name = name;
  239. Price = price;
  240. }
  241.  
  242. public void ShowProduct()
  243. {
  244. Console.WriteLine(Name + ", " + Price + " руб.");
  245. }
  246. }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement