private static void Main(string[] args)
        {
            var carFactory = new CarFactory();
            var calculator = new Calculator();

            /*
             * 1. Benz G65 from Germany (Europe county), 6.0L, original price $217,900USD
             * 2. Honda Jazz 1.5L (Japan) original price $19,490 USD
             * 3. Jeep wrangler 3.6L (USA) original price, $36,995USD
             * 4. Chery QQ 1.0L (China) original price, $6,000USD
             */

            var benzG65             = carFactory.CreateCar("Benz G65", 217900m, 6.0m, StaticData.Country.Germany);
            var benzG65EndUserPrice = calculator.CalculateEndUserPrice(benzG65);

            var hondaJazz             = carFactory.CreateCar("Honda Jazz", 19490m, 1.5m, StaticData.Country.Japan);
            var hondaJazzEndUserPrice = calculator.CalculateEndUserPrice(hondaJazz);

            var jeepWrangler             = carFactory.CreateCar("Jeep wrangler", 36995m, 3.6m, StaticData.Country.USA);
            var jeepWranglerEndUserPrice = calculator.CalculateEndUserPrice(jeepWrangler);

            //throw Exception
            var cheryQq             = carFactory.CreateCar("Chery QQ", 6000m, 1.0m, StaticData.Country.China);
            var cheryQqEndUserPrice = calculator.CalculateEndUserPrice(cheryQq);
        }
Ejemplo n.º 2
0
 public void BuildTestContext(CarFactory cf)
 {
     AbstractCar c1 = cf.CreateCar();
     AbstractCar c2 = cf.CreateCar();
     AbstractCar c3 = cf.CreateCar();
     AbstractCar c4 = cf.CreateCar();
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 简单工厂模式测试
        /// </summary>
        static void SimpleFactory()
        {
            CarFactory carFactory = new CarFactory();
            ICar       audi       = carFactory.CreateCar("audi");

            audi.Run();
            ICar bmw = carFactory.CreateCar("bmw");

            bmw.Run();
        }
Ejemplo n.º 4
0
        public void RegisterDriver(List <string> commandArgs)
        {
            //•	RegisterDriver {type} {name} {hp} {fuelAmount} {tyreType} {tyreHardness}
            //•	RegisterDriver {type} {name} {hp} {fuelAmount} Ultrasoft {tyreHardness} { grip}
            if (commandArgs.Count > 8)
            {
                return;
            }
            string type         = commandArgs[1];
            string name         = commandArgs[2];
            int    hp           = int.Parse(commandArgs[3]);
            double fuelAmount   = double.Parse(commandArgs[4]);
            string tyreType     = commandArgs[5];
            double tyreHardness = double.Parse(commandArgs[6]);
            Tyre   tyre         = null;

            if (commandArgs.Count == 8)
            {
                double grip = double.Parse(commandArgs[7]);
                tyre = tyreFactory.CreateTyre(tyreType, tyreHardness, grip);
            }
            else
            {
                tyre = tyreFactory.CreateTyre(tyreType, tyreHardness);
            }
            if (tyre != null)
            {
                Car car = carFactory.CreateCar(hp, fuelAmount, tyre);
                if (car != null)
                {
                    Driver driver = driverFactory.CreateDriver(type, name, car);
                    drivers.Add(driver);
                }
            }
        }
Ejemplo n.º 5
0
        private void AddVehicles()
        {
            for (int i = 1; i <= 3; i++)
            {
                string[] vehicleArgs = Console.ReadLine()
                                       .Split(" ", StringSplitOptions.RemoveEmptyEntries);

                Vehicle vehicle = null;

                if (i == 1)
                {
                    vehicle = CarFactory.CreateCar(vehicleArgs);
                }
                else if (i == 2)
                {
                    vehicle = TruckFactory.CreateTruck(vehicleArgs);
                }
                else
                {
                    vehicle = BusFactory.CreateBus(vehicleArgs);
                }

                vehicles.Add(vehicle);
            }
        }
Ejemplo n.º 6
0
    public void Register(int id, string type, string brand, string model, int yearOfProduction, int horsepower,
                         int acceleration, int suspension, int durability)
    {
        var car = factory.CreateCar(type, brand, model, yearOfProduction, horsepower, acceleration, suspension, durability);

        cars[id] = car;
    }
Ejemplo n.º 7
0
        public void ShouldThrowNotSupportedExceptionWhenTypeIsInvalid(string type)
        {
            var    carFactory = new CarFactory();
            Action getCar     = () => carFactory.CreateCar(type);

            getCar.Should().ThrowExactly <NotSupportedException>();
        }
Ejemplo n.º 8
0
    //Deprecated (originally used for testing - would still work with a revision)
    public void SpawnCars(int howmany)
    {
        if (vehicles == null)
        {
            vehicles = new List <Car>();
        }
        //map.roads.Count
        int   toSpawn = howmany;
        int   spawned = 0;
        float prob    = 0.02f;

        for (int i = 0; spawned < toSpawn; i++)
        {
            if (AllParkingOccupied())
            {
                return;
            }
            foreach (ParkingSpace parking in parkingSpots)
            {
                if (UnityEngine.Random.Range(0f, 1f) < prob && !parking.occupied)
                {
                    Car c = CarFactory.CreateCar(parking.road);
                    vehicles.Add(c);
                    spawned++;
                }
            }
        }
    }
Ejemplo n.º 9
0
    public void RegisterDriver(List <string> commandArgs)
    {
        try
        {
            var    driverType   = commandArgs[0];
            var    driverName   = commandArgs[1];
            var    hp           = int.Parse(commandArgs[2]);
            var    fuelAmount   = double.Parse(commandArgs[3]);
            var    tyreType     = commandArgs[4];
            var    tyreHardness = double.Parse(commandArgs[5]);
            var    grip         = 0.0;
            Driver driver       = null;
            Car    car          = null;
            Tyre   tyre         = null;
            if (tyreType == "Ultrasoft")
            {
                grip = double.Parse(commandArgs[6]);
                tyre = TyreFactory.CreateUltraSoft(tyreHardness, grip);
            }
            else if (tyreType == "Hard")
            {
                tyre = TyreFactory.CreateHard(tyreHardness);
            }

            car    = CarFactory.CreateCar(hp, fuelAmount, tyre);
            driver = DriverFactory.CreateDriver(driverType, driverName, car);

            this.drivers.Add(driver);
        }
        catch
        {
        }
    }
    public void Register(int id, string type, string brand, string model,
                         int yearOfProduction, int horsepower, int acceleration, int suspension, int durability)
    {
        Car car = carFactory.CreateCar(type, brand, model,
                                       yearOfProduction, horsepower, acceleration, suspension, durability);

        this.cars.Add(id, car);
    }
Ejemplo n.º 11
0
        public decimal TestCarImportTaxRate(string name, decimal originalPrice, decimal capacity,
                                            StaticData.Country importFrom)
        {
            var carFactory = new CarFactory();
            var car        = carFactory.CreateCar(name, originalPrice, capacity, importFrom);

            return(car.ImportTaxRate);
        }
Ejemplo n.º 12
0
        public void ShouldCreatePetrolCarInstance(string type)
        {
            var carFactory = new CarFactory();
            var car        = carFactory.CreateCar(type);

            car.Should().BeOfType <PetrolCar>();
            car.GetName().Should().Be("Petrol");
        }
Ejemplo n.º 13
0
        public void ShouldCreateElectricCarInstance(string type)
        {
            var carFactory = new CarFactory();
            var car        = carFactory.CreateCar(type);

            car.Should().BeOfType <ElectricCar>();
            car.GetName().Should().Be("Electric");
        }
Ejemplo n.º 14
0
        public static void AddCar()
        {
            var car = _carFactory.CreateCar();

            _allCarsRepository.AddCar(car);
            var transaction = _transactionFactory.CreateTransaction(car, true);

            _allTransactionsRepository.AddTransaction(transaction);
        }
Ejemplo n.º 15
0
        static void FactoryMethodDemo()
        {
            var factory = new CarFactory();

            var sedanCar = factory.CreateCar(CarType.Sedan);

            sedanCar.Display();
            sedanCar.Drive();

            var hatchbackCar = factory.CreateCar(CarType.Hatchback);

            hatchbackCar.Display();
            hatchbackCar.Drive();

            var suvCar = factory.CreateCar(CarType.Suv);

            suvCar.Display();
            suvCar.Drive();
        }
        public decimal TestCalculateEndUserPrice(string name, decimal originalPrice, decimal capacity,
                                                 StaticData.Country importFrom)
        {
            var carFactory   = new CarFactory();
            var calculator   = new Calculator();
            var car          = carFactory.CreateCar(name, originalPrice, capacity, importFrom);
            var endUserPrice = calculator.CalculateEndUserPrice(car);

            return(endUserPrice);
        }
Ejemplo n.º 17
0
        public void TestCreateChinaCar(string name, decimal originalPrice, decimal capacity,
                                       StaticData.Country importFrom)
        {
            var carFactory = new CarFactory();

            var ex = Assert.Throws <Exception>(
                () => carFactory.CreateCar(name, originalPrice, capacity, importFrom)
                );

            StringAssert.Contains("Not support this country", ex.Message);
        }
Ejemplo n.º 18
0
    public void Register(int id, string type, string brand, string model, int yearOfProduction,
                         int horsepower, int acceleration, int suspension, int durability)
    {
        var carFactory  = new CarFactory();
        Car producedCar = carFactory.CreateCar(type, brand, model, yearOfProduction, horsepower, acceleration,
                                               suspension, durability);

        if (producedCar != null)
        {
            this.Cars.Add(id, producedCar);
        }
    }
Ejemplo n.º 19
0
 public void RegisterDriver(List <string> commandArgs)
 {
     try
     {
         Tyre   tyre   = TyreFactory.CreateTyre(commandArgs.Skip(4).ToList());
         Car    car    = CarFactory.CreateCar(commandArgs.Skip(2).Take(2).ToList(), tyre);
         Driver driver = DriverFactory.CreateDriver(commandArgs.Take(2).ToList(), car);
         this.drivers.Add(commandArgs[1], driver);
     }
     catch (ArgumentException ae)
     {
         Console.WriteLine(ae.Message);
     }
 }
Ejemplo n.º 20
0
    public void RegisterDriver(List <string> commandArgs)
    {
        try
        {
            var tyre   = TyreFactory.CreateTyre(commandArgs.Skip(4).ToArray());
            var car    = CarFactory.CreateCar(commandArgs.Skip(2).ToArray(), tyre);
            var driver = DriverFactory.CreateDriver(commandArgs.Take(2).ToArray(), car);
            this.activeDrivers.Add(driver);
        }

        catch (ArgumentException ae)
        {
        }
    }
    public Driver CreateDriver(List <string> arguments)
    {
        CarFactory    carFactory = new CarFactory();
        string        type       = arguments[0];
        string        name       = arguments[1];
        List <string> carArgs    = arguments.Skip(2).ToList();
        Car           car        = carFactory.CreateCar(carArgs);

        switch (type)
        {
        case "Endurance":
            return(new EnduranceDriver(name, car));

        case "Aggressive":
            return(new AggressiveDriver(name, car));

        default:
            throw new ArgumentException();
        }
    }
Ejemplo n.º 22
0
        static void FactoryExample()
        {
            Car PorcheCayman = CarFactory.CreateCar("Porche", "Cayman S", CarcassType.Coupe, 210, 25, 1.4f);

            PorcheCayman.SetDimensions(1.25f, 1.90f, 1.10f);

            Car Lancer = CarFactory.CreateCar("Mitsubishi", "Lancer", CarcassType.Coupe, 180, 27.5f);

            Car KiaRioXline = CarFactory.CreateCar("KIA", "Rio", CarcassType.SUV, 200, 29, 2.2f);

            KiaRioXline.SetDimensions(1.45f, 2.15f, 1.55f);

            KiaRioXline.ChangeWheels(new Wheel()
            {
                Diameter = 26.5f, Manufacturer = "Somebody", PatternDepth = 1.5f
            });

            Console.WriteLine(PorcheCayman);
            Console.WriteLine(Lancer);
            Console.WriteLine(KiaRioXline);
        }
Ejemplo n.º 23
0
    //Spawns cars from the resource asset at path levelName
    public void SpawnCars(string levelName)
    {
        string    file    = ReadText(levelName);
        JSONNode  objects = JSON.Parse(file);
        JSONArray a       = objects["cars"].AsArray;

        for (int i = 0; i < a.Count; ++i)
        {
            JSONNode     thiscar = a[i];
            ParkingSpace parking = FindParkingSpace(thiscar["x"].AsInt, thiscar["y"].AsInt);
            if (parking == null || parking.road == null)
            {
                Debug.Log("null");
            }
            Car c = CarFactory.CreateCar(parking.road);
            if (thiscar["tank"] != null)
            {
                c.tankLevel = thiscar["tank"].AsFloat;
            }
            c.plan.Trips = ReadDestinations(thiscar);
            world.vehicles.Add(c);
        }
    }
Ejemplo n.º 24
0
        public CrdTest()
        {
            test = new Test();

            inv.Sedans = new List <Car>
            {
                CarFactory.CreateCar(CarType.Sedan, new CarSpecs("Audi", "S", 2005)),
                CarFactory.CreateCar(CarType.Sedan, new CarSpecs("BMW", "4", 2006), "Turbo"),
                CarFactory.CreateCar(CarType.Sedan, new CarSpecs("Porsche", "Panera", 2011)),
            };
            inv.Trucks = new List <Car>
            {
                CarFactory.CreateCar(CarType.Truck, new CarSpecs("GM", "Tahoe", 2014)),
                CarFactory.CreateCar(CarType.Truck, new CarSpecs("Ford", "F150", 2016)),
                CarFactory.CreateCar(CarType.Truck, new CarSpecs("Chevrolet", "Silverado", 2018))
            };
            inv.Vans = new List <Car>
            {
                CarFactory.CreateCar(CarType.Van, new CarSpecs("Toyota", "Sienna", 2016)),
                CarFactory.CreateCar(CarType.Van, new CarSpecs("Honda", "Odyssey", 2018)),
                CarFactory.CreateCar(CarType.Van, new CarSpecs("Chrysler", "Pacifica", 2019), "Voyager")
            };
        }
Ejemplo n.º 25
0
        static void Main(string[] args)
        {
            Console.WriteLine("Start building cars");
            List <CarFactory> myCars = new List <CarFactory>();

            myCars.Add(CarFactory.CreateCar(CarTypes.FamaliyCar, CarSafety.FirstSafe, "Blue"));
            myCars.Add(CarFactory.CreateCar(CarTypes.SportCar, CarSafety.SecondSafe, "Red"));


            Console.WriteLine("Compare cars:");
            myCars = myCars.CompareCars(true);


            Console.WriteLine("Specific feature vs car -  {0}:", myCars[0]);

            if (myCars.HasSafety(car))
            {
                Console.WriteLine("Safety exist in {1} and is better: {0} [{2} ", myCars.HasSafety(FirstSafe), myCars.HasSafety(SecondSafe));
            }
            else
            {
                Console.WriteLine("Safety does not exist in {0}", car);
            }
        }
Ejemplo n.º 26
0
 public Client(CarFactory carFactory)
 {
     Car    = carFactory.CreateCar();
     Engine = carFactory.CreateEngine();
     Body   = carFactory.CreateBody();
 }
Ejemplo n.º 27
0
 public Client(CarFactory carFactory)
 {
     _car    = carFactory.CreateCar();
     _engine = carFactory.CreateEngine();
     _body   = carFactory.CreateBody();
 }
Ejemplo n.º 28
0
        public static void GenerateCars(string FactoryName, int MaxCars)
        {
            _factoryName = FactoryName;
            // Keep sending.
            while (true)
            {
                Console.WriteLine("Starting cars");
                for (int i = 0; i < MaxCars; i++)
                {
                    Car newCar = CarFactory.CreateCar(_factoryName);
                    Console.WriteLine("Created a new car {0} of type {1}", newCar.Id, newCar.TypeOfCar);
                }
                while (true)
                {
                    int currentSpeed = 0;
                    int maxSpeed     = 0;
                    VehicleTireReading currentReading = null;
                    //Speed it up
                    Console.WriteLine("Speed up the cars");
                    foreach (Car currentCar in CarFactory.Cars)
                    {
                        maxSpeed = currentCar.CalcMaxSpeed();
                        do
                        {
                            currentReading = currentCar.ReadTires();
                            writeToConsole(currentReading, currentCar);

                            //If we have a flat, stop the car
                            if (currentReading.HasFlat)
                            {
                                maxSpeed     = 0;
                                currentSpeed = 0;
                                currentCar.Stop();
                            }
                            else
                            {
                                currentCar.Move(new Random().Next(1, 10));
                                sendCarDataToEventHub(currentCar, currentReading);
                                currentSpeed = currentReading.CurrentSpeed;
                                //Sleep to simulate traffic
                                if (currentSpeed == new Random().Next(0, maxSpeed))
                                {
                                    Thread.Sleep(new Random().Next(500, 3000));
                                    currentCar.AddAirToTires(new Random().Next(0, 3), new Random().Next(0, 5));
                                    break;
                                }
                            }
                        }while (currentSpeed < maxSpeed);
                    }

                    Thread.Sleep(new Random().Next(500, 3000));
                    //Now slow it down
                    Console.WriteLine("Slow down the car");
                    foreach (Car currentCar in CarFactory.Cars)
                    {
                        do
                        {
                            currentCar.Slow(new Random().Next(1, 10));
                            currentReading = currentCar.ReadTires();
                            if (!currentReading.HasFlat)
                            {
                                writeToConsole(currentReading, currentCar);
                                sendCarDataToEventHub(currentCar, currentReading);
                                currentSpeed = currentCar.CurrentSpeed;
                                if (currentSpeed == 0)
                                {
                                    Thread.Sleep(new Random().Next(500, 3000));
                                    currentCar.RemoveAirFromTires(new Random().Next(0, 3), new Random().Next(0, 5));
                                    break;
                                }
                            }
                        }while (currentSpeed > 0);
                    }
                }
            }
        }
Ejemplo n.º 29
0
        // POST api/<controller>
        public void Post([FromBody] IncomingCar car)
        {
            var newCar = _service.Upsert(CarFactory.CreateCar(car.LicenseNumber));

            Console.WriteLine(newCar);
        }
Ejemplo n.º 30
0
 public Client(CarFactory car_factory)
 {
     abstractCar    = car_factory.CreateCar();
     abstractEngine = car_factory.CreateEngine();
 }