angelllo

06. Vehicle Catalogue

Jun 27th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _06._Vehicle_Catalogue
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string[] command = Console.ReadLine().Split().ToArray();
  13.  
  14. List<Car> cars = new List<Car>();
  15. List<Truck>trucks = new List<Truck>();
  16.  
  17. double sumCarsPower = 0;
  18. double sumTrucksPower = 0;
  19.  
  20. while (command[0] != "End")
  21. {
  22. string type = command[0];
  23. string model = command[1];
  24. string color = command[2];
  25. int horsePower = int.Parse(command[3]);
  26.  
  27. if (type == "car")
  28. {
  29. Car car = new Car(type, model, color, horsePower);
  30. cars.Add(car);
  31. }
  32. else if (type == "truck")
  33. {
  34. Truck truck = new Truck(type, model, color, horsePower);
  35. trucks.Add(truck);
  36. }
  37. command = Console.ReadLine().Split().ToArray();
  38. }
  39.  
  40. string modelsInput = Console.ReadLine();
  41. while (modelsInput != "Close the Catalogue")
  42. {
  43.  
  44. if (cars.Exists(cars => cars.Model == modelsInput))
  45. {
  46. int index = cars.FindIndex(cars => cars.Model == modelsInput);
  47. Console.WriteLine($"Type: Car");
  48. Console.WriteLine($"Model: {cars[index].Model}");
  49. Console.WriteLine($"Color: {cars[index].Color}");
  50. Console.WriteLine($"Horsepower: {cars[index].HorsePower}");
  51. }
  52.  
  53. if (trucks.Exists(trucks => trucks.Model == modelsInput))
  54. {
  55. int index = trucks.FindIndex(trucks => trucks.Model == modelsInput);
  56. Console.WriteLine($"Type: Truck");
  57. Console.WriteLine($"Model: {trucks[index].Model}");
  58. Console.WriteLine($"Color: {trucks[index].Color}");
  59. Console.WriteLine($"Horsepower: {trucks[index].HorsePower}");
  60. }
  61.  
  62. modelsInput = Console.ReadLine();
  63.  
  64. }
  65. foreach (var item in cars)
  66. {
  67. sumCarsPower = cars.Sum(c => c.HorsePower);
  68. }
  69.  
  70. foreach (var item in trucks)
  71. {
  72. sumTrucksPower = trucks.Sum(t => t.HorsePower);
  73. }
  74.  
  75. if (cars.Count > 0 )
  76. {
  77. double averageCarPower = sumCarsPower / cars.Count;
  78. Console.WriteLine($"Cars have average horsepower of: {averageCarPower:f2}.");
  79. }
  80. else
  81. {
  82. Console.WriteLine($"Cars have average horsepower of: {0:f2}.");
  83. }
  84.  
  85. if (trucks.Count > 0)
  86. {
  87.  
  88. double averageTruckPower = sumTrucksPower / trucks.Count;
  89. Console.WriteLine($"Trucks have average horsepower of: {averageTruckPower:f2}.");
  90. }
  91. else
  92. {
  93. Console.WriteLine($"Trucks have average horsepower of: {0:f2}.");
  94. }
  95. }
  96.  
  97. public class Car
  98. {
  99. public string Type { get; set; }
  100. public string Model { get; set; }
  101. public string Color { get; set; }
  102. public int HorsePower { get; set; }
  103.  
  104. public Car(string type, string model, string color, int horsePower)
  105. {
  106. this.Type = type;
  107. this.Model = model;
  108. this.Color = color;
  109. this.HorsePower = horsePower;
  110. }
  111.  
  112. }
  113.  
  114. public class Truck
  115. {
  116. public string Type { get; set; }
  117. public string Model { get; set; }
  118. public string Color { get; set; }
  119. public int HorsePower { get; set; }
  120.  
  121. public Truck(string type, string model, string color, int horsepower)
  122. {
  123. this.Type = type;
  124. this.Model = model;
  125. this.Color = color;
  126. this.HorsePower = horsepower;
  127. }
  128.  
  129. }
  130.  
  131. }
  132. }
Add Comment
Please, Sign In to add comment