Advertisement
TwinFrame

Trains workVersion

Mar 25th, 2020
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Clight_29_OOP_Trains
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int xDefault = 0;
  12. int ySecondDiv = 5;
  13. int yThirdDiv = 9;
  14. Random random = new Random();
  15.  
  16. int priceOneKM = 3;
  17. int wallet = 0;
  18. float speedTrain = 80; //средняя скорость (км/ч)
  19. float speedTrainMinute = speedTrain / 60;
  20. int numPlaceInBigCar = 48;
  21. int numPlaceInMidCar = 24;
  22. int numPlaceInSmallCar = 12;
  23.  
  24. Station Piter = new Station("С-Петербург");
  25. Station Moscow = new Station("Москва");
  26. Station Ekaterinburg = new Station("Екатеринбург");
  27. Station Novosibirsk = new Station("Новосибирск");
  28. Station Irkutsk = new Station("Иркутск");
  29. Station UlanUde = new Station("Улан-Удэ");
  30. Station Chita = new Station("Чита");
  31. Station Yakutsk = new Station("Якутск");
  32.  
  33. Route westRussia = new Route("Зап. Россия",
  34. new Station[] { Piter, Moscow, Ekaterinburg, Novosibirsk, Irkutsk },
  35. new int[] { 700, 1500, 1650, 1730 });
  36. Route eastRussia = new Route("Вост. Россия",
  37. new Station[] { Irkutsk, UlanUde, Chita, Yakutsk },
  38. new int[] { 500, 730, 1650, 1670 });
  39.  
  40. RailWayCompany company = new RailWayCompany("Рельсы и шпалы", new Route[] { westRussia, eastRussia }, wallet, priceOneKM);
  41. RailwayCar carBig = new RailwayCar(numPlaceInBigCar);
  42. RailwayCar carMid = new RailwayCar(numPlaceInMidCar);
  43. RailwayCar carSmall = new RailwayCar(numPlaceInSmallCar);
  44. RailwayCar[] rangeOfCars = { carMid, carSmall, carBig };
  45.  
  46. while (true)
  47. {
  48. Console.CursorVisible = false;
  49. Console.Clear();
  50. Console.WriteLine($"Вас приветствует ж/д компания \"{company.Name}\". Бюджет: { company.Wallet} руб.\n");
  51. WriteColorLine("F1 - Купить билет");
  52. WriteColorLine("Esc - Выход");
  53.  
  54. ConsoleKeyInfo key = Console.ReadKey();
  55. switch (key.Key)
  56. {
  57. case ConsoleKey.Escape:
  58. Environment.Exit(0);
  59. break;
  60. case ConsoleKey.F1:
  61. bool isChooseRoute = true;
  62. while (isChooseRoute)
  63. {
  64. Console.Clear();
  65. Console.WriteLine("Маршруты компании: ");
  66. company.ShowRoutes();
  67. Console.SetCursorPosition(xDefault, ySecondDiv);
  68. WriteColorLine("F1 - Выбрать маршрут");
  69. WriteColorLine("Esc - Назад");
  70.  
  71. ConsoleKeyInfo keyRoute = Console.ReadKey();
  72. switch (keyRoute.Key)
  73. {
  74. case ConsoleKey.Escape:
  75. isChooseRoute = false;
  76. break;
  77. case ConsoleKey.F1:
  78. int userInputRoute = CheckUserInput("Выберете номер маршрута", company.Routes.Length, xDefault, yThirdDiv);
  79. Route tempRoute = company.Routes[userInputRoute - 1];
  80.  
  81. bool isChoosePoints = true;
  82. while (isChoosePoints)
  83. {
  84. Console.Clear();
  85. Console.WriteLine("Вы выбрали маршрут: ");
  86. tempRoute.ShowRoute();
  87. Console.SetCursorPosition(xDefault, ySecondDiv);
  88. WriteColorLine("F1 - Выбрать станции отправки и назначения");
  89. WriteColorLine("Esc - Назад");
  90.  
  91. ConsoleKeyInfo keyPoints = Console.ReadKey();
  92. switch (keyPoints.Key)
  93. {
  94. case ConsoleKey.Escape:
  95. isChoosePoints = false;
  96. break;
  97. case ConsoleKey.F1:
  98. int pointA = CheckUserInput("Выберете пункт отправления", tempRoute.RouteStation.Length, xDefault, yThirdDiv);
  99. int pointB = CheckUserInput("Выберете пункт назначения", tempRoute.RouteStation.Length, pointA, xDefault, yThirdDiv + 1);
  100.  
  101. int travelDistance = tempRoute.GetTravelDistance(pointA, pointB);
  102. int travelTimeMinute = Convert.ToInt32(travelDistance / speedTrainMinute);
  103. int[] travelTimeDayHourMinute = ConvertMinuteToHour(travelTimeMinute);
  104.  
  105. int passangers = random.Next(20, 501);
  106. int ticketPrice = company.GetTicketPrice(travelDistance);
  107. int sumTicketPrice = passangers * ticketPrice;
  108. Dictionary<RailwayCar, int> train = CreateTrain(passangers, rangeOfCars);
  109.  
  110. Console.Clear();
  111. Console.WriteLine($"ж/д компания \"{company.Name}\". Бюджет: {company.Wallet} руб.\n");
  112. WriteColorLine("F1 - Сформировать и запустить ж/д состав");
  113. WriteColorLine("Esc - Назад");
  114. Console.SetCursorPosition(xDefault, ySecondDiv);
  115. Console.Write("Ваш маршрут: \n");
  116. tempRoute.ShowRoute(pointA, pointB);
  117. Console.WriteLine("\n\nИнформация о маршруте:");
  118. Console.WriteLine($"Расстояние: {travelDistance} км");
  119. ShowTravelTime(travelTimeDayHourMinute);
  120. Console.WriteLine("\nФинансовая информация:");
  121. Console.WriteLine($"Цена билета: {ticketPrice} руб.");
  122. Console.WriteLine($"Куплено билетов: {passangers}");
  123. Console.WriteLine($"Итого: {sumTicketPrice} руб.\n");
  124. Console.WriteLine("Информация о составе:");
  125. ShowTrainDetail(train);
  126.  
  127. ConsoleKeyInfo keyStartTrain = Console.ReadKey();
  128. switch (keyStartTrain.Key)
  129. {
  130. case ConsoleKey.Escape:
  131. isChoosePoints = false;
  132. break;
  133. case ConsoleKey.F1:
  134. company.AddWallet(sumTicketPrice);
  135. Console.Clear();
  136. Console.WriteLine($"ж/д компания \"{company.Name}\". Бюджет: {company.Wallet} руб.\n");
  137. WriteColorLine("Процесс прошел успешно.", ConsoleColor.DarkGreen);
  138. Console.ReadKey();
  139. isChoosePoints = false;
  140. isChooseRoute = false;
  141. break;
  142. }
  143. break;
  144. }
  145. }
  146. break;
  147. }
  148. }
  149. break;
  150. }
  151. }
  152. }
  153. public static void WriteColorLine(string text, ConsoleColor color = ConsoleColor.DarkYellow)
  154. {
  155. Console.ForegroundColor = color;
  156. Console.WriteLine(text);
  157. Console.ResetColor();
  158. }
  159. public static int CheckUserInput(string text, int numOfCheck, int x, int y)
  160. {
  161. bool isChecking = true;
  162. int input = 0;
  163. while (isChecking)
  164. {
  165. Console.SetCursorPosition(x, y);
  166. Console.Write(text + ": ");
  167. bool goodInputUser = Int32.TryParse(Console.ReadLine(), out int value);
  168.  
  169. if (goodInputUser && value > 0 && value <= numOfCheck)
  170. {
  171. input = value;
  172. isChecking = false;
  173. }
  174. else
  175. {
  176. Console.SetCursorPosition(x, y + 1);
  177. WriteColorLine($"Введите число от 1 до {numOfCheck}.", ConsoleColor.DarkRed);
  178. Console.ReadKey();
  179. Console.SetCursorPosition(x, y);
  180. Console.WriteLine(" ");
  181. Console.WriteLine(" ");
  182. }
  183. }
  184. return input;
  185. }
  186. public static int CheckUserInput(string text, int numOfCheck, int pointA, int x, int y)
  187. {
  188. bool isChecking = true;
  189. int input = 0;
  190. while (isChecking)
  191. {
  192. Console.SetCursorPosition(x, y);
  193. Console.Write(text + ": ");
  194. bool goodInputUser = Int32.TryParse(Console.ReadLine(), out int value);
  195.  
  196. if (goodInputUser && value > 0 && value <= numOfCheck && value != pointA)
  197. {
  198. input = value;
  199. isChecking = false;
  200. }
  201. else if (value == pointA)
  202. {
  203. Console.SetCursorPosition(x, y + 1);
  204. WriteColorLine($"Пункт назначения совпадает с пунктом отправления." +
  205. $"Введите число от 1 до {numOfCheck}, кроме {pointA}", ConsoleColor.DarkRed);
  206. Console.ReadKey();
  207. Console.SetCursorPosition(x, y);
  208. Console.WriteLine(" ");
  209. Console.WriteLine(" ");
  210. }
  211. else
  212. {
  213. Console.SetCursorPosition(x, y + 1);
  214. WriteColorLine($"Введите число от 1 до {numOfCheck}, кроме {pointA}", ConsoleColor.DarkRed);
  215. Console.ReadKey();
  216. Console.SetCursorPosition(x, y);
  217. Console.WriteLine(" ");
  218. Console.WriteLine(" ");
  219. }
  220. }
  221. return input;
  222. }
  223. public static int[] ConvertMinuteToHour(int travelTimeMinute)
  224. {
  225. int days = 0;
  226. int hours = travelTimeMinute / 60;
  227. int minutes = travelTimeMinute % 60;
  228.  
  229. if (hours > 24)
  230. {
  231. days = hours / 24;
  232. hours -= days * 24;
  233. }
  234. int[] dayHourMinute = new int[] { days, hours, minutes };
  235. return dayHourMinute;
  236. }
  237. public static void ShowTravelTime(int[] travelTimeHourMinute)
  238. {
  239. if (travelTimeHourMinute[0] > 0)
  240. {
  241. Console.WriteLine($"Время в пути: {travelTimeHourMinute[0]} дн. {travelTimeHourMinute[1]} час. {travelTimeHourMinute[2]} мин.");
  242. }
  243. else
  244. {
  245. Console.WriteLine($"Время в пути: {travelTimeHourMinute[1]} час. {travelTimeHourMinute[2]} мин.");
  246. }
  247. }
  248. public static Dictionary<RailwayCar, int> CreateTrain(int passengers, RailwayCar[] rangeOfCars)
  249. {
  250. bool sorting = true;
  251. RailwayCar[] tempRangeOfCars = rangeOfCars;
  252. RailwayCar[] formedSortRangeOfCars = new RailwayCar[rangeOfCars.Length];
  253. int numDeleteCar = 0;
  254. while (sorting)
  255. {
  256. for (int j = 0; j < rangeOfCars.Length; j++)
  257. {
  258. RailwayCar currentMaxCar = new RailwayCar(0);
  259. if (tempRangeOfCars.Length == 0)
  260. {
  261. sorting = false;
  262. }
  263. else
  264. {
  265. for (int i = 0; i < tempRangeOfCars.Length; i++)
  266. {
  267. if (i < (tempRangeOfCars.Length - 1) && tempRangeOfCars[i].ItemPlace > currentMaxCar.ItemPlace)
  268. {
  269.  
  270. currentMaxCar = tempRangeOfCars[i];
  271. numDeleteCar = i;
  272. }
  273. else if (i == (tempRangeOfCars.Length - 1) && tempRangeOfCars[i].ItemPlace > currentMaxCar.ItemPlace)
  274. {
  275. currentMaxCar = tempRangeOfCars[i];
  276. numDeleteCar = i;
  277. }
  278. }
  279.  
  280. formedSortRangeOfCars[j] = currentMaxCar;
  281. RailwayCar[] temp2RangeOfCars = new RailwayCar[tempRangeOfCars.Length - 1];
  282. if (tempRangeOfCars.Length > 0)
  283. {
  284. for (int i = 0; i < numDeleteCar; i++)
  285. {
  286. temp2RangeOfCars[i] = tempRangeOfCars[i];
  287. }
  288. for (int i = numDeleteCar; i < temp2RangeOfCars.Length; i++)
  289. {
  290. temp2RangeOfCars[i] = tempRangeOfCars[i + 1];
  291. }
  292. }
  293. tempRangeOfCars = temp2RangeOfCars;
  294. }
  295. }
  296. }
  297. int numCurrentCar = 0;
  298. int[] itemCar = new int[formedSortRangeOfCars.Length];
  299. for (int i = 0; i < formedSortRangeOfCars.Length; i++)
  300. {
  301. numCurrentCar = passengers / formedSortRangeOfCars[i].ItemPlace;
  302. if (numCurrentCar > 0)
  303. {
  304. itemCar[i] = numCurrentCar;
  305. passengers -= numCurrentCar * formedSortRangeOfCars[i].ItemPlace;
  306. }
  307. else
  308. {
  309. itemCar[i] = 0;
  310. }
  311. }
  312. if (passengers > 0)
  313. {
  314. itemCar[itemCar.Length - 1] += 1;
  315. }
  316. if (itemCar[itemCar.Length - 1] > 1 &&
  317. formedSortRangeOfCars[formedSortRangeOfCars.Length - 2].ItemPlace >=
  318. 2 * formedSortRangeOfCars[formedSortRangeOfCars.Length - 1].ItemPlace)
  319. {
  320. itemCar[itemCar.Length - 1] -= 2;
  321. itemCar[itemCar.Length - 2] += 1;
  322. }
  323.  
  324. Dictionary<RailwayCar, int> train = new Dictionary<RailwayCar, int> { };
  325. for (int i = 0; i < formedSortRangeOfCars.Length; i++)
  326. {
  327. train.Add(formedSortRangeOfCars[i], itemCar[i]);
  328. }
  329. return train;
  330. }
  331. public static void ShowTrainDetail(Dictionary<RailwayCar, int> train)
  332. {
  333. foreach (KeyValuePair<RailwayCar, int> keyValue in train)
  334. {
  335. if (keyValue.Value > 0)
  336. {
  337. Console.WriteLine($"Вагон {keyValue.Key.Name}: {keyValue.Value} ед.");
  338. }
  339. }
  340. }
  341. }
  342. class Station
  343. {
  344. public string Name { get; private set; }
  345.  
  346. public Station(string name)
  347. {
  348. this.Name = name;
  349. }
  350. }
  351. class Route
  352. {
  353. public string Name { get; private set; }
  354. public Station[] RouteStation { get; private set; }
  355. public int[] RouteDistance { get; private set; }
  356. public Route(string name, Station[] routeStation, int[] routeDistance)
  357. {
  358. this.Name = name;
  359. this.RouteStation = routeStation;
  360. this.RouteDistance = routeDistance;
  361.  
  362. }
  363. public int GetTravelDistance(int pointA, int pointB)
  364. {
  365. int tempPointA;
  366. int tempPointB;
  367. int tempTravelDistance = 0;
  368.  
  369. if (pointA < pointB) { tempPointA = pointA; tempPointB = pointB; }
  370. else { tempPointA = pointB; tempPointB = pointA; }
  371.  
  372. for (int i = tempPointA - 1; i < tempPointB - 1; i++)
  373. {
  374. tempTravelDistance += RouteDistance[i];
  375. }
  376.  
  377. return tempTravelDistance;
  378. }
  379. public void ShowRoute()
  380. {
  381. int step = 0;
  382. for (int i = 0; i < RouteStation.Length; i++)
  383. {
  384. step++;
  385. Console.Write($"({step}) {RouteStation[i].Name}");
  386. if (i < RouteStation.Length - 1)
  387. {
  388. Console.Write(" - ");
  389. }
  390. }
  391. }
  392. public void ShowRoute(int pointA, int pointB)
  393. {
  394. int step = 0;
  395. if (pointA < pointB)
  396. {
  397. for (int i = pointA - 1; i < pointB; i++)
  398. {
  399. step++;
  400. Console.Write($"({step}) {RouteStation[i].Name}");
  401. if (i < pointB - 1)
  402. {
  403. Console.Write(" - ");
  404. }
  405. }
  406. }
  407. else
  408. {
  409. for (int i = pointA - 1; i >= pointB - 1; i--)
  410. {
  411. step++;
  412. Console.Write($"({step}) {RouteStation[i].Name}");
  413. if (i >= pointB)
  414. {
  415. Console.Write(" - ");
  416. }
  417. }
  418.  
  419. }
  420. }
  421. }
  422. class RailWayCompany
  423. {
  424. public string Name { get; private set; }
  425. public Route[] Routes { get; private set; }
  426. public int Wallet { get; private set; }
  427. public int PriceOneKM { get; private set; }
  428.  
  429. public RailWayCompany(string name, Route[] routes, int wallet, int priceOneKM)
  430. {
  431. Name = name;
  432. Routes = routes;
  433. PriceOneKM = priceOneKM;
  434. }
  435. public void ShowRoutes()
  436. {
  437. for (int i = 0; i < Routes.Length; i++)
  438. {
  439. Console.Write($"{i + 1}. ");
  440. Routes[i].ShowRoute();
  441. Console.WriteLine();
  442. }
  443. }
  444. public int GetTicketPrice(int travelDistance)
  445. {
  446. int tempTicketPrice = travelDistance * PriceOneKM;
  447. return tempTicketPrice;
  448. }
  449. public void AddWallet(int sumTicketPrice)
  450. {
  451. Wallet += sumTicketPrice;
  452. }
  453. }
  454. class RailwayCar
  455. {
  456. public string Name { get; private set; }
  457. public int ItemPlace { get; private set; }
  458. public RailwayCar(int itemPlace)
  459. {
  460. ItemPlace = itemPlace;
  461. Name = "Van-" + itemPlace;
  462. }
  463. }
  464. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement