Advertisement
dobroslav-atanasov

Untitled

Feb 16th, 2018
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. namespace CarSalesman
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. public class Startup
  8. {
  9. public static void Main()
  10. {
  11. var n = int.Parse(Console.ReadLine());
  12.  
  13. var listOfEngine = new List<Engine>();
  14. var listOfCars = new List<Car>();
  15.  
  16. for (int i = 0; i < n; i++)
  17. {
  18. var input = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  19. var model = input[0];
  20. var power = int.Parse(input[1]);
  21. var engine = new Engine(model, power);
  22. if (input.Length == 3)
  23. {
  24. int displacement;
  25. if (int.TryParse(input[2], out displacement))
  26. {
  27. engine.Displacement = displacement;
  28. }
  29. else
  30. {
  31. engine.Efficiency = input[2];
  32. }
  33. }
  34. else if (input.Length == 4)
  35. {
  36. var displacement = int.Parse(input[2]);
  37. var efficiency = input[3];
  38. engine.Displacement = displacement;
  39. engine.Efficiency = efficiency;
  40. }
  41.  
  42. listOfEngine.Add(engine);
  43. }
  44. var m = int.Parse(Console.ReadLine());
  45. for (int i = 0; i < m; i++)
  46. {
  47. var input = Console.ReadLine().Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  48. var model = input[0];
  49. var engineModel = input[1];
  50. var engine = listOfEngine.FirstOrDefault(e => e.Model == engineModel);
  51. var car = new Car(model, engine);
  52. if (input.Length == 3)
  53. {
  54. int weight;
  55. if (int.TryParse(input[2], out weight))
  56. {
  57. car.Weight = weight;
  58. }
  59. else
  60. {
  61. car.Color = input[2];
  62. }
  63. }
  64. else if (input.Length == 4)
  65. {
  66. var weight = int.Parse(input[2]);
  67. var color = input[3];
  68. car.Weight = weight;
  69. car.Color = color;
  70. }
  71.  
  72. listOfCars.Add(car);
  73. }
  74.  
  75. foreach (var car in listOfCars)
  76. {
  77. PrintCarModelEngineAndEngineProperties(car);
  78. PrintCarWeightAndColor(car);
  79. }
  80. }
  81.  
  82. private static void PrintCarWeightAndColor(Car car)
  83. {
  84. if (car.Weight == 0)
  85. {
  86. Console.WriteLine($" Weight: n/a");
  87. }
  88. else
  89. {
  90. Console.WriteLine($" Weight: {car.Weight}");
  91. }
  92.  
  93. if (car.Color == null)
  94. {
  95. Console.WriteLine($" Color: n/a");
  96. }
  97. else
  98. {
  99. Console.WriteLine($" Color: {car.Color}");
  100. }
  101. }
  102.  
  103. private static void PrintCarModelEngineAndEngineProperties(Car car)
  104. {
  105. Console.WriteLine($"{car.Model}:");
  106. Console.WriteLine($" {car.Engine.Model}:");
  107. Console.WriteLine($" Power: {car.Engine.EnginePower}");
  108. if (car.Engine.Displacement == 0)
  109. {
  110. Console.WriteLine($" Displacement: n/a");
  111. }
  112. else
  113. {
  114. Console.WriteLine($" Displacement: {car.Engine.Displacement}");
  115. }
  116. if (car.Engine.Efficiency == null)
  117. {
  118. Console.WriteLine($" Efficiency: n/a");
  119. }
  120. else
  121. {
  122. Console.WriteLine($" Efficiency: {car.Engine.Efficiency}");
  123. }
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement