Esempio n. 1
0
 static void Main(string[] args)
 {
     /*******************************************************************************************
      * There could have been several approaches to this problem
      * 1)Taking this as directed graph.
      *
      * 2)Making a global time controller which could raise ticks and synchronise all objects
      *  like traffic lights, driver's action based on every tick.But this approach is more suitable
      *  in case of simulations when there are several objecs etc.
      *
      *
      * 3)So, I have taken the third approach as per problem scope and to maintain simplicity.
      *
      * The following assumptions have been made :
      * All units are same..like time in minutes, secs or hrs
      * Traffic light will assume the next light if the previous light duration is just over.
      * i.e during the transition of lights old to new ...new light will be assumed to operate.
      *
      * Car is in  "Start" state and at the start of journey and lights are green
      *
      * Basic design is :
      *
      * Route is composed of places which could be stretch or traffic light
      * Driver will select driving strategy based on place
      * Traffic light will give the waiting time based on elapsed time ..time passed since start of journey.
      *
      *
      *
      * Test cases have assumed same time unit..for all entities
      * * *******************************************************************************************/
     //Please take all values in same unit preferably minute
     //sample input
     ICar car = new Car(10/60.0);//10 secs
     car.Start();
     List<IPlace> orderedPlaces = new List<IPlace>();
     orderedPlaces.Add(new RoadStretch(60/60.0, 150));//  km/hr to km/minutes
     orderedPlaces.Add(new TrafficLight(true, 1, 2));
     orderedPlaces.Add(new RoadStretch(30/60.0, 120));
     orderedPlaces.Add(new TrafficLight(true, 2, 2));
     orderedPlaces.Add(new RoadStretch(50/60.0, 80));
     Route route = new Route(orderedPlaces);
     IDriver driver = new Driver();
     Console.WriteLine(driver.Drive(car,route));//in minutes
     car.Stop();
     Console.ReadLine();
 }
Esempio n. 2
0
        public void Drive()
        {
            Driver d = new Driver();
            Car c = new Car(10);

            //All green lights
            List<IPlace> gPlaces = new List<IPlace>();
            gPlaces.Add(new  RoadStretch(50, 150));
            gPlaces.Add(new TrafficLight(false, 2, 3));
            gPlaces.Add(new RoadStretch(30, 120));
            gPlaces.Add(new RoadStretch(30, 90));
            gPlaces.Add(new TrafficLight(true, 2, 1));
            gPlaces.Add(new RoadStretch(50, 150));

            IRoute route = new Route(gPlaces);
            double t = d.Drive(c, route);
            Assert.AreEqual(13, t);

            //All red lights

            List<IPlace> rPlaces = new List<IPlace>();
            rPlaces.Add(new RoadStretch(50, 150));
            rPlaces.Add(new TrafficLight(false, 1, 2));
            rPlaces.Add(new RoadStretch(30, 120));
            rPlaces.Add(new RoadStretch(30, 90));
            rPlaces.Add(new TrafficLight(true, 2, 2));
            rPlaces.Add(new RoadStretch(50, 150));

            route = new Route(rPlaces);
            c.Reset();
            t = d.Drive(c, route);
            Assert.AreEqual(37, t);

            //general case

            List<IPlace> orderedPlaces = new List<IPlace>();
            orderedPlaces.Add(new RoadStretch(50, 150));
            route = new Route(orderedPlaces);
            c.Reset();
            t = d.Drive(c, route);
            Assert.AreEqual(3, t);

            orderedPlaces.Add(new RoadStretch(30, 120));
            orderedPlaces.Add(new TrafficLight(true, 2, 2));
            c.Reset();
            t = d.Drive(c, route);

            Assert.AreEqual(18, t, "result doesn't  match");

            orderedPlaces.Add(new RoadStretch(50, 150));
            orderedPlaces.Add(new TrafficLight(true, 1, 2));
            c.Reset();
            t = d.Drive(c, route);
            Assert.AreEqual(21, t);

            //Thumb rule

            //Total distance covered by car should be equal to the length of all stretches in the route
            double totalDistance = c.GetMeterReading();
            Assert.AreEqual(totalDistance,420);

            c.Stop();
        }