コード例 #1
0
        /// <summary>
        /// Generates a car. Logs to a console in case user specifies car make not correctly and 'Null' car is chosen.
        /// </summary>
        /// <param name="parameters">Parameters for requested car.</param>
        /// <returns>A Car. Can be a 'Null' Car.</returns>
        public Car ProvideCar(CarParameters parameters)
        {
            Car car = null;

            switch (parameters.CarMake)
            {
            case "opel":
                car = new OpelCar();
                break;

            case "stanley":
                car = new StanleyCar();
                break;

            case "volvo":
                car = new VolvoCar();
                break;

            default:
                car = new NullCar();
                Console.WriteLine("Logging: Possible problem, a null car is created");
                break;
            }

            if (!(car is NullCar))
            {
                switch (parameters.EngineRequested)
                {
                case "diesel":
                    car.Engine = new DieselEngine();
                    break;

                case "electric":
                    car.Engine = new ElectricEngine();
                    break;

                case "gasoline":
                    car.Engine = new GasolineEngine();
                    break;

                case "steam":
                    car.Engine = new SteamEngine();
                    break;

                default:
                    car.Engine = new NoneEngine();
                    break;
                }

                switch (parameters.PurposeRequested)
                {
                case "groceries":
                    car.Purpose = new GroceriesPurpose();
                    break;

                case "racing":
                    car.Purpose = new RacingPurpose();
                    break;

                default:
                    car.Purpose = new NoPurpose();
                    break;
                }
            }

            return(car);
        }
コード例 #2
0
 public static ICar GetCarById(int Id) => Id >= 0 && Id < data.Count ? data[Id] : NullCar.Create();