Advertisement
TwinFrame

Avtoservice ver2

Mar 12th, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.76 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 int priceRepair { get; private set; }
  180. public bool damage { get; private set; }
  181. private float maxDifficult = 10f;
  182.  
  183. public Detail(bool damage = false)
  184. {
  185. this.priceRepair = CalculatingPriceRepair(GetPrice());
  186. this.damage = damage;
  187. }
  188. public abstract string GetName();
  189. protected abstract int GetPrice();
  190. protected abstract int GetDifficulty();
  191. protected abstract void SetDifficultyDetail(float maxDifficult);
  192. private int CalculatingPriceRepair(int price)
  193. {
  194. int difficultyWorks = GetDifficulty();
  195. int priceTemp;
  196. if (difficultyWorks < 0 && difficultyWorks > maxDifficult)
  197. {
  198. SetDifficultyDetail(maxDifficult);
  199. difficultyWorks = 10;
  200. }
  201. float factorDyfficulty = difficultyWorks / maxDifficult;
  202. priceTemp = Convert.ToInt32(price * factorDyfficulty);
  203. return priceTemp;
  204. }
  205. public void ShowInfo(int xDefault, int xStep, int yLinesAllMenu, int numDetailInRow)
  206. {
  207. Console.SetCursorPosition(xDefault, yLinesAllMenu);
  208. Console.WriteLine($"Имя: {GetName()}");
  209. Console.SetCursorPosition(xDefault, yLinesAllMenu + 1);
  210. Console.WriteLine($"Цена: {GetPrice()} руб.");
  211. }
  212. public void ShowFullInfo(int numDetail, int xDefault, int yLines, int numDetailInRow)
  213. {
  214. string condition;
  215. ConsoleColor colorCondition;
  216. if (damage == false)
  217. {
  218. condition = "Рабочее";
  219. colorCondition = ConsoleColor.DarkGreen;
  220. }
  221. else
  222. {
  223. condition = "Повреждено";
  224. colorCondition = ConsoleColor.DarkRed;
  225. }
  226. Console.SetCursorPosition(xDefault, yLines);
  227. Console.Write($"{numDetail} {GetName()}");
  228. Console.SetCursorPosition(xDefault, yLines + 1);
  229. Console.WriteLine($"Цена: {GetPrice()} руб.");
  230. Console.SetCursorPosition(xDefault, yLines + 2);
  231. Console.WriteLine($"Ремонт: {priceRepair} руб.");
  232. Console.SetCursorPosition(xDefault, yLines + 3);
  233. Console.Write($"Состояние: ");
  234. Console.ForegroundColor = colorCondition;
  235. Console.Write(condition + "\n");
  236. Console.ResetColor();
  237. }
  238. public void RepairDetail()
  239. {
  240. damage = false;
  241. }
  242. public void SetDamage()
  243. {
  244. damage = true;
  245. }
  246. }
  247. class Engine : Detail
  248. {
  249. string name = "Двигатель";
  250. int price = 13000;
  251. int difficulty = 9;
  252.  
  253. public override string GetName()
  254. {
  255. return name;
  256. }
  257. protected override int GetPrice()
  258. {
  259. return price;
  260. }
  261. protected override int GetDifficulty()
  262. {
  263. return difficulty;
  264. }
  265. protected override void SetDifficultyDetail(float maxDifficult)
  266. {
  267. difficulty = Convert.ToInt32(maxDifficult);
  268. }
  269. }
  270. class Wheels : Detail
  271. {
  272. private string name = "Колёса";
  273. private int price = 6500;
  274. private int difficulty = 6;
  275.  
  276. public override string GetName()
  277. {
  278. return name;
  279. }
  280. protected override int GetPrice()
  281. {
  282. return price;
  283. }
  284. protected override int GetDifficulty()
  285. {
  286. return difficulty;
  287. }
  288. protected override void SetDifficultyDetail(float maxDifficult)
  289. {
  290. difficulty = Convert.ToInt32(maxDifficult);
  291. }
  292. }
  293. class Transmission : Detail
  294. {
  295. private string name = "Коробка передач";
  296. private int price = 5500;
  297. private int difficulty = 5;
  298.  
  299. public override string GetName()
  300. {
  301. return name;
  302. }
  303. protected override int GetPrice()
  304. {
  305. return price;
  306. }
  307. protected override int GetDifficulty()
  308. {
  309. return difficulty;
  310. }
  311. protected override void SetDifficultyDetail(float maxDifficult)
  312. {
  313. difficulty = Convert.ToInt32(maxDifficult);
  314. }
  315. }
  316. class Brake : Detail
  317. {
  318. private string name = "Тормоз. система";
  319. private int price = 5000;
  320. private int difficulty = 5;
  321.  
  322. public override string GetName()
  323. {
  324. return name;
  325. }
  326. protected override int GetPrice()
  327. {
  328. return price;
  329. }
  330. protected override int GetDifficulty()
  331. {
  332. return difficulty;
  333. }
  334. protected override void SetDifficultyDetail(float maxDifficult)
  335. {
  336. difficulty = Convert.ToInt32(maxDifficult);
  337. }
  338. }
  339. class Electrical : Detail
  340. {
  341. private string name = "Система электр.";
  342. private int price = 3500;
  343. private int difficulty = 4;
  344.  
  345. public override string GetName()
  346. {
  347. return name;
  348. }
  349. protected override int GetPrice()
  350. {
  351. return price;
  352. }
  353. protected override int GetDifficulty()
  354. {
  355. return difficulty;
  356. }
  357. protected override void SetDifficultyDetail(float maxDifficult)
  358. {
  359. difficulty = Convert.ToInt32(maxDifficult);
  360. }
  361. }
  362. class Chassis : Detail
  363. {
  364. private string name = "Ходовая часть";
  365. private int price = 3000;
  366. private int difficulty = 14;
  367.  
  368. public override string GetName()
  369. {
  370. return name;
  371. }
  372. protected override int GetPrice()
  373. {
  374. return price;
  375. }
  376. protected override int GetDifficulty()
  377. {
  378. return difficulty;
  379. }
  380. protected override void SetDifficultyDetail(float maxDifficult)
  381. {
  382. difficulty = Convert.ToInt32(maxDifficult);
  383. }
  384. }
  385. class Car
  386. {
  387. private string mark;
  388. Detail[] car = new Detail[] { new Engine(), new Wheels(), new Transmission(), new Brake(), new Electrical(), new Chassis() };
  389.  
  390. public Car(string mark)
  391. {
  392. this.mark = mark;
  393. }
  394. public void ShowCarDetail(string carOwner, int xDefault, int xStep, int yLines, int numDetailInRow)
  395. {
  396. int j = 0;
  397. int z = 0;
  398. Console.SetCursorPosition(xDefault, yLines - 3);
  399. Console.WriteLine("Владелец машины: " + carOwner);
  400. Console.WriteLine($"Марка машины: {mark}");
  401. for (int i = 0; i < car.Length; i++)
  402. {
  403. if (i > 0)
  404. {
  405. j++;
  406. if (j >= numDetailInRow)
  407. {
  408. j = 0;
  409. z++;
  410. }
  411. }
  412. car[i].ShowFullInfo(i + 1, xDefault + (z * xStep), yLines + (j * 5), numDetailInRow);
  413. }
  414. }
  415. public int GetNumDetail()
  416. {
  417. return car.Length;
  418. }
  419. public void SetDamageDetail(int[] damageDetail)
  420. {
  421. for (int i = 0; i < damageDetail.Length; i++)
  422. {
  423. car[damageDetail[i]].SetDamage();
  424. }
  425. }
  426. public void SetRepairDetail(int repairDetail)
  427. {
  428. car[repairDetail - 1].RepairDetail();
  429. }
  430. public bool CheckRepair(int inputUser)
  431. {
  432. if (car[inputUser - 1].damage == true)
  433. {
  434. return true;
  435. }
  436. else
  437. {
  438. return false;
  439. }
  440. }
  441. public string GetNameDetail(int inputUser)
  442. {
  443. return car[inputUser - 1].GetName();
  444. }
  445. public Detail GetDetailByNumber(int inputUser)
  446. {
  447. return car[inputUser - 1];
  448. }
  449. }
  450. class Avtoservice
  451. {
  452. public string name { get; private set; }
  453. private int wallet;
  454. public Dictionary<Detail, int> storage { get; private set; }
  455.  
  456. public Avtoservice(string name, int wallet, Dictionary<Detail, int> storage)
  457. {
  458. this.name = name;
  459. this.wallet = wallet;
  460. this.storage = storage;
  461. }
  462. public void ShowDescription()
  463. {
  464. Console.WriteLine($"Автосервис \"{name}\". Баланс: {wallet} руб.\n");
  465. }
  466. public void ShowStorage(int xDefault, int xStep, int yLinesAllMenu, int numDetailInRow)
  467. {
  468. int i = 0;
  469. int z = 0;
  470. Console.SetCursorPosition(xDefault, yLinesAllMenu - 3);
  471. Console.WriteLine("Склад:");
  472. foreach (KeyValuePair<Detail, int> detail in storage)
  473. {
  474. Console.SetCursorPosition(xDefault + (i * xStep), yLinesAllMenu);
  475.  
  476. detail.Key.ShowInfo(xDefault + (z * xStep), xStep, yLinesAllMenu + (i * 4), numDetailInRow);
  477. Console.SetCursorPosition(xDefault + (z * xStep), yLinesAllMenu + (i * 4) + 2);
  478. Console.WriteLine($"Наличие: {detail.Value} шт.\n");
  479. i++;
  480. if (i % numDetailInRow == 0)
  481. {
  482. i = 0;
  483. z++;
  484. }
  485. }
  486. }
  487. public bool CheckRepair(string nameDetail)
  488. {
  489. bool isCheck = false;
  490. foreach (KeyValuePair<Detail, int> detail in storage)
  491. {
  492. if (nameDetail == detail.Key.GetName() && detail.Value > 0)
  493. {
  494. isCheck = true;
  495. break;
  496. }
  497. }
  498. return isCheck;
  499. }
  500. public bool CheckPenalty(string nameDetail)
  501. {
  502. bool isCheck = false;
  503. foreach (KeyValuePair<Detail, int> detail in storage)
  504. {
  505. if (nameDetail == detail.Key.GetName() && detail.Value <= 0)
  506. {
  507. isCheck = true;
  508. break;
  509. }
  510. }
  511. return isCheck;
  512. }
  513. public void RemoveDetail(Detail detail)
  514. {
  515. Detail currentDetail = detail;
  516. storage[currentDetail] --;
  517. }
  518. public void AddToWallet(int number)
  519. {
  520. wallet += number;
  521. }
  522. public void RemoveFromWallet(int number)
  523. {
  524. wallet -= number;
  525. }
  526. }
  527. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement