fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Car
  6. {
  7. public string Brand { get; set; }
  8. public int ReleaseYear { get; set; }
  9. public int Mileage { get; set; }
  10. public int Price { get; set; }
  11. public List<int> EmergencySituations { get; set; }
  12. public int ReplacedPartsCount { get; set; }
  13. public int EmergencyCount { get; set; }
  14.  
  15. public Car(string brand, int releaseYear, int mileage, int price, List<int> emergencySituations)
  16. {
  17. Brand = brand;
  18. ReleaseYear = releaseYear;
  19. Mileage = mileage;
  20. Price = price;
  21. EmergencySituations = emergencySituations;
  22. ReplacedPartsCount = emergencySituations.Sum();
  23. EmergencyCount = emergencySituations.Count;
  24. }
  25.  
  26. public string GetDescription()
  27. {
  28. return $"Автомобиль {ReleaseYear} года выпуска, марка {Brand}.\n" +
  29. $"Пробег: {Mileage}.\n" +
  30. $"Аварий: {EmergencyCount}, " +
  31. $"из с ремонтом: {EmergencyCount - EmergencySituations.Count(x => x == 0)}.\n" +
  32. $"Стоимость автомобиля составляет {Price}р.\n";
  33. }
  34. }
  35.  
  36. public class Program
  37. {
  38. private static List<Car> carList = new List<Car>
  39. {
  40. new Car("Toyota Camry", 2018, 120000, 1800000, new List<int> { 0, 1, 2 }),
  41. new Car("BMW 520i", 2020, 85000, 3200000, new List<int> { 3, 0 }),
  42. new Car("Hyundai Solaris", 2015, 180000, 750000, new List<int> { 0, 0, 1 }),
  43. new Car("Volkswagen Polo", 2019, 95000, 1200000, new List<int>()),
  44. new Car("Audi A6", 2021, 60000, 4500000, new List<int> { 5 }),
  45. new Car("Kia Rio", 2017, 140000, 900000, new List<int> { 1, 1, 0, 2 }),
  46. new Car("Mercedes-Benz C-Class", 2022, 40000, 5500000, new List<int>()),
  47. new Car("Lada Vesta", 2020, 70000, 800000, new List<int> { 0 }),
  48. new Car("Renault Logan", 2016, 160000, 650000, new List<int> { 2, 0, 1, 0 }),
  49. new Car("Skoda Octavia", 2018, 110000, 1500000, new List<int> { 0, 0 }),
  50. new Car("Nissan Qashqai", 2019, 80000, 1900000, new List<int> { 4, 1, 0 }),
  51. new Car("Ford Focus", 2014, 200000, 600000, new List<int> { 0, 1 }),
  52. new Car("Mazda 3", 2017, 130000, 1100000, new List<int> { 1, 2, 3, 0 }),
  53. new Car("Mitsubishi Outlander", 2021, 55000, 2800000, new List<int>()),
  54. new Car("Chevrolet Cruze", 2013, 190000, 550000, new List<int> { 1, 0 })
  55. };
  56.  
  57. public static void Main(string[] args)
  58. {
  59. PrintMenu();
  60. }
  61.  
  62. static void PrintMenu()
  63. {
  64. while (true)
  65. {
  66. Console.WriteLine(@"
  67.  
  68. Выберите один из пунктов меню ниже и напишите его номер
  69.  
  70. ╔═══════════════════════════════════════╗
  71. ║ Меню выбора автомобилей ║
  72. ╠═══════════════════════════════════════╣
  73. ║ 1 Самый дешевый автомобиль ║
  74. ║ 2 Самый новый автомобиль ║
  75. ║ 3 Автомобиль с меньшим кол-вом аварий ║
  76. ║ 4 С авариями без замен деталей ║
  77. ║ 5 Выход из программы ║
  78. ╚═══════════════════════════════════════╝
  79. ");
  80.  
  81. string number = Console.ReadLine();
  82. if (!int.TryParse(number, out int choice) || !new[] { 1, 2, 3, 4, 5 }.Contains(choice))
  83. {
  84. Console.WriteLine("Ожидалось целое число: 1, 2, 3, 4 или 5");
  85. continue;
  86. }
  87.  
  88.  
  89. switch (choice)
  90. {
  91. case 1:
  92. List<Car> sortedAutoByPrice = carList.OrderBy(car => car.Price).ToList();
  93. PrintCars(sortedAutoByPrice);
  94. break;
  95. case 2:
  96. List<Car> sortedAutoByYear = carList.OrderByDescending(car => car.ReleaseYear).ThenBy(car => car.Mileage).ToList();
  97. PrintCars(sortedAutoByYear);
  98. break;
  99. case 3:
  100. List<Car> sortedAutoByAccidents = carList.OrderBy(car => car.EmergencyCount).ToList();
  101. PrintCars(sortedAutoByAccidents);
  102. break;
  103. case 4:
  104. List<Car> sortedAutoWithAccidents = carList.Where(car => car.EmergencyCount > 0).OrderBy(car => car.EmergencyCount).ToList();
  105. PrintCars(sortedAutoWithAccidents);
  106. break;
  107. case 5:
  108. return;
  109. }
  110. }
  111. }
  112.  
  113. static void PrintCars(List<Car> cars)
  114. {
  115. int currentCarIndex = 0;
  116. int maxCars = cars.Count;
  117.  
  118. while (true)
  119. {
  120. Car car = cars[currentCarIndex];
  121. Console.WriteLine("\n\n\n");
  122. Console.WriteLine(car.GetDescription());
  123. Console.WriteLine(
  124. @"
  125. Выберите один из пунктов меню ниже и напишите его номер
  126.  
  127. ╔═══════════════════════════════════════╗
  128. ║ Обзор автомобилей по фильтру ║
  129. ╠═══════════════════════════════════════╣
  130. ║ 1 Вернуться в главное меню x ║
  131. ║ 2 Предыдущий автомобиль <-- ║
  132. ║ 3 Следующий автомобиль --> ║
  133. ╚═══════════════════════════════════════╝
  134. "
  135. );
  136.  
  137. string number = Console.ReadLine();
  138.  
  139. if (!int.TryParse(number, out int choice) || !new[] { 1, 2, 3 }.Contains(choice))
  140. {
  141. Console.WriteLine("Ожидалось целое число: 1, 2 или 3");
  142. continue;
  143. }
  144.  
  145.  
  146. switch (choice)
  147. {
  148. case 1:
  149. return;
  150. case 2:
  151. if (currentCarIndex-1 == -1)
  152. {
  153. Console.WriteLine("Вы долистали до начала списка.");
  154. }
  155. else
  156. {
  157. currentCarIndex--;
  158. }
  159. break;
  160. case 3:
  161. if (currentCarIndex+1 == maxCars)
  162. {
  163. Console.WriteLine("Вы долистали до конца списка.");
  164. }
  165. else
  166. {
  167. currentCarIndex++;
  168. }
  169. break;
  170.  
  171. }
  172. //currentCarIndex = Math.Max(0, currentCarIndex);
  173. //currentCarIndex = Math.Min(maxCars -1, currentCarIndex);
  174. }
  175. }
  176. }
  177.  
Success #stdin #stdout 0.09s 32456KB
stdin
1
2
1
5
stdout

Выберите один из пунктов меню ниже и напишите его номер

╔═══════════════════════════════════════╗
║        Меню выбора автомобилей        ║
╠═══════════════════════════════════════╣
║ 1 Самый дешевый автомобиль            ║
║ 2 Самый новый автомобиль              ║
║ 3 Автомобиль с меньшим кол-вом аварий ║
║ 4 С авариями без замен деталей        ║
║ 5 Выход из программы                  ║
╚═══════════════════════════════════════╝





Автомобиль 2013 года выпуска, марка Chevrolet Cruze.
Пробег: 190000.
Аварий: 2, из с ремонтом: 1.
Стоимость автомобиля составляет 550000р.


Выберите один из пунктов меню ниже и напишите его номер

╔═══════════════════════════════════════╗
║      Обзор автомобилей по фильтру     ║
╠═══════════════════════════════════════╣
║ 1 Вернуться в главное меню         x  ║
║ 2 Предыдущий автомобиль           <-- ║
║ 3 Следующий автомобиль            --> ║
╚═══════════════════════════════════════╝

Вы долистали до начала списка.




Автомобиль 2013 года выпуска, марка Chevrolet Cruze.
Пробег: 190000.
Аварий: 2, из с ремонтом: 1.
Стоимость автомобиля составляет 550000р.


Выберите один из пунктов меню ниже и напишите его номер

╔═══════════════════════════════════════╗
║      Обзор автомобилей по фильтру     ║
╠═══════════════════════════════════════╣
║ 1 Вернуться в главное меню         x  ║
║ 2 Предыдущий автомобиль           <-- ║
║ 3 Следующий автомобиль            --> ║
╚═══════════════════════════════════════╝



Выберите один из пунктов меню ниже и напишите его номер

╔═══════════════════════════════════════╗
║        Меню выбора автомобилей        ║
╠═══════════════════════════════════════╣
║ 1 Самый дешевый автомобиль            ║
║ 2 Самый новый автомобиль              ║
║ 3 Автомобиль с меньшим кол-вом аварий ║
║ 4 С авариями без замен деталей        ║
║ 5 Выход из программы                  ║
╚═══════════════════════════════════════╝