Advertisement
TwinFrame

Shop tempVer

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