5. Vehicle Catalogue 33/100
Може ли малко помощ за печатането накрая? В условието на задачата Vehicle.Type : "Cars" и "Truck трябва да се изпечатват по начин, по който само първата им буква да е главна (37 ред). Ако използвам ToUpper() ми ги изпечатва всички с главни букви, а ако използвам ToLower() ми ги изпечатва всички с малки букви. Как мога да печатам с главни букви единствено първата буква, а всички други да си останат с малки? Заради този проблем с печатането Judge ми дава 33/100. https://judge.softuni.bg/Contests/Practice/Index/1215#5
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 2 nd digit after the decimal separator.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
namespace Objects
{
class MainClass
{
public static void Main()
{
List<Vehicle> listVehicles = new List<Vehicle>();
string command = string.Empty;
while ((command = Console.ReadLine()) != "End")
{
string[] commandArr = command.Split().ToArray();
string type = commandArr[0];
string model = commandArr[1];
string colour = commandArr[2];
int horsepower = int.Parse(commandArr[3]);
Vehicle newVehicle = new Vehicle(type, model, colour, horsepower);
listVehicles.Add(newVehicle);
}
string inputModel = string.Empty;
while((inputModel=Console.ReadLine()) != "Close the Catalogue")
{
var newVehicle = listVehicles.FirstOrDefault(v => v.Model == inputModel);
if (newVehicle != null)
{
Console.WriteLine($"Type: {newVehicle.Type.ToUpper()}\nModel: {newVehicle.Model}\nColor: {newVehicle.Colour}\nHorsepower: {newVehicle.HorsePower}");
}
else
{
continue;
}
}
var listOfCars = listVehicles.FindAll(v => v.Type == "car");
var carsHorsePower = listOfCars.Sum(h => h.HorsePower);
if (listOfCars.Count == 0)
{
var carsAverageHorsePower = 0;
Console.WriteLine(($"Cars have average horsepower of: {carsAverageHorsePower:f2}."));
}
else
{
var carsAverageHorsePower = carsHorsePower / listOfCars.Count();
Console.WriteLine($"Cars have average horsepower of: {carsAverageHorsePower:f2}.");
}
var listOfTruck = listVehicles.FindAll(v => v.Type == "truck");
var truckHorsePower = listOfTruck.Sum(v => v.HorsePower);
if (listOfTruck.Count == 0)
{
var truckAverageHorsePower = 0;
Console.WriteLine($"Trucks have average horsepower of: {truckAverageHorsePower:f2}.");
}
else if(listOfTruck.Count>0)
{
var truckAverageHorsePower = truckHorsePower / listOfTruck.Count();
Console.WriteLine($"Trucks have average horsepower of: {truckAverageHorsePower:f2}.");
}
}
public class Vehicle
{
public string Type { get; set; }
public string Model { get; set; }
public string Colour { get; set; }
public int HorsePower { get; set; }
public Vehicle(string type, string model, string colour, int horsepower)
{
this.Type = type;
this.Model = model;
this.Colour = colour;
this.HorsePower = horsepower;
}
}
}
}
Thank you very much!
I want to read more about StringBuilder. It seems it is the only way to print the first letter of a string with lower or upper case.
Best regards!
Elena