Exemplo n.º 1
0
        public string DisplayVehicleCatalog()
        {
            var tempCars   = Cars.OrderBy(car => car.Brand).ToList();
            var tempTrucks = Trucks.OrderBy(truck => truck.Brand).ToList();
            var sb         = new StringBuilder();

            if (Cars.Count > 0)
            {
                sb.AppendLine("Cars:");
                foreach (var car in tempCars)
                {
                    sb.AppendLine($"{car.Brand}: {car.Model} - {car.HorsePower}hp");
                }
            }
            if (Trucks.Count > 0)
            {
                sb.AppendLine("Trucks:");
                foreach (var truck in tempTrucks)
                {
                    sb.AppendLine($"{truck.Brand}: {truck.Model} - {truck.Weight}kg");
                }
            }

            return(sb.ToString());
        }
Exemplo n.º 2
0
        public void AddVehicleToCatalog(string vehicle)
        {
            Truck   truck   = new Truck();
            Car     car     = new Car();
            Catalog catalog = new Catalog();

            switch (vehicle.Split('/')[0])
            {
            case "Truck":
                truck.Brand  = vehicle.Split('/')[1];
                truck.Model  = vehicle.Split('/')[2];
                truck.Weight = int.Parse(vehicle.Split('/')[3]);
                Trucks.Add(truck);
                break;

            case "Car":
                car.Brand      = vehicle.Split('/')[1];
                car.Model      = vehicle.Split('/')[2];
                car.HorsePower = int.Parse(vehicle.Split('/')[3]);
                Cars.Add(car);
                break;
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            List <string> command = Console.ReadLine().Split().ToList();
            List <Cars>   cars    = new List <Cars>();
            List <Trucks> trucks  = new List <Trucks>();

            while (command[0] != "End")
            {
                if (command[0] == "car")
                {
                    Cars temp = new Cars();
                    temp.Model = command[1];
                    temp.Color = command[2];
                    temp.HP    = int.Parse(command[3]);
                    cars.Add(temp);
                }
                else if (command[0] == "truck")
                {
                    Trucks temp = new Trucks();
                    temp.Model = command[1];
                    temp.Color = command[2];
                    temp.HP    = int.Parse(command[3]);
                    trucks.Add(temp);
                }
                command = Console.ReadLine().Split().ToList();
            }

            string model = Console.ReadLine();

            while (model != "Close the Catalogue")
            {
                if (cars.Select(x => x.Model).Contains(model))
                {
                    int indexOfCar = cars.FindIndex(x => x.Model == model);

                    Console.WriteLine("Type: Car");
                    Console.WriteLine($"Model: {cars[indexOfCar].Model}");
                    Console.WriteLine($"Color: {cars[indexOfCar].Color}");
                    Console.WriteLine($"Horsepower: {cars[indexOfCar].HP}");
                }
                else if (trucks.Select(x => x.Model).Contains(model))
                {
                    int indexOftruck = trucks.FindIndex(x => x.Model == model);

                    Console.WriteLine("Type: Truck");
                    Console.WriteLine($"Model: {trucks[indexOftruck].Model}");
                    Console.WriteLine($"Color: {trucks[indexOftruck].Color}");
                    Console.WriteLine($"Horsepower: {trucks[indexOftruck].HP}");
                }
                model = Console.ReadLine();
            }

            double trucksAverage = 0;
            double carsAverage   = 0;

            if (trucks.Count != 0)
            {
                trucksAverage = trucks.Average(x => x.HP);
            }
            if (cars.Count != 0)
            {
                carsAverage = cars.Average(x => x.HP);
            }

            Console.WriteLine($"Cars have average horsepower of: {carsAverage:f2}.");
            Console.WriteLine($"Trucks have average horsepower of: {trucksAverage:f2}.");
        }