Advertisement
TwinFrame

Shop workVersion

Feb 4th, 2020
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.24 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Clight_25_OOP_Shop
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int numProductsInRow = 3;
  10. int step = 25;
  11. int ySecondRow = 12;
  12. int xSecondRow = 30;
  13. int yCheckMenu = 5;
  14. int buyerWallet = 5000;
  15.  
  16. Product tomato = new Product("Помидоры", 15, 250);
  17. Product cucumber = new Product("Огурцы", 15, 200);
  18. Product potato = new Product("Картофель", 20, 100);
  19. Product orange = new Product("Апельсины", 7, 350);
  20. Product aplle = new Product("Яблоки", 13, 150);
  21. Product kiwi = new Product("Киви", 5, 300);
  22.  
  23. Shop vegetableShop = new Shop("Овощной", new Product[] { tomato, cucumber, potato, orange, kiwi, aplle }, 0);
  24.  
  25. string name = "Покупатель";
  26. Buyer buyer = new Buyer(name, new Product[0], buyerWallet);
  27.  
  28. while (true)
  29. {
  30. Console.CursorVisible = false;
  31. Console.WriteLine($"Добро пожаловать в магазин \"{vegetableShop.GetName()}\"\n\n");
  32. Console.WriteLine("Выберете действие:\n");
  33. Console.WriteLine("F1 - Зайти в магазин");
  34. Console.WriteLine("F2 - Посмотреть свои покупки");
  35. Console.WriteLine("F5 - Выйти");
  36.  
  37. ConsoleKeyInfo key = Console.ReadKey();
  38. switch (key.Key)
  39. {
  40. case ConsoleKey.F1:
  41.  
  42. bool inShop = true;
  43. while (inShop)
  44. {
  45. Console.Clear();
  46. Console.WriteLine("Выберете действие:\n");
  47. Console.WriteLine("F1 - Совершить покупку");
  48. Console.WriteLine("F5 - Назад");
  49.  
  50. Console.SetCursorPosition(0, ySecondRow - 3);
  51. Console.WriteLine($"Выручка магазина: {vegetableShop.GetMoney()} руб.");
  52. Console.SetCursorPosition(0, ySecondRow - 2);
  53. Console.WriteLine("Прилавок магазина:\n");
  54. vegetableShop.ShowProducts(0, ySecondRow, numProductsInRow, step);
  55.  
  56. Console.SetCursorPosition(2 * xSecondRow, ySecondRow - 3);
  57. Console.WriteLine($"Ваш кошелек: {buyer.GetMoney()} руб.");
  58. Console.SetCursorPosition(2 * xSecondRow, ySecondRow - 2);
  59. Console.WriteLine("Ваши покупки:\n");
  60. buyer.ShowProducts(2 * xSecondRow, ySecondRow, numProductsInRow, step, buyer.GetCurrentProducts());
  61.  
  62. ConsoleKeyInfo keyShop = Console.ReadKey();
  63. switch (keyShop.Key)
  64. {
  65. case ConsoleKey.F1:
  66.  
  67. int userNumProduct = CheckUserInput(vegetableShop.GetProducts(), yCheckMenu);
  68. int userQuantity = CheckUserInput(vegetableShop.GetProducts(), yCheckMenu, userNumProduct);
  69. bool checkUserBuyPossible = CheckUserInput(vegetableShop.GetProducts(), yCheckMenu, userNumProduct, userQuantity, buyer.GetMoney());
  70.  
  71. if (checkUserBuyPossible)
  72. {
  73. bool goodShopingBuyer = false;
  74. bool goodShopingShop = false;
  75. goodShopingBuyer = buyer.DoShoping(vegetableShop.GetProducts(), userNumProduct, userQuantity);
  76. goodShopingShop = vegetableShop.DoShoping(vegetableShop.GetProducts(), userNumProduct, userQuantity);
  77.  
  78. if (goodShopingBuyer != true && goodShopingShop != true)
  79. {
  80. Console.SetCursorPosition(0, ySecondRow - 5);
  81. Console.WriteLine("Покупка не прошла");
  82. Console.ReadKey();
  83. }
  84. }
  85. else
  86. {
  87. Console.SetCursorPosition(0, yCheckMenu + 1);
  88. Console.WriteLine($"У вас недостаточно средств для этой покупки. " +
  89. $"Не хватает {(vegetableShop.GetPrice(userNumProduct - 1) * userQuantity) - buyer.GetMoney()} руб.");
  90. Console.ReadKey();
  91. Console.SetCursorPosition(0, yCheckMenu);
  92. Console.WriteLine(" ");
  93. Console.WriteLine(" ");
  94. }
  95. break;
  96.  
  97. case ConsoleKey.F5:
  98.  
  99. buyer.MergeProducts();
  100. inShop = false;
  101. break;
  102. }
  103. }
  104. break;
  105.  
  106. case ConsoleKey.F2:
  107. Console.SetCursorPosition(0, ySecondRow - 3);
  108. Console.WriteLine($"Ваш кошелек: {buyer.GetMoney()} руб.");
  109. Console.SetCursorPosition(0, ySecondRow - 2);
  110. Console.WriteLine("Ваши покупки:\n");
  111. buyer.ShowProducts(0, ySecondRow, numProductsInRow, step, buyer.GetProducts());
  112. Console.ReadKey();
  113. break;
  114.  
  115. case ConsoleKey.F5:
  116. Console.WriteLine("\n\nПриходите еще!");
  117. Console.ReadKey();
  118. Environment.Exit(0);
  119. break;
  120. }
  121. Console.Clear();
  122. }
  123. }
  124. static int CheckUserInput(Product[] products, int yCheckMenu)
  125. {
  126. bool checkUserInput;
  127. bool checkInput = true;
  128. int input = 0;
  129.  
  130. while (checkInput)
  131. {
  132. Console.SetCursorPosition(0, yCheckMenu);
  133. Console.Write($"Выберите номер продукта (1 - {products.Length}):");
  134. string userInput = Console.ReadLine();
  135.  
  136. checkUserInput = Int32.TryParse(userInput, out int value);
  137.  
  138. if (checkUserInput && value <= products.Length && value > 0)
  139. {
  140. input = value;
  141. checkInput = false;
  142. }
  143. else
  144. {
  145. Console.SetCursorPosition(0, yCheckMenu + 1);
  146. Console.WriteLine("Введите корректное число.");
  147. Console.ReadKey();
  148. Console.SetCursorPosition(0, yCheckMenu);
  149. Console.WriteLine(" ");
  150. Console.WriteLine(" ");
  151. }
  152. }
  153. return input;
  154. }
  155. static int CheckUserInput(Product[] products, int yCheckMenu, int num)
  156. {
  157. bool checkUserInput;
  158. bool checkInput = true;
  159. int input = 0;
  160.  
  161. while (checkInput)
  162. {
  163. Console.SetCursorPosition(0, yCheckMenu);
  164. Console.Write($"Выберите количество товара (1 - {products[num - 1]._quantity}):");
  165. string userInput = Console.ReadLine();
  166.  
  167. checkUserInput = Int32.TryParse(userInput, out int value);
  168.  
  169. if (checkUserInput && value <= products[num - 1]._quantity && value > 0)
  170. {
  171. input = value;
  172. checkInput = false;
  173. }
  174. else
  175. {
  176. Console.SetCursorPosition(0, yCheckMenu + 1);
  177. Console.WriteLine("Введите корректное число.");
  178. Console.ReadKey();
  179. Console.SetCursorPosition(0, yCheckMenu);
  180. Console.WriteLine(" ");
  181. Console.WriteLine(" ");
  182. }
  183. }
  184. return input;
  185. }
  186.  
  187. static bool CheckUserInput(Product[] products, int yCheckMenu, int num, int quantity, int currentWallet)
  188. {
  189. bool checkUserInput;
  190. int currentCost = quantity * products[num - 1]._price;
  191.  
  192. if (currentCost <= currentWallet)
  193. {
  194. checkUserInput = true;
  195. return checkUserInput;
  196. }
  197. else
  198. {
  199. checkUserInput = false;
  200. return checkUserInput;
  201. }
  202. }
  203. }
  204.  
  205. interface IShoping
  206. {
  207. bool DoShoping(Product[] products, int num, int quantity);
  208. }
  209.  
  210. interface IGetMoney
  211. {
  212. int GetMoney();
  213. }
  214.  
  215. class Dealer
  216. {
  217. protected string _name;
  218. protected Product[] _products;
  219.  
  220. public Dealer(string name, Product[] products)
  221. {
  222. _name = name;
  223. _products = products;
  224. }
  225.  
  226. public void ShowProducts(int x, int y, int numProductsInRow, int step)
  227. {
  228. if (_products.Length > 0)
  229. {
  230. int j = 0;
  231. int z = 0;
  232. for (int i = 0; i < _products.Length; i++)
  233. {
  234. if (i > 0)
  235. {
  236. j++;
  237.  
  238. if (j == numProductsInRow)
  239. {
  240. j = 0;
  241. z++;
  242. }
  243. }
  244.  
  245. Console.SetCursorPosition(x + (z * step), y + (j * 4));
  246. Console.Write((i + 1) + " ");
  247. _products[i].ShowProduct(2 + x + (z * step), y + (j * 4));
  248. }
  249. }
  250. else
  251. {
  252. Console.SetCursorPosition(0, 12);
  253. Console.WriteLine("<пусто>");
  254. }
  255. }
  256.  
  257. public void ShowProducts(int x, int y, int numProductsInRow, int step, Product[] products)
  258. {
  259. if (products.Length > 0)
  260. {
  261. int j = 0;
  262. int z = 0;
  263. for (int i = 0; i < products.Length; i++)
  264. {
  265. if (i > 0)
  266. {
  267. j++;
  268.  
  269. if (j == numProductsInRow)
  270. {
  271. j = 0;
  272. z++;
  273. }
  274. }
  275.  
  276. Console.SetCursorPosition(x + (z * step), y + (j * 4));
  277. Console.Write((i + 1) + " ");
  278. products[i].ShowProductNoPrice(2 + x + (z * step), y + (j * 4));
  279. }
  280. }
  281. else
  282. {
  283. Console.SetCursorPosition(x, 12);
  284. Console.WriteLine("<пусто>");
  285. }
  286. }
  287.  
  288. public string GetName()
  289. {
  290. return _name;
  291. }
  292.  
  293. public Product[] GetProducts()
  294. {
  295. return _products;
  296. }
  297.  
  298. public int GetPrice(int num)
  299. {
  300. int currentPrice = _products[num]._price;
  301. return currentPrice;
  302. }
  303.  
  304. }
  305. class Shop : Dealer, IShoping, IGetMoney
  306. {
  307. protected int _income;
  308.  
  309. public Shop(string name, Product[] products, int income) : base(name, products)
  310. {
  311. _income = income;
  312. }
  313.  
  314. public bool DoShoping(Product[] products, int num, int quantity)
  315. {
  316. bool goodShoping;
  317.  
  318. if (_products[num - 1]._quantity > quantity)
  319. {
  320. _income += _products[num - 1]._price * quantity;
  321. _products[num - 1].SubstractQuantity(quantity);
  322.  
  323. goodShoping = true;
  324. return goodShoping;
  325. }
  326. else if (_products[num - 1]._quantity == quantity)
  327. {
  328. _income += _products[num - 1]._price * quantity;
  329.  
  330. Product[] currentProducts = new Product[_products.Length - 1];
  331. for (int i = 0; i < num - 1; i++)
  332. {
  333. currentProducts[i] = _products[i];
  334. }
  335. for (int i = num - 1; i < currentProducts.Length; i++)
  336. {
  337. currentProducts[i] = _products[i + 1];
  338. }
  339. _products = currentProducts;
  340.  
  341. goodShoping = true;
  342. return goodShoping;
  343. }
  344. else
  345. {
  346. goodShoping = false;
  347. return goodShoping;
  348. }
  349. }
  350.  
  351. public int GetMoney()
  352. {
  353. return _income;
  354. }
  355. }
  356.  
  357. class Buyer : Dealer, IShoping, IGetMoney
  358. {
  359. protected int _wallet;
  360. protected Product[] _currentProducts;
  361.  
  362. public Buyer(string name, Product[] products, int wallet) : base(name, products)
  363. {
  364. _wallet = wallet;
  365. _currentProducts = new Product[0];
  366. }
  367.  
  368. public bool DoShoping(Product[] products, int num, int quantity)
  369. {
  370.  
  371. bool goodShoping = false;
  372. bool equalName = false;
  373.  
  374. for (int i = 0; i < _currentProducts.Length; i++)
  375. {
  376. if (products[num - 1]._name == _currentProducts[i]._name)
  377. {
  378. _currentProducts[i].AddQuantity(quantity);
  379. _wallet -= products[num - 1]._price * quantity;
  380. goodShoping = true;
  381. equalName = true;
  382. }
  383. }
  384.  
  385. if (equalName != true)
  386. {
  387. Product[] tempProducts = new Product[_currentProducts.Length + 1];
  388. Product tempProduct = new Product(products[num - 1]._name, quantity, products[num - 1]._price);
  389.  
  390. for (int j = 0; j < _currentProducts.Length; j++)
  391. {
  392. tempProducts[j] = _currentProducts[j];
  393. }
  394.  
  395. tempProducts[tempProducts.Length - 1] = tempProduct;
  396. _wallet -= products[num - 1]._price * quantity;
  397.  
  398. _currentProducts = tempProducts;
  399. goodShoping = true;
  400. }
  401. return goodShoping;
  402. }
  403.  
  404. public Product[] GetCurrentProducts()
  405. {
  406. return _currentProducts;
  407. }
  408.  
  409. public int GetMoney()
  410. {
  411. return _wallet;
  412. }
  413.  
  414. public void MergeProducts()
  415. {
  416. Product[] mergeProducts = new Product[_products.Length];
  417. for (int i = 0; i < mergeProducts.Length; i++)
  418. {
  419. mergeProducts[i] = _products[i];
  420. }
  421.  
  422. for (int i = 0; i < _currentProducts.Length; i++)
  423. {
  424. int z = 0;
  425. for (int j = 0; j < mergeProducts.Length; j++)
  426. {
  427. if (_currentProducts[i]._name == mergeProducts[j]._name)
  428. {
  429. mergeProducts[j].AddQuantity(_currentProducts[i]._quantity);
  430. z++;
  431. }
  432. }
  433. if (z == 0)
  434. {
  435. Product[] tempProducts = new Product[mergeProducts.Length + 1];
  436. for (int y = 0; y < mergeProducts.Length; y++)
  437. {
  438. tempProducts[y] = mergeProducts[y];
  439. }
  440.  
  441. Product tempProduct = new Product(_currentProducts[i]._name, _currentProducts[i]._quantity, _currentProducts[i]._price);
  442.  
  443. tempProducts[tempProducts.Length - 1] = _currentProducts[i];
  444. mergeProducts = tempProducts;
  445. }
  446. }
  447. _currentProducts = new Product[0];
  448. _products = mergeProducts;
  449. }
  450. }
  451.  
  452. class Product
  453. {
  454. public string _name { get; protected set; }
  455. public int _price { get; protected set; }
  456. public int _quantity { get; protected set; }
  457.  
  458. public Product(string name, int quantity, int price)
  459. {
  460. _name = name;
  461. _price = price;
  462. _quantity = quantity;
  463. }
  464.  
  465. public void AddQuantity(int quantity)
  466. {
  467. _quantity += quantity;
  468. }
  469. public void SubstractQuantity(int quantity)
  470. {
  471. _quantity -= quantity;
  472. }
  473.  
  474. public void ShowProduct(int x, int y)
  475. {
  476. Console.SetCursorPosition(x, y);
  477. Console.Write($"{_name}");
  478. Console.SetCursorPosition(x, y + 1);
  479. Console.Write($"Кол-во: {_quantity} кг");
  480. Console.SetCursorPosition(x, y + 2);
  481. Console.Write($"Цена: {_price} руб.");
  482. }
  483.  
  484. public void ShowProductNoPrice(int x, int y)
  485. {
  486. Console.SetCursorPosition(x, y);
  487. Console.Write($"{_name}");
  488. Console.SetCursorPosition(x, y + 1);
  489. Console.Write($"Кол-во: {_quantity} кг");
  490. }
  491. }
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement