Exemple #1
0
        public List <Bus> GetBus(RoutesData route, string station)
        {
            ICalculator _calculator = new SimpleCalculator();
            int         Min1;
            int         Min2;
            DateTime    time   = DateTime.Now;
            List <Bus>  Busses = new List <Bus>();

            if (station == route.RouteStops[0])
            {
                Min1 = _calculator.GetMinutes(route, route.RouteStops.IndexOf(station), time, true);
                Bus Vehicle1 = new Bus
                {
                    Route           = route,
                    Minute          = Min1,
                    LastStation     = route.RouteStops[route.RouteStops.Count - 1],
                    SelectedStation = station
                };
                Busses.Add(Vehicle1);
            }
            if (station == route.RouteStops[route.RouteStops.Count - 1])
            {
                Min2 = _calculator.GetMinutes(route, route.RouteStops.IndexOf(station), time, false);
                Bus Vehicle2 = new Bus
                {
                    Route           = route,
                    Minute          = Min2,
                    LastStation     = route.RouteStops[0],
                    SelectedStation = station
                };
                Busses.Add(Vehicle2);
            }


            if (station != route.RouteStops[route.RouteStops.Count - 1] && station != route.RouteStops[0])
            {
                Min1 = _calculator.GetMinutes(route, route.RouteStops.IndexOf(station), time, true);
                Min2 = _calculator.GetMinutes(route, route.RouteStops.IndexOf(station), time, false);
                Bus Vehicle1 = new Bus
                {
                    Route           = route,
                    Minute          = Min1,
                    LastStation     = route.RouteStops[route.RouteStops.Count - 1],
                    SelectedStation = station
                };
                Bus Vehicle2 = new Bus
                {
                    Route           = route,
                    Minute          = Min2,
                    LastStation     = route.RouteStops[0],
                    SelectedStation = station
                };
                Busses.Add(Vehicle1);
                Busses.Add(Vehicle2);
            }

            Busses = Busses.OrderBy(Bus => Bus.Minute).ToList();
            return(Busses);
        }
        public int GetMinutes(RoutesData route, int i, DateTime time, bool x)
        {
            int minutes = 0;
            int length  = 0;

            if (x)
            {
                for (int m = 0; m < i; ++m)
                {
                    length += route.Distance[m];
                }
            }

            if (x == false)
            {
                for (int m = i; m < route.Distance.Count; ++m)
                {
                    length += route.Distance[m];
                }
            }

            if ((time.Hour > route.Finish.Hour && time.Hour < route.Start.Hour) ||
                (time.Hour == route.Finish.Hour && time.Minute > route.Finish.Minute) ||
                (time.Hour == route.Start.Hour) && (time.Minute < route.Start.Minute))
            {
                int betweenMinutes;
                if (time.Hour > route.Start.Hour)
                {
                    betweenMinutes = (1440 - time.Hour * 60 - time.Minute) + route.Start.Hour * 60 + route.Start.Minute;
                }
                else
                {
                    betweenMinutes = route.Start.Hour * 60 + route.Start.Minute - time.Hour * 60 - time.Minute;
                }
                minutes = length + betweenMinutes;
                return(minutes);
            }

            bool a = true;
            int  HowManyMinutesBeforeLastBus = (time.Hour * 60 + time.Minute - route.Start.Hour * 60 -
                                                route.Start.Minute) % route.Interval;

            if (length < HowManyMinutesBeforeLastBus)
            {
                minutes = route.Interval - HowManyMinutesBeforeLastBus + length;
                return(minutes);
            }

            do
            {
                if ((length - HowManyMinutesBeforeLastBus) >= 0)
                {
                    minutes = length - HowManyMinutesBeforeLastBus;
                    HowManyMinutesBeforeLastBus = HowManyMinutesBeforeLastBus + route.Interval;
                }
                else
                {
                    a = false;
                }
            } while (a == true);

            return(minutes);
        }