Ejemplo n.º 1
0
        public bool Validate(Car car)
        {
            var isValid = true;
            if (car.NumberOfDoors <= 0) isValid = false;
            if (car.BrandName == null) return false;
            if (string.IsNullOrWhiteSpace(car.ModelName)) isValid = false;

            if (car.NeedsEngine)
            {
                if (car.Engine == null) isValid = false;
                else
                {
                    // validate engine
                    if (!ValidateEngine(car.Engine)) isValid = false;
                }

            }
            else
            {
                if (car.Engine != null) isValid = false; // if NeedsEngine is false, Engine must be null
            }
            if (!car.Wheels.Any())
            {
                isValid = false; // must have some wheels
            }
            if (car.Wheels.Count > 4)
            {
                isValid = false; // must have no more than 4 wheels
            }

            return isValid;
        }
 private Car MakeValidCar()
 {
     var c = new Car();
     c.BrandName = "Renault";
     c.ModelName = "Megane";
     c.NumberOfDoors = 3;
     // add 4 wheels
     Enumerable.Range(0, 4).ToList()
         .ForEach(i => c.Wheels.Add(new Wheel(c, i)));
     c.Engine = new DieselEngine();
     return c;
 }