Loading...
Elena123456 avatar Elena123456 235 Точки

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;  
            }

        }
    }
}
  

0
Fundamentals Module 27/08/2020 22:17:09
Axiomatik avatar Axiomatik 2422 Точки
Best Answer

There is no ready-made functionality that allows to print out the first letter of a string with upper or lower case, you just have to write out the word in your output mesage. I have also included an alternative with StringBuilder which will be heavily used at the exam for any given text-output and changed int horsePower to double to obtain the correct result for judge.

Best,

New Code(100%):

using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;

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];
                double horsepower = double.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);
                //StringBuilder sb = new StringBuilder();

                if (newVehicle.Type == "car")
                {
                    Console.WriteLine($"Type: Car");
                    Console.WriteLine($"Model: {newVehicle.Model}");
                    Console.WriteLine($"Color: {newVehicle.Colour}");
                    Console.WriteLine($"Horsepower: {newVehicle.HorsePower}");

                    // StringBuilder Alternative
                    //sb.AppendLine($"Type: Car");
                    //sb.AppendLine($"Model: {newVehicle.Model}");
                    //sb.AppendLine($"Color: {newVehicle.Colour}");
                    //sb.AppendLine($"Horsepower: {newVehicle.HorsePower}");
                }
                else if (newVehicle.Type == "truck")
                {
                    Console.WriteLine($"Type: Truck");
                    Console.WriteLine($"Model: {newVehicle.Model}");
                    Console.WriteLine($"Color: {newVehicle.Colour}");
                    Console.WriteLine($"Horsepower: {newVehicle.HorsePower}");

                    // StringBuilder Alternative
                    //sb.AppendLine($"Type: Car");
                    //sb.AppendLine($"Model: {newVehicle.Model}");
                    //sb.AppendLine($"Color: {newVehicle.Colour}");
                    //sb.AppendLine($"Horsepower: {newVehicle.HorsePower}");                    
                }

                //Console.WriteLine(sb.ToString().TrimEnd());
            }

            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 double HorsePower { get; set; }

            public Vehicle(string type, string model, string colour, double horsepower)
            {
                this.Type = type;
                this.Model = model;
                this.Colour = colour;
                this.HorsePower = horsepower;
            }
        }
    }
}

1
Elena123456 avatar Elena123456 235 Точки

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

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.