Advertisement
Stan0033

Untitled

Jul 15th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Numerics;
  5.  
  6.  
  7. namespace Classes
  8. {
  9. class Vehicle
  10. {
  11. //{typeOfVehicle} {model} {color} {horsepower}
  12. public string Type { get; set; }
  13. public string Model { get; set; }
  14. public string Color { get; set; }
  15. public double Horsepower { get; set; }
  16.  
  17. public Vehicle(string type, string model, string color, double horsepower)
  18. {
  19. Type = type;
  20. Model = model;
  21. Color = color;
  22. Horsepower = horsepower;
  23. }
  24.  
  25. public override string ToString()
  26. {
  27. return $"Type: {Type}\nModel: {Model}\nColor: {Color}\nHorsepower: {Horsepower}";
  28. }
  29.  
  30. class Program
  31. {
  32.  
  33. static void Main(string[] args)
  34. {
  35. List<Vehicle> Catalog = new List<Vehicle>();
  36. while (true)
  37. {
  38. string input = Console.ReadLine();
  39. if (input == "End") break;
  40. string[] parts = input.Split(" ").ToArray();
  41. //{typeOfVehicle} {model} {color} {horsepower}
  42. string type = Upper(parts[0]);
  43. string model = parts[1];
  44. string color = parts[2];
  45. double hpower = double.Parse(parts[3]);
  46. var car = new Vehicle(type, model, color, hpower);
  47. Catalog.Add(car);
  48.  
  49. }
  50.  
  51. while (true)
  52. {
  53. string input = Console.ReadLine();
  54. if (input == "Close the Catalogue") break;
  55. foreach (var idx in Catalog)
  56. {
  57. if(idx.Model==input)
  58. Console.WriteLine(idx);
  59. }
  60.  
  61.  
  62. }
  63.  
  64.  
  65.  
  66. double carsAvg= CalcAvgHp(Catalog, "Car");
  67. double truckAvg= CalcAvgHp(Catalog, "Truck");
  68.  
  69. Console.WriteLine($"Cars have average horsepower of: {carsAvg:f2}.");
  70. Console.WriteLine($"Trucks have average horsepower of: {truckAvg:f2}.");
  71. }
  72.  
  73. private static double CalcAvgHp(List<Vehicle> catalog, string type)
  74. {
  75. double hp = 0;
  76. int counter = 0;
  77. foreach (var idxVehicle in catalog)
  78. {
  79. if (idxVehicle.Type == type)
  80. {
  81. counter++;
  82. hp += idxVehicle.Horsepower;
  83. }
  84. }
  85.  
  86. if (counter == 0 || hp == 0) return 0;
  87. return hp / counter;
  88. }
  89. }
  90. public static string Upper(string aInput)
  91. {
  92.  
  93. char[] a = aInput.ToCharArray();
  94. a[0] = char.ToUpper(a[0]);
  95. return new string(a);
  96. }
  97.  
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement