public Car(string model, Engine engine) { this.model = model; this.engine = engine; this.weight = 0; this.color = "n/a"; }
public Car(string model, Engine engine, string weight = "n/a", string color = "n/a") { this.Model = model; this.Engine = engine; this.Weight = weight; this.Color = color; }
public Car(string model, Engine engine, int weight, string color) : this(model, engine) { this.weight = weight; this.color = color; }
public Car(string model, Engine engine, string color) : this(model, engine) { this.color = color; }
public Car(string model, Engine engine, int weight) : this(model, engine) { this.weight = weight; }
static void Main(string[] args) { Dictionary<string, Engine> engines = new Dictionary<string, Engine>(); Dictionary<string, Car> cars = new Dictionary<string, Car>(); int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { var input = Console.ReadLine().Trim().Split(); Engine engine = null; if (input.Length == 3) { try { int displacement = int.Parse(input[2]); engine = new Engine(input[0], input[1], input[2], "n/a"); } catch (Exception) { string efficiency = input[2]; engine = new Engine(input[0], input[1], "n/a", efficiency); } } else if (input.Length == 4) { engine = new Engine(input[0], input[1], input[2], input[3]); } else { engine = new Engine(input[0], input[1], "n/a", "n/a"); } engines[engine.Model] = engine; } n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { var input = Console.ReadLine().Trim().Split(); Car car = null; if (input.Length == 3) { try { int weight = int.Parse(input[2]); car = new Car(input[0], engines[input[1]], input[2],"n/a"); } catch (Exception) { string color = input[2]; car = new Car(input[0], engines[input[1]], "n/a", color); } } else if (input.Length == 4) { car = new Car(input[0], engines[input[1]], input[2], input[3]); } else { car = new Car(input[0], engines[input[1]], "n/a", "n/a"); } cars[car.Model] = car; } foreach (var car in cars) { Console.WriteLine(car.Value); } }
static void Main(string[] args) { int enginesCount = int.Parse(Console.ReadLine()); List<Engine> engines = new List<Engine>(); for(int i=0; i<enginesCount; i++) { string[] tokens = Console.ReadLine().Split(new char[0], StringSplitOptions.RemoveEmptyEntries); string model = tokens[0]; string power = tokens[1]; if(tokens.Length == 2) { engines.Add(new Engine(model, power)); } else if(tokens.Length == 3) { int displacement; bool isDisplacement = int.TryParse(tokens[2], out displacement); if(isDisplacement) { engines.Add(new Engine(model, power, displacement)); } else { string efficiency = tokens[2]; engines.Add(new Engine(model, power, efficiency)); } } else if(tokens.Length == 4) { int displacement = int.Parse(tokens[2]); string efficiency = tokens[3]; engines.Add(new Engine(model, power, displacement, efficiency)); } } int carsCount = int.Parse(Console.ReadLine()); List<Car> cars = new List<Car>(); for(int i=0; i<carsCount; i++) { string[] tokens = Console.ReadLine().Split(new char[0], StringSplitOptions.RemoveEmptyEntries); string model = tokens[0]; string engineModel = tokens[1]; Engine engine = new Engine(null, null); foreach(Engine eng in engines) { if(eng.model == engineModel) { engine = eng; } } if(tokens.Length == 2) { cars.Add(new Car(model, engine)); } else if (tokens.Length == 3) { int weight; bool isWeight = int.TryParse(tokens[2], out weight); if (isWeight) { cars.Add(new Car(model, engine, weight)); } else { string color = tokens[2]; cars.Add(new Car(model, engine, color)); } } else if (tokens.Length == 4) { int weight = int.Parse(tokens[2]); string color = tokens[3]; cars.Add(new Car(model, engine, weight, color)); } } foreach(Car car in cars) { Console.WriteLine("{0}:", car.model); Console.WriteLine(" {0}:",car.engine.model); Console.WriteLine(" Power: {0}", car.engine.power); Console.WriteLine(" Displacement: {0}", car.engine.displacement == 0 ? "n/a" : car.engine.displacement.ToString()); Console.WriteLine(" Efficiency: {0}", car.engine.efficiency); Console.WriteLine(" Weight: {0}", car.weight == 0 ? "n/a" : car.weight.ToString()); Console.WriteLine(" Color: {0}", car.color); } }