06. Vehicle Catalogue 33/100
Грешка дава на тест 1, 2, 3 и 4
using System;
using System.Collections.Generic;
using System.Linq;
namespace _06._Vehicle_Catalogue
{
class Program
{
static void Main(string[] args)
{
List<VehicleCatalogue> vehicles = new List<VehicleCatalogue>();
string input;
while ((input = Console.ReadLine()) != "End")
{
string[] tokens = input.Split();
string type = tokens[0];
switch (type)
{
case "car":
type = "Car";
break;
case "truck":
type = "Truck";
break;
}
string model = tokens[1];
string color = tokens[2];
int horsePower = int.Parse(tokens[3]);
VehicleCatalogue newVehicle = new VehicleCatalogue(type, model, color, horsePower);
vehicles.Add(newVehicle);
}
while ((input = Console.ReadLine()) != "Close the Catalogue")
{
string wantedModel = input;
foreach (var vehicle in vehicles)
{
if (vehicle.Model == wantedModel)
{
Console.WriteLine(vehicle.ToString());
}
}
}
if(vehicles.Find(x => x.Type == "Car") != null)
{
double carsAverageHorsePower = vehicles.FindAll(x => x.Type == "Car").Average(x => x.HorsePower);
Console.WriteLine($"Cars have average horsepower of: {carsAverageHorsePower:f2}.");
}
if(vehicles.Find(x => x.Type == "Truck") != null)
{
double trucksAverageHorsePower = Math.Round(vehicles.FindAll(x => x.Type == "Truck").Average(x => x.HorsePower));
Console.WriteLine($"Trucks have average horsepower of: {trucksAverageHorsePower:f2}.");
}
}
}
class VehicleCatalogue
{
public VehicleCatalogue(string type, string model, string color, int horsePower)
{
Type = type;
Model = model;
Color = color;
HorsePower = horsePower;
}
public string Type { get; set; }
public string Model { get; set; }
public string Color { get; set; }
public int HorsePower { get; set; }
public override string ToString()
{
string output = $"Type: {Type}\n";
output += $"Model: {Model}\n";
output += $"Color: {Color}\n";
output += $"Horsepower: {HorsePower}";
return output;
}
}
}
При мен беше същия случай, смених horspower на double и даде 100.
Благодаря!