Advertisement
TwinFrame

Avtoservice workVersion

Mar 17th, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Clight_28_OOP_Avtoservice
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int xStep = 23;
  12. int xDefault = 0;
  13. int spaceBetweenRow = 12;
  14. int yLinesMainMenu = 6;
  15. int yLinesSlaveMenu = 3;
  16. int yLinesAllMenu = yLinesMainMenu + yLinesSlaveMenu + 3;
  17. int numDetailInRow = 3;
  18. int fine = 1000; //штраф
  19.  
  20. Random random = new Random();
  21. Dictionary<Detail, int> storage = new Dictionary<Detail, int>
  22. {
  23. {new Engine(), 2 },
  24. {new Wheels(), 4 },
  25. {new Transmission(), 3 },
  26. {new Brake(), 5 },
  27. {new Electrical(), 6 },
  28. {new Chassis(), 2 },
  29. };
  30. Avtoservice avtoservice = new Avtoservice("КлиникаАвто", 35000, storage);
  31.  
  32. string[] brandCar = { "Toyota", "Volvo", "BMW", "Mercedez-Benz", "Nissan", "Fiat", "Renault", "Peugeot", "Kia", "Hundai" };
  33. string[] nameOwner = { "Михаил", "Виталий", "Артем", "Евгений", "Андрей", "Олег", "Петр", "Роман", "Мария", "Анна", "Ольга", "Нина" };
  34.  
  35. Car car = CreateCar(random, brandCar);
  36. string carOwner = nameOwner[random.Next(0, nameOwner.Length)];
  37. while (true)
  38. {
  39. Console.CursorVisible = false;
  40. bool isRepair = true;
  41. Console.WriteLine($"Автосервис \"{avtoservice.Name}\"\n");
  42. Console.WriteLine("F1 - Начать ремонт");
  43. Console.WriteLine("F2 - Закупить деталь на склад");
  44. Console.WriteLine("F5 - Выйти");
  45.  
  46. ConsoleKeyInfo key = Console.ReadKey(true);
  47. switch (key.Key)
  48. {
  49. case ConsoleKey.F1:
  50. while (isRepair)
  51. {
  52. Console.Clear();
  53. avtoservice.ShowDescription();
  54. Console.WriteLine("F1 - Ремонт детали");
  55. Console.WriteLine("F2 - Посмотреть следующую машину");
  56. Console.WriteLine("F5 - Назад");
  57. car.ShowCarDetail(carOwner, xDefault, xStep, yLinesAllMenu, numDetailInRow);
  58. avtoservice.ShowStorage((xStep * 2) + spaceBetweenRow, xStep, yLinesAllMenu, numDetailInRow);
  59.  
  60. ConsoleKeyInfo keySlaveMenu = Console.ReadKey(true);
  61. switch (keySlaveMenu.Key)
  62. {
  63. case ConsoleKey.F1:
  64. int userDetailNum = CheckInputUser(xDefault, yLinesMainMenu, car);
  65. bool goodCheckRepair = car.CheckRepair(userDetailNum);
  66. string userDetailName = car.GetNameDetail(userDetailNum);
  67. bool isPenalty = avtoservice.CheckPenalty(userDetailName);
  68.  
  69. if (goodCheckRepair)
  70. {
  71. bool goodCheckDetailInStorage = avtoservice.CheckRepair(userDetailName);
  72.  
  73. if (goodCheckDetailInStorage)
  74. {
  75. car.SetRepairDetail(userDetailNum);
  76. Detail tempDetail = car.GetDetailByNumber(userDetailNum);
  77. avtoservice.RemoveDetail(tempDetail);
  78. avtoservice.AddToWallet(tempDetail.PriceRepair);
  79. WriteSlaveMenu(xDefault, yLinesMainMenu, "Ремонт прошел успешно.", ConsoleColor.DarkGreen);
  80. }
  81. else if (isPenalty)
  82. {
  83. avtoservice.RemoveFromWallet(fine);
  84. WriteSlaveMenu(xDefault, yLinesMainMenu, "На складе не оказалось нужной детали. Штраф 1000 рублей.");
  85. }
  86. else
  87. {
  88. WriteSlaveMenu(xDefault, yLinesMainMenu, "Сделка не прошла. Вне зоны ответственности Автосервиса.");
  89. }
  90.  
  91. }
  92. else if (isPenalty)
  93. {
  94. WriteSlaveMenu(xDefault, yLinesMainMenu, "Детали не оказалось на складе. Это спасло от штрафа.");
  95. }
  96. else
  97. {
  98. avtoservice.RemoveFromWallet(fine);
  99. avtoservice.RemoveDetail(car.GetDetailByNumber(userDetailNum));
  100. WriteSlaveMenu(xDefault, yLinesMainMenu, "Вы заменили изначально рабочую деталь. Штраф 1000 рублей.");
  101. }
  102. break;
  103. case ConsoleKey.F2:
  104. car = CreateCar(random, brandCar);
  105. carOwner = nameOwner[random.Next(0, nameOwner.Length)];
  106. break;
  107. case ConsoleKey.F5:
  108. Console.Clear();
  109. isRepair = false;
  110. break;
  111. }
  112. }
  113. break;
  114. case ConsoleKey.F2:
  115. bool isAddingDetail = true;
  116. while (isAddingDetail)
  117. {
  118. Console.Clear();
  119. avtoservice.ShowDescription();
  120. Console.WriteLine("F1 - Закупить деталь");
  121. Console.WriteLine("F5 - Назад");
  122. avtoservice.ShowStorage((xStep * 2) + spaceBetweenRow, xStep, yLinesAllMenu, numDetailInRow);
  123.  
  124. ConsoleKeyInfo keyBuyDetail = Console.ReadKey(true);
  125. switch (keyBuyDetail.Key)
  126. {
  127. case ConsoleKey.F1:
  128. int itemDetail;
  129. Detail tempDetail = CheckInputDetail(xDefault, yLinesMainMenu, yLinesAllMenu, avtoservice, out itemDetail);
  130. avtoservice.AddDetails(tempDetail, itemDetail);
  131. int sumDetail = tempDetail.GetPrice() * itemDetail;
  132. avtoservice.RemoveFromWallet(sumDetail);
  133. break;
  134. case ConsoleKey.F5:
  135. Console.Clear();
  136. isAddingDetail = false;
  137. break;
  138. }
  139. }
  140. break;
  141. case ConsoleKey.F5:
  142. Environment.Exit(0);
  143. break;
  144. default:
  145. WriteSlaveMenu(xDefault, yLinesMainMenu, "Не корректный ввод.");
  146. Console.Clear();
  147. break;
  148. }
  149. }
  150.  
  151. static int GenerateNumber(Random random, int minValue, int maxValue)
  152. {
  153. return random.Next(minValue, maxValue);
  154. }
  155.  
  156. static Car CreateCar(Random random, string[] brandCar)
  157. {
  158. int numMark = random.Next(0, brandCar.Length);
  159. Car car = new Car(brandCar[numMark]);
  160.  
  161. //Установление деталей, нуждающихся в ремонте.
  162. int numDamageDetail = GenerateNumber(random, 1, 4);
  163. int[] damageDetail = new int[numDamageDetail];
  164. for (int i = 0; i < numDamageDetail; i++)
  165. {
  166. damageDetail[i] = GenerateNumber(random, 0, car.GetNumDetail());
  167. }
  168. car.SetDamageDetail(damageDetail);
  169. return car;
  170. }
  171.  
  172. static int CheckInputUser(int xDefault, int yLinesMainMenu, Car car)
  173. {
  174. bool isInput = true;
  175. int input = 0;
  176. while (isInput)
  177. {
  178. Console.SetCursorPosition(xDefault, yLinesMainMenu);
  179. Console.Write("Выберите номер ремонтируемой детали: ");
  180.  
  181. bool goodInputUser = Int32.TryParse(Console.ReadLine(), out int value);
  182. if (goodInputUser && value > 0 && value <= car.GetNumDetail())
  183. {
  184. isInput = false;
  185. input = value;
  186. }
  187. else
  188. {
  189. WriteSlaveMenu(xDefault, yLinesMainMenu + 1, $"Не корректные данные. Введите от 1 до {car.GetNumDetail()}.");
  190. Console.SetCursorPosition(xDefault, yLinesMainMenu);
  191. Console.WriteLine(" ");
  192. Console.WriteLine(" ");
  193. }
  194. }
  195. return input;
  196. }
  197.  
  198. static Detail CheckInputDetail(int xDefault, int yLinesMainMenu, int yLinesAllMenu, Avtoservice avtoservice, out int itemDetail)
  199. {
  200. Detail userDetail = null;
  201. itemDetail = 0;
  202. bool isInput = true;
  203. while (isInput)
  204. {
  205. int lengthStorage = avtoservice.Storage.Count;
  206. Console.SetCursorPosition(xDefault, yLinesAllMenu);
  207. for (int i = 0; i < lengthStorage; i++)
  208. {
  209. Console.WriteLine($"{i + 1} - {avtoservice.Storage.ElementAt(i).Key.GetName()}; Цена: {avtoservice.Storage.ElementAt(i).Key.GetPrice()} руб.");
  210. }
  211.  
  212. Console.SetCursorPosition(xDefault, yLinesMainMenu - 1);
  213. Console.Write("Выберите деталь: ");
  214.  
  215. bool goodInputUser = Int32.TryParse(Console.ReadLine(), out int valueNum);
  216. if (goodInputUser && valueNum > 0 && valueNum <= lengthStorage)
  217. {
  218. Console.Write($"Выберите количество покупаемой детали \"{avtoservice.Storage.ElementAt(valueNum - 1).Key.GetName()}\": ");
  219. bool goodItemDetail = Int32.TryParse(Console.ReadLine(), out int valueItem);
  220. int sumDetail = avtoservice.Storage.ElementAt(valueNum - 1).Key.GetPrice() * valueItem;
  221. if (goodItemDetail && sumDetail < avtoservice.GetWallet())
  222. {
  223. WriteSlaveMenu(xDefault, yLinesMainMenu + 1, "Покупка прошла успешно.", ConsoleColor.DarkGreen);
  224. itemDetail = valueItem;
  225. userDetail = avtoservice.Storage.ElementAt(valueNum - 1).Key;
  226. isInput = false;
  227. }
  228. else if (goodItemDetail && sumDetail > avtoservice.GetWallet())
  229. {
  230. int needMoney = sumDetail - avtoservice.GetWallet();
  231. string text = $"Не хватило { needMoney} руб.на покупку { valueItem} шт. детали \"{avtoservice.Storage.ElementAt(valueNum - 1).Key.GetName()}\"";
  232. WriteSlaveMenu(xDefault, yLinesMainMenu + 1, text);
  233. Console.SetCursorPosition(xDefault, yLinesMainMenu - 1);
  234. Console.WriteLine(" ");
  235. Console.WriteLine(" ");
  236. Console.WriteLine(" ");
  237. }
  238. else
  239. {
  240. WriteSlaveMenu(xDefault, yLinesMainMenu + 1, "Не корректно введены значения. Попробуйте снова.");
  241. Console.SetCursorPosition(xDefault, yLinesMainMenu - 1);
  242. Console.WriteLine(" ");
  243. Console.WriteLine(" ");
  244. Console.WriteLine(" ");
  245. }
  246. }
  247. else
  248. {
  249. string text = $"Не корректные данные. Введите от 1 до {avtoservice.Storage.Count}.";
  250. WriteSlaveMenu(xDefault, yLinesMainMenu + 1, text);
  251. Console.SetCursorPosition(xDefault, yLinesMainMenu - 1);
  252. Console.WriteLine(" ");
  253. Console.WriteLine(" ");
  254. Console.WriteLine(" ");
  255. }
  256. }
  257. return userDetail;
  258. }
  259.  
  260. static void WriteSlaveMenu(int xDefault, int yLinesMainMenu, string text, ConsoleColor textColor = ConsoleColor.DarkYellow)
  261. {
  262. Console.SetCursorPosition(xDefault, yLinesMainMenu);
  263. Console.WriteLine(" ");
  264. Console.SetCursorPosition(xDefault, yLinesMainMenu);
  265. Console.ForegroundColor = textColor;
  266. Console.WriteLine(text);
  267. Console.ResetColor();
  268. Console.ReadKey();
  269. Console.SetCursorPosition(xDefault, yLinesMainMenu);
  270. Console.WriteLine(" ");
  271. }
  272. }
  273. }
  274. abstract class Detail
  275. {
  276. public int PriceRepair { get; private set; }
  277. public bool Damage { get; private set; }
  278. private float _maxDifficult = 10f;
  279.  
  280. public Detail(bool damage = false)
  281. {
  282. PriceRepair = CalculatingPriceRepair(GetPrice());
  283. Damage = damage;
  284. }
  285. public abstract string GetName();
  286. public abstract int GetPrice();
  287. protected abstract int GetDifficulty();
  288. protected abstract void SetDifficultyDetail(float maxDifficult);
  289. private int CalculatingPriceRepair(int price)
  290. {
  291. int difficultyWorks = GetDifficulty();
  292. int priceTemp;
  293. if (difficultyWorks < 0 && difficultyWorks > _maxDifficult)
  294. {
  295. SetDifficultyDetail(_maxDifficult);
  296. difficultyWorks = 10;
  297. }
  298. float factorDyfficulty = difficultyWorks / _maxDifficult;
  299. priceTemp = Convert.ToInt32(price * factorDyfficulty);
  300. return priceTemp;
  301. }
  302. public void ShowInfo(int xDefault, int xStep, int yLinesAllMenu, int numDetailInRow)
  303. {
  304. Console.SetCursorPosition(xDefault, yLinesAllMenu);
  305. Console.WriteLine($"Имя: {GetName()}");
  306. Console.SetCursorPosition(xDefault, yLinesAllMenu + 1);
  307. Console.WriteLine($"Цена: {GetPrice()} руб.");
  308. }
  309. public void ShowFullInfo(int numDetail, int xDefault, int yLines, int numDetailInRow)
  310. {
  311. string condition;
  312. ConsoleColor colorCondition;
  313. if (Damage == false)
  314. {
  315. condition = "Рабочее";
  316. colorCondition = ConsoleColor.DarkGreen;
  317. }
  318. else
  319. {
  320. condition = "Повреждено";
  321. colorCondition = ConsoleColor.DarkRed;
  322. }
  323. Console.SetCursorPosition(xDefault, yLines);
  324. Console.Write($"{numDetail} {GetName()}");
  325. Console.SetCursorPosition(xDefault, yLines + 1);
  326. Console.WriteLine($"Цена: {GetPrice()} руб.");
  327. Console.SetCursorPosition(xDefault, yLines + 2);
  328. Console.WriteLine($"Ремонт: {PriceRepair} руб.");
  329. Console.SetCursorPosition(xDefault, yLines + 3);
  330. Console.Write($"Состояние: ");
  331. Console.ForegroundColor = colorCondition;
  332. Console.Write(condition + "\n");
  333. Console.ResetColor();
  334. }
  335. public void RepairDetail()
  336. {
  337. Damage = false;
  338. }
  339. public void SetDamage()
  340. {
  341. Damage = true;
  342. }
  343. }
  344. class Engine : Detail
  345. {
  346. string name = "Двигатель";
  347. int price = 13000;
  348. int difficulty = 9;
  349.  
  350. public override string GetName()
  351. {
  352. return name;
  353. }
  354. public override int GetPrice()
  355. {
  356. return price;
  357. }
  358. protected override int GetDifficulty()
  359. {
  360. return difficulty;
  361. }
  362. protected override void SetDifficultyDetail(float maxDifficult)
  363. {
  364. difficulty = Convert.ToInt32(maxDifficult);
  365. }
  366. }
  367. class Wheels : Detail
  368. {
  369. private string _name = "Колёса";
  370. private int _price = 6500;
  371. private int _difficulty = 6;
  372.  
  373. public override string GetName()
  374. {
  375. return _name;
  376. }
  377. public override int GetPrice()
  378. {
  379. return _price;
  380. }
  381. protected override int GetDifficulty()
  382. {
  383. return _difficulty;
  384. }
  385. protected override void SetDifficultyDetail(float maxDifficult)
  386. {
  387. _difficulty = Convert.ToInt32(maxDifficult);
  388. }
  389. }
  390. class Transmission : Detail
  391. {
  392. private string _name = "Коробка передач";
  393. private int _price = 5500;
  394. private int _difficulty = 5;
  395.  
  396. public override string GetName()
  397. {
  398. return _name;
  399. }
  400. public override int GetPrice()
  401. {
  402. return _price;
  403. }
  404. protected override int GetDifficulty()
  405. {
  406. return _difficulty;
  407. }
  408. protected override void SetDifficultyDetail(float maxDifficult)
  409. {
  410. _difficulty = Convert.ToInt32(maxDifficult);
  411. }
  412. }
  413. class Brake : Detail
  414. {
  415. private string _name = "Тормоз. система";
  416. private int _price = 5000;
  417. private int _difficulty = 5;
  418.  
  419. public override string GetName()
  420. {
  421. return _name;
  422. }
  423. public override int GetPrice()
  424. {
  425. return _price;
  426. }
  427. protected override int GetDifficulty()
  428. {
  429. return _difficulty;
  430. }
  431. protected override void SetDifficultyDetail(float maxDifficult)
  432. {
  433. _difficulty = Convert.ToInt32(maxDifficult);
  434. }
  435. }
  436. class Electrical : Detail
  437. {
  438. private string _name = "Система электр.";
  439. private int _price = 3500;
  440. private int _difficulty = 4;
  441.  
  442. public override string GetName()
  443. {
  444. return _name;
  445. }
  446. public override int GetPrice()
  447. {
  448. return _price;
  449. }
  450. protected override int GetDifficulty()
  451. {
  452. return _difficulty;
  453. }
  454. protected override void SetDifficultyDetail(float maxDifficult)
  455. {
  456. _difficulty = Convert.ToInt32(maxDifficult);
  457. }
  458. }
  459. class Chassis : Detail
  460. {
  461. private string _name = "Ходовая часть";
  462. private int _price = 3000;
  463. private int _difficulty = 14;
  464.  
  465. public override string GetName()
  466. {
  467. return _name;
  468. }
  469. public override int GetPrice()
  470. {
  471. return _price;
  472. }
  473. protected override int GetDifficulty()
  474. {
  475. return _difficulty;
  476. }
  477. protected override void SetDifficultyDetail(float maxDifficult)
  478. {
  479. _difficulty = Convert.ToInt32(maxDifficult);
  480. }
  481. }
  482. class Car
  483. {
  484. private string _markCar;
  485. readonly Detail[] CarDetails = new Detail[] { new Engine(), new Wheels(), new Transmission(), new Brake(), new Electrical(), new Chassis() };
  486.  
  487. public Car(string markCar)
  488. {
  489. _markCar = markCar;
  490. }
  491. public void ShowCarDetail(string carOwner, int xDefault, int xStep, int yLines, int numDetailInRow)
  492. {
  493. int numLine = 0;
  494. int numRow = 0;
  495. Console.SetCursorPosition(xDefault, yLines - 3);
  496. Console.WriteLine("Владелец машины: " + carOwner);
  497. Console.WriteLine($"Марка машины: {_markCar}");
  498. for (int i = 0; i < CarDetails.Length; i++)
  499. {
  500. if (i > 0)
  501. {
  502. numLine++;
  503. if (numLine >= numDetailInRow)
  504. {
  505. numLine = 0;
  506. numRow++;
  507. }
  508. }
  509. CarDetails[i].ShowFullInfo(i + 1, xDefault + (numRow * xStep), yLines + (numLine * 5), numDetailInRow);
  510. }
  511. }
  512. public int GetNumDetail()
  513. {
  514. return CarDetails.Length;
  515. }
  516. public void SetDamageDetail(int[] damageDetail)
  517. {
  518. for (int i = 0; i < damageDetail.Length; i++)
  519. {
  520. CarDetails[damageDetail[i]].SetDamage();
  521. }
  522. }
  523. public void SetRepairDetail(int repairDetail)
  524. {
  525. CarDetails[repairDetail - 1].RepairDetail();
  526. }
  527. public bool CheckRepair(int inputUser)
  528. {
  529. if (CarDetails[inputUser - 1].Damage == true)
  530. {
  531. return true;
  532. }
  533. else
  534. {
  535. return false;
  536. }
  537. }
  538. public string GetNameDetail(int inputUser)
  539. {
  540. return CarDetails[inputUser - 1].GetName();
  541. }
  542. public Detail GetDetailByNumber(int inputUser)
  543. {
  544. return CarDetails[inputUser - 1];
  545. }
  546. }
  547. class Avtoservice
  548. {
  549. public string Name { get; private set; }
  550. private int _wallet;
  551. public Dictionary<Detail, int> Storage { get; private set; }
  552.  
  553. public Avtoservice(string name, int wallet, Dictionary<Detail, int> storage)
  554. {
  555. this.Name = name;
  556. this._wallet = wallet;
  557. this.Storage = storage;
  558. }
  559. public int GetWallet()
  560. {
  561. return _wallet;
  562. }
  563. public void ShowDescription()
  564. {
  565. Console.WriteLine($"Автосервис \"{Name}\". Баланс: {_wallet} руб.\n");
  566. }
  567. public void ShowStorage(int xDefault, int xStep, int yLinesAllMenu, int numDetailInRow)
  568. {
  569. int i = 0;
  570. int z = 0;
  571. Console.SetCursorPosition(xDefault, yLinesAllMenu - 3);
  572. Console.WriteLine("Склад:");
  573. foreach (KeyValuePair<Detail, int> detail in Storage)
  574. {
  575. Console.SetCursorPosition(xDefault + (i * xStep), yLinesAllMenu);
  576.  
  577. detail.Key.ShowInfo(xDefault + (z * xStep), xStep, yLinesAllMenu + (i * 4), numDetailInRow);
  578. Console.SetCursorPosition(xDefault + (z * xStep), yLinesAllMenu + (i * 4) + 2);
  579. Console.Write("Наличие: ");
  580.  
  581. ConsoleColor colorQuantity = ConsoleColor.White;
  582. if (detail.Value < 1)
  583. {
  584. colorQuantity = ConsoleColor.DarkRed;
  585. }
  586. else if (detail.Value < 4)
  587. {
  588. colorQuantity = ConsoleColor.DarkGray;
  589. }
  590. Console.ForegroundColor = colorQuantity;
  591. Console.WriteLine($"{detail.Value} шт.\n");
  592. Console.ResetColor();
  593.  
  594. i++;
  595. if (i % numDetailInRow == 0)
  596. {
  597. i = 0;
  598. z++;
  599. }
  600. }
  601. }
  602. public bool CheckRepair(string nameDetail)
  603. {
  604. bool isCheck = false;
  605. foreach (KeyValuePair<Detail, int> detail in Storage)
  606. {
  607. if (nameDetail == detail.Key.GetName() && detail.Value > 0)
  608. {
  609. isCheck = true;
  610. break;
  611. }
  612. }
  613. return isCheck;
  614. }
  615. public bool CheckPenalty(string nameDetail)
  616. {
  617. bool isCheck = false;
  618. foreach (KeyValuePair<Detail, int> detail in Storage)
  619. {
  620. if (nameDetail == detail.Key.GetName() && detail.Value <= 0)
  621. {
  622. isCheck = true;
  623. break;
  624. }
  625. }
  626. return isCheck;
  627. }
  628. public void RemoveDetail(Detail detail)
  629. {
  630. Detail tempDetail = null;
  631.  
  632. foreach (var item in Storage.Keys)
  633. {
  634. if (detail.GetType() == item.GetType())
  635. {
  636. tempDetail = item;
  637. }
  638. }
  639. Storage[tempDetail]--;
  640. }
  641. public void AddDetails(Detail detail, int itemDetail)
  642. {
  643. Detail tempDetail = null;
  644. foreach (var item in Storage.Keys)
  645. {
  646. if (detail.GetType() == item.GetType())
  647. {
  648. tempDetail = item;
  649. break;
  650. }
  651. }
  652. Storage[tempDetail] += itemDetail;
  653. }
  654. public void AddToWallet(int number)
  655. {
  656. _wallet += number;
  657. }
  658. public void RemoveFromWallet(int number)
  659. {
  660. _wallet -= number;
  661. }
  662. }
  663. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement