Objects and Classes - Exercise 06. Vehicle Catalogue
You have to create a vehicle catalogue. You will store only two types of vehicles – a car and a truck. Until you receive the “End” command you will be receiving lines of input in the following format:
{typeOfVehicle} {model} {color} {horsepower} |
After the “End” command, you will start receiving models of vehicles. Print the data for every received vehicle in the following format:
Type: {typeOfVehicle} Model: {modelOfVehicle} Color: {colorOfVehicle} Horsepower: {horsepowerOfVehicle} |
When you receive the command “Close the Catalogue”, print the average horsepower for the cars and for the trucks in the following format:
{typeOfVehicles} have average horsepower of {averageHorsepower}.
The average horsepower is calculated by dividing the sum of the horsepower of all vehicles from the certain type by the total count of vehicles from the same type. Round the answer to the 2nd digit after the decimal separator.
Constraints
- The type of vehicle will always be either a car or a truck.
- You will not receive the same model twice.
- The received horsepower will be an integer in the range [1…1000]
- You will receive at most 50 vehicles.
- The separator will always be a single whitespace.
Examples
Input |
Output |
truck Man red 200 truck Mercedes blue 300 car Ford green 120 car Ferrari red 550 car Lamborghini orange 570 End Ferrari Ford Man Close the Catalogue |
Type: Car Model: Ferrari Color: red Horsepower: 550 Type: Car Model: Ford Color: green Horsepower: 120 Type: Truck Model: Man Color: red Horsepower: 200 Cars have average horsepower of: 413.33. Trucks have average horsepower of: 250.00. |
using System;
using System.Collections.Generic;
using System.Linq;
namespace _06._Vehicle_Catalogue
{
class Program
{
static void Main(string[] args)
{
var catalog = new Catalog();
while (true)
{
string command = Console.ReadLine();
if (command == "End")
{
break;
}
string[] tokens = command.Split();
string type = tokens[0];
string model = tokens[1];
string color = tokens[2];
int horsePower = int.Parse(tokens[3]);
if (type == "car")
{
var car = new Car(model, color, horsePower);
catalog.Cars.Add(car);
}
else if (type == "truck")
{
var truck = new Truck(model, color, horsePower);
catalog.Trucks.Add(truck);
}
}
while (true)
{
string command = Console.ReadLine();
if (command == "Close the Catalogue")
{
break;
}
var car = catalog.Cars.Find(x => x.Model == command);
var truck = catalog.Trucks.Find(x => x.Model == command);
Console.WriteLine(car);
Console.WriteLine(truck);
}
var carsHp = catalog.Cars.Average(x => x.HorsePower);
var trucksHp = catalog.Trucks.Average(x => x.HorsePower);
Console.WriteLine($"Cars have average horsepower of: {carsHp:f2}.");
Console.WriteLine($"Trucks have average horsepower of: {trucksHp:f2}.");
}
}
class Catalog
{
public Catalog()
{
Cars = new List<Car>();
Trucks = new List<Truck>();
}
public List<Car> Cars { get; set; }
public List<Truck> Trucks { get; set; }
}
class Car
{
public string Model { get; set; }
public string Color { get; set; }
public int HorsePower { get; set; }
public Car(string model, string color, int horsePower)
{
this.Model = model;
this.Color = color;
this.HorsePower = horsePower;
}
public override string ToString()
{
return $"Type: Car\nModel: {this.Model}\nColor: {this.Color}\nHorsepower: {this.HorsePower}";
}
}
class Truck
{
public string Model { get; set; }
public string Color { get; set; }
public int HorsePower { get; set; }
public Truck(string model, string color, int horsePower)
{
this.Model = model;
this.Color = color;
this.HorsePower = horsePower;
}
public override string ToString()
{
return $"Type: Truck\nModel: {this.Model}\nColor: {this.Color}\nHorsepower: {this.HorsePower}";
}
}
линк към judge https://judge.softuni.bg/Contests/Practice/Index/1215#5
}
judge ми дава 16/100, гледах решения с два калса- car и truck, иска ми се да я реша по моя начин, който е с три класа, но не мога.
Бегло прегледах кода и забелязах, че решението ти е концентрирано в Main метода което според мен е грешно. Утре ще прегледам и условието на задачата, ще кодна и ще тествам след което ще ти отговоря по-адекватно, като се надявам да ти бъда от помощ.
Привет. Направих едно решение с 3 класа за каталога и 1 клас за изпълнение на логиката. Като го пусна в Judge, получавам 16/100 и следната грешка
Входа е коректен, изходът също, дебъгвах го с различни данни на входа и ексепшъни не ми връща, а Judge не го харесва и не разбирам защо. Ето и моето решение