static void Main(string[] args) { VehicleCatalog catalog = new VehicleCatalog(); Console.WriteLine("Escribe una palabra:"); string keyWord = Console.ReadLine(); IteratorVehicle iterator = catalog.Search(keyWord); Vehicle vehicle; iterator.Start(); vehicle = iterator.Item(); if (vehicle != null) { while (vehicle != null) { vehicle.Preview(); iterator.Next(); vehicle = iterator.Item(); } } else { Console.WriteLine("No se encontraron elementos."); } Console.ReadKey(); }
static void Main(string[] args) { VehicleCatalog vehicleCatalog = new VehicleCatalog(); string[] command = Console.ReadLine().Split('/'); while (command[0] != "end") { string type = command[0]; string brand = command[1]; string model = command[2]; switch (type) { case "Car": int horsePower = int.Parse(command[3]); Car car = new Car() { Brand = brand, Model = model, HorsePower = horsePower }; vehicleCatalog.Cars.Add(car); break; case "Truck": int weight = int.Parse(command[3]); Truck truck = new Truck() { Brand = brand, Model = model, Weight = weight }; vehicleCatalog.Trucks.Add(truck); break; } command = Console.ReadLine().Split('/'); } if (vehicleCatalog.Cars.Count != 0) { Console.WriteLine("Cars:"); foreach (Car car in vehicleCatalog.Cars.OrderBy(c => c.Brand)) { Console.WriteLine($"{car.Brand}: {car.Model} - {car.HorsePower}hp"); } } if (vehicleCatalog.Trucks.Count != 0) { Console.WriteLine("Trucks:"); foreach (Truck truck in vehicleCatalog.Trucks.OrderBy(t => t.Brand)) { Console.WriteLine($"{truck.Brand}: {truck.Model} - {truck.Weight}kg"); } } }
static void Main(string[] args) { VehicleCatalog vehicleCatalog = new VehicleCatalog(); string[] vehicleData = Console.ReadLine().Split(); while (vehicleData[0] != "End") { string type = vehicleData[0]; string model = vehicleData[1]; string color = vehicleData[2]; int horsePower = int.Parse(vehicleData[3]); switch (type) { case "car": Car car = new Car() { Model = model, Color = color, Horsepower = horsePower }; vehicleCatalog.Cars.Add(car); break; case "truck": Truck truck = new Truck() { Model = model, Color = color, Horsepower = horsePower }; vehicleCatalog.Trucks.Add(truck); break; } vehicleData = Console.ReadLine().Split(); } string command = Console.ReadLine(); while (command != "Close the Catalogue") { if (vehicleCatalog.Cars.Any(c => c.Model == command)) { Car car = vehicleCatalog.Cars .Where(c => c.Model == command) .First(); Console.WriteLine($"Type: {car.GetType().Name}"); Console.WriteLine($"Model: {car.Model}"); Console.WriteLine($"Color: {car.Color}"); Console.WriteLine($"Horsepower: {car.Horsepower}"); } if (vehicleCatalog.Trucks.Any(t => t.Model == command)) { Truck truck = vehicleCatalog.Trucks .Where(t => t.Model == command) .First(); Console.WriteLine($"Type: {truck.GetType().Name}"); Console.WriteLine($"Model: {truck.Model}"); Console.WriteLine($"Color: {truck.Color}"); Console.WriteLine($"Horsepower: {truck.Horsepower}"); } command = Console.ReadLine(); } Console.WriteLine(vehicleCatalog.Cars.Count == 0 ? "Cars have average horsepower of: 0.00." : $"Cars have average horsepower of: {(double)vehicleCatalog.Cars.Sum(c => c.Horsepower) / vehicleCatalog.Cars.Count:F2}."); Console.WriteLine(vehicleCatalog.Trucks.Count == 0 ? "Trucks have average horsepower of: 0.00." : $"Trucks have average horsepower of: {(double)vehicleCatalog.Trucks.Sum(t => t.Horsepower) / vehicleCatalog.Trucks.Count:F2}."); }