Exercise: Objects and Classes - 06. Vehicle catalogue
Според второто ограничение, когато се появи превозно средсво с вече въведена марка не го запазваме. Въпреки това пак ми дава само 33/100
using System;
using System.Collections.Generic;
namespace _06._Vehicle_catalogue
{
class Program
{
static void Main(string[] args)
{
string input = string.Empty;
double carsHorsePower = 0;
int carCount = 0;
double trucksHorsePower = 0;
int truckCount = 0;
List<Vehicle> vechicles = new List<Vehicle>();
List<string> brands = new List<string>();
List<string> checkBrands = new List<string>();
while ((input = Console.ReadLine()) != "End")
{
string[] tokens = input.Split(' ');
if (checkBrands.Contains(tokens[1]))
{
continue;
}
Vehicle vehicle = new Vehicle(tokens[0], tokens[1], tokens[2], double.Parse(tokens[3]));
if (tokens[0] == "car")
{
carsHorsePower += double.Parse(tokens[3]);
carCount++;
vechicles.Add(vehicle);
}
else if (tokens[0] == "truck")
{
trucksHorsePower += double.Parse(tokens[3]);
truckCount++;
vechicles.Add(vehicle);
}
checkBrands.Add(tokens[1]);
}
input = Console.ReadLine();
while (input != "Close the Catalogue")
{
brands.Add(input);
input = Console.ReadLine();
}
for (int i = 0; i < brands.Count; i++)
{
foreach (Vehicle vehicle in vechicles)
{
if (brands[i] == vehicle.Model)
{
Console.WriteLine($"Type: {System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(vehicle.Type.ToLower())}\n" +
$"Model: {vehicle.Model}\n" +
$"Color: {vehicle.Color}\n" +
$"Horsepower: {vehicle.HorsePower}");
}
}
}
Console.WriteLine($"Cars have average horsepower of: {carsHorsePower / carCount:F2}.");
Console.WriteLine($"Trucks have average horsepower of: {trucksHorsePower / truckCount:F2}.");
}
}
class Vehicle
{
public string Type { get; set; }
public string Model { get; set; }
public string Color { get; set; }
public double HorsePower { get; set; }
public Vehicle(string type, string model, string color, double horsePower)
{
Type = type;
Model = model;
Color = color;
HorsePower = horsePower;
}
}
}