Example #1
0
        static void Main()
        {
            var listOfVehicles = new Catalogue();

            listOfVehicles.Cars   = new List <Cars>();
            listOfVehicles.Trucks = new List <Trucks>();

            double sumCarPower   = 0;
            double sumTruckPower = 0;

            while (true)
            {
                string input = Console.ReadLine();
                if (input == "End")
                {
                    break;
                }
                string[] inputVehicle  = input.Split();
                string   typeOfVehicle = inputVehicle[0];
                string   model         = inputVehicle[1];
                string   color         = inputVehicle[2];
                int      horsePower    = int.Parse(inputVehicle[3]);

                if (typeOfVehicle.ToLower() == "car")
                {
                    var Car = new Cars(model, color, horsePower);
                    listOfVehicles.Cars.Add(Car);
                    sumCarPower += horsePower;
                }
                else if (typeOfVehicle.ToLower() == "truck")
                {
                    var Truck = new Trucks(model, color, horsePower);
                    listOfVehicles.Trucks.Add(Truck);
                    sumTruckPower += horsePower;
                }
            }

            while (true)
            {
                string inputModel = Console.ReadLine();
                if (inputModel == "Close the Catalogue")
                {
                    break;
                }
                if (listOfVehicles.Cars.Any(x => x.Model == inputModel))
                {
                    foreach (var car in listOfVehicles.Cars.Where(x => x.Model == inputModel))
                    {
                        Console.WriteLine($"Type: {car.Type} \n" +
                                          $"Model: {car.Model} \n" +
                                          $"Color: {car.Color} \n" +
                                          $"Horsepower: {car.HorsePower}");
                    }
                }
                else if (listOfVehicles.Trucks.Any(x => x.Model == inputModel))
                {
                    foreach (var truck in listOfVehicles.Trucks.Where(x => x.Model == inputModel))
                    {
                        Console.WriteLine($"Type: {truck.Type} \n" +
                                          $"Model: {truck.Model} \n" +
                                          $"Color: {truck.Color} \n" +
                                          $"Horsepower: {truck.HorsePower}");
                    }
                }
            }
            double averagePowerCars   = ((double)sumCarPower / listOfVehicles.Cars.Count);
            double averagePowerTrucks = ((double)sumTruckPower / listOfVehicles.Trucks.Count);

            if (listOfVehicles.Cars.Count > 0)
            {
                Console.WriteLine($"Cars have average horsepower of: {averagePowerCars:f2}.");
            }
            else
            {
                Console.WriteLine($"Cars have average horsepower of: {0:f2}.");
            }
            if (listOfVehicles.Trucks.Count > 0)
            {
                Console.WriteLine($"Trucks have average horsepower of: {averagePowerTrucks:f2}.");
            }
            else
            {
                Console.WriteLine($"Trucks have average horsepower of: {0:f2}.");
            }
        }
        static void Main(string[] args)
        {
            List <Catalogue> catalogues = new List <Catalogue>();

            while (true)
            {
                string[] info = Console.ReadLine().Split();
                string   type = info[0];
                if (type == "End")
                {
                    break;
                }
                else
                {
                    string brand = info[1];
                    string color = info[2];
                    int    power = int.Parse(info[3]);

                    Catalogue catalogue = new Catalogue(type, brand, color, power);

                    catalogues.Add(catalogue);
                }
            }


            while (true)
            {
                var printModel = Console.ReadLine();

                if (printModel == "Close the Catalogue")
                {
                    break;
                }
                else
                {
                    Console.WriteLine(string.Join(" ", catalogues.Where(x => x.Model == printModel)));
                }
            }

            List <Catalogue> cars   = catalogues.Where(x => x.Type == "car").ToList();
            List <Catalogue> trucks = catalogues.Where(x => x.Type == "truck").ToList();

            if (cars.Count > 0)
            {
                double averageCarHp = cars.Average(x => x.HorsePower);
                Console.WriteLine($"Cars have average horsepower of: {averageCarHp:f2}.");
            }
            else
            {
                Console.WriteLine($"Cars have average horsepower of: {cars.Sum(x => x.HorsePower):f2}.");
            }

            if (trucks.Count > 0)
            {
                double averageTruckHp = trucks.Average(x => x.HorsePower);

                Console.WriteLine($"Trucks have average horsepower of: {averageTruckHp:f2}.");
            }
            else
            {
                Console.WriteLine($"Cars have average horsepower of: {trucks.Sum(x => x.HorsePower):f2}.");
            }
        }