Advertisement
onqkoie

Untitled

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