Advertisement
TwinFrame

Avtoservice ver1

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