static void Main(string[] args) { List <VehicleCatalogue> vehicleCatalogue = new List <VehicleCatalogue>(); while (true) { string input = Console.ReadLine(); if (input == "End") { break; } string[] arrInput = input.Split(" ", StringSplitOptions.RemoveEmptyEntries); string typeOfVehicle = arrInput[0]; string model = arrInput[1]; string color = arrInput[2]; int horsepower = int.Parse(arrInput[3]); VehicleCatalogue vehicle = new VehicleCatalogue(typeOfVehicle, model, color, horsepower); vehicleCatalogue.Add(vehicle); } while (true) { string inputModel = Console.ReadLine(); if (inputModel == "Close the Catalogue") { break; } foreach (var item in vehicleCatalogue) { if (item.Model == inputModel) { if (item.Type == "car") { Console.WriteLine($"Type: Car"); } else { Console.WriteLine($"Type: Truck"); } Console.WriteLine($"Model: {item.Model}"); Console.WriteLine($"Color: {item.Color}"); Console.WriteLine($"Horsepower: {item.Horsepower}"); } } } List <VehicleCatalogue> cars = vehicleCatalogue.Where(x => x.Type == "car").ToList(); Console.WriteLine($"Cars have average horsepower of: {GetAverage(cars):f2}."); List <VehicleCatalogue> trucks = vehicleCatalogue.Where(x => x.Type == "truck").ToList(); Console.WriteLine($"Trucks have average horsepower of: {GetAverage(trucks):f2}."); }
static void Main(string[] args) { // {typeOfVehicle} {model} {color} {horsepower} string input = Console.ReadLine(); List <VehicleCatalogue> roster = new List <VehicleCatalogue>(); while (input != "End") { string[] tokens = input.Split(); string type = tokens[0]; string model = tokens[1]; string colour = tokens[2]; double horsePower = double.Parse(tokens[3]); VehicleCatalogue catalogue = new VehicleCatalogue(type, model, colour, horsePower); roster.Add(catalogue); input = Console.ReadLine(); } string inputTwo = Console.ReadLine(); while (inputTwo != "Close the Catalogue") { string model = inputTwo; VehicleCatalogue printCar = roster.First(x => x.Model == model); Console.WriteLine(printCar); inputTwo = Console.ReadLine(); } List <VehicleCatalogue> onlyCars = roster.Where(x => x.Type == "car").ToList(); List <VehicleCatalogue> onlyTrucks = roster.Where(x => x.Type == "truck").ToList(); double sumCarsHp = onlyCars.Sum(x => x.HorsePower); double sumTrucksHp = onlyTrucks.Sum(x => x.HorsePower); double averageCarHp = 0; // there is a chance that there are either no cars or no trucks double averageTruckHp = 0; // so division by 0 is invalid if (onlyCars.Count > 0) //?? { averageCarHp = sumCarsHp / onlyCars.Count; } if (onlyTrucks.Count > 0) { averageTruckHp = sumTrucksHp / onlyTrucks.Count; } Console.WriteLine($"Cars have average horsepower of: {averageCarHp:f2}."); Console.WriteLine($"Trucks have average horsepower of: {averageTruckHp:f2}."); }
static void Main() { List <VehicleCatalogue> catalogue = new List <VehicleCatalogue>(); while (true) { List <string> command = Console.ReadLine() .Split(" ") .ToList(); if (command[0] == "End") { break; } string type = command[0]; string model = command[1]; string color = command[2]; int horsePower = int.Parse(command[3]); VehicleCatalogue vehicle = new VehicleCatalogue(type, model, color, horsePower); catalogue.Add(vehicle); } List <string> modelPrint = new List <string>(); while (true) { string input = Console.ReadLine(); if (input == "Close the Catalogue") { break; } modelPrint.Add(input); } foreach (var model in modelPrint) { foreach (var vehicleColection in catalogue) { if (vehicleColection.Model == model) { Console.WriteLine($"Type: {vehicleColection.Type.First().ToString().ToUpper() + vehicleColection.Type.Substring(1)}"); Console.WriteLine($"Model: {vehicleColection.Model}"); Console.WriteLine($"Color: {vehicleColection.Color}"); Console.WriteLine($"Horsepower: {vehicleColection.HorsePower}"); break; } } } double averageHorsepowerCar = 0; double averageHorsepowerTruck = 0; int counterCar = 0; int counterTruck = 0; foreach (var model in catalogue) { if (model.Type == "car") { averageHorsepowerCar += model.HorsePower; counterCar++; } else if (model.Type == "truck") { averageHorsepowerTruck += model.HorsePower; counterTruck++; } } averageHorsepowerCar /= counterCar; averageHorsepowerTruck /= counterTruck; if (counterCar > 0) { Console.WriteLine($"Cars have average horsepower of: {averageHorsepowerCar:F2}."); } else { Console.WriteLine($"Cars have average horsepower of: {0:F2}."); } if (counterTruck > 0) { Console.WriteLine($"Trucks have average horsepower of: {averageHorsepowerTruck:F2}."); } else { Console.WriteLine($"Trucks have average horsepower of: {0:F2}."); } }