Ejemplo n.º 1
0
        /// <summary>
        /// initialize a busCollection with 10 bus routs which contain at least 40 stops
        /// </summary>
        public static void initializeBusRoute(ref BusCollection myBusCollection, ref List <BusStop> myUniqueStops)
        {
            //BusCollection myBusCollection = new BusCollection();
            Random   r          = new Random();
            BusRoute myBusRoute = new BusRoute();

            BusRoute[] myRoutes = new BusRoute[Areas.Length];
            int        myRandom;

            ///////////////////////////////////////////
            ///     INITAILIZEING 40 STOPS
            //////////////////////////////////////////
            for (int i = 0; i < stopName.Length; i++)
            {
                myStops[i]             = new stopLocation();
                myStops[i].m_location  = stopName[i];
                myStops[i].m_longitude = r.Next(34, 36) + (float)(r.NextDouble() % 0.2 + 0.3);
                if (myStops[i].m_longitude < 35)
                {
                    myStops[i].m_longitude += (float)r.NextDouble();
                }
                myStops[i].m_latitude = r.Next(31, 34) + (float)(r.NextDouble() % 0.3);
                if (myStops[i].m_longitude < 33)
                {
                    myStops[i].m_longitude += (float)r.NextDouble();
                }
                do
                {
                    myStops[i].m_busStationKey = r.Next(10000000);
                } while (myBusCollection.keyExist(myStops[i].m_busStationKey));
            }
            ///////////////////////////////////////////
            ///     INITAILIZEING 10 BUS ROUTES
            //////////////////////////////////////////
            for (int i = 0; i < 10; i++)
            {
                myBusRoute = new BusRoute();

                myBusRoute = initializeBus(myBusRoute, myBusCollection, myUniqueStops);

                myBusCollection.addFirstBusRoute(myBusRoute);
            }
            ///////////////////////////////////////////
            ///     ALL STOP ARE USED CHECK
            //////////////////////////////////////////
            bool   addedNewBus = false;
            bool   foundFirst  = false;
            string last        = "";

            myBusRoute = new BusRoute();
            for (int i = 0; i < stopName.Length; i++)
            {
                if (notUseDStops[i] == 0)
                {
                    BusStop myNewStop = new BusStop();
                    myNewStop.m_mylocation.m_busStationKey = myStops[i].m_busStationKey;
                    myNewStop.m_mylocation.m_latitude      = myStops[i].m_latitude;
                    myNewStop.m_mylocation.m_location      = myStops[i].m_location;
                    myNewStop.m_mylocation.m_longitude     = myStops[i].m_longitude;
                    myNewStop.m_mylocation.m_location      = myStops[i].m_location;
                    addedNewBus = true;
                    notUseDStops[i]++;
                    myBusRoute.add(myNewStop, myBusRoute.m_Stations.Count());
                    last = myStops[i].m_location;
                    if (!foundFirst)
                    {
                        myBusRoute.m_FirstStation = last;
                        foundFirst = true;
                    }
                    if (!myBusCollection.keyExist(myStops[i].m_busStationKey))
                    {
                        myBusCollection.m_BusKeysList.Add(myStops[i].m_busStationKey);
                        myUniqueStops.Add(myNewStop);
                    }
                }
            }
            if (foundFirst)
            {
                myBusRoute.m_LastStation = last;
                myBusRoute.m_Area        = Areas[r.Next(Areas.Length)];
                do
                {
                    myBusRoute.m_BusLine = r.Next(1000);
                } while (myBusCollection.busLineExist(myBusRoute));
            }
            myBusCollection.addFirstBusRoute(myBusRoute);
            /////////////////////////////////////////////////
            //   10 STOPS ARE USED IN MORE THAN 1 BUS  LINE
            //////////////////////////////////////////////////

            BusRoute myNewRoute = new BusRoute();

            for (int i = 0; i < 10; i++)
            {
                if (notUseDStops[i] < 2)
                {
                    BusStop myNewStop = new BusStop();
                    myNewStop.m_mylocation.m_busStationKey = myStops[i].m_busStationKey;
                    myNewStop.m_mylocation.m_latitude      = myStops[i].m_latitude;
                    myNewStop.m_mylocation.m_location      = myStops[i].m_location;
                    myNewStop.m_mylocation.m_longitude     = myStops[i].m_longitude; notUseDStops[i]++;
                    myNewRoute.add(myNewStop, myNewRoute.m_Stations.Count());
                    if (!myBusCollection.keyExist(myStops[i].m_busStationKey))
                    {
                        myBusCollection.m_BusKeysList.Add(myStops[i].m_busStationKey);
                        myUniqueStops.Add(myNewStop);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// User menu
        /// </summary>
        /// <param name="myBusCollection">the Collection of all the bus</param>
        public static void LineManagement(ref BusCollection myBusCollection)
        {
            List <BusStop> myUniqueStops = new List <BusStop>();

            initializeBusRoute(ref myBusCollection, ref myUniqueStops);
            bool          NotEnd = true;
            int           countBus = 0;
            BusStop       firstStop = new BusStop(), lastStop = new BusStop(), addeDStop = new BusStop();
            BusCollection pathCollection = new BusCollection();

            while (NotEnd)
            {
                Console.WriteLine("OPTIONS:\n1: ADD\n2: DELETE\n3: SEARCH\n4: PRINT \n5: EXIT");
                string choice = "";
                int    realChoice = 0, placeAdd = 0;
                realChoice = Program_6077_5711_01.getInt(choice, ref realChoice);
                switch (realChoice)
                {
                case (int)menuChoice.ADD:
                    Console.WriteLine("1: add bus Stop\n2: add busLine");
                    realChoice = Program_6077_5711_01.getInt(choice, ref realChoice);
                    switch (realChoice)
                    {
                    case (int)menuAdd.ADD_BUS_STOP:
                        do
                        {
                            Console.WriteLine("enter busLine range 0 - {0}:\n", myBusCollection.m_BusCollectionList.Count);
                            placeAdd = Program_6077_5711_01.getInt(choice, ref realChoice);
                            if (placeAdd > myBusCollection.m_BusCollectionList.Count || placeAdd < 0)
                            {
                                Console.WriteLine("ERROR");
                            }
                        } while (placeAdd > myBusCollection.m_BusCollectionList.Count || placeAdd < 0);
                        addeDStop = GetBusStop(myBusCollection, ref myUniqueStops);
                        try
                        {
                            myBusCollection[placeAdd].add(addeDStop, myBusCollection[placeAdd].m_Stations.Count);
                        }
                        catch (IndexOutOfRangeException ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        break;

                    case (int)menuAdd.ADD_BUS_ROUTE:
                    {
                        myBusCollection.addBusRoute(GetBusRoute(myBusCollection, ref myUniqueStops));
                    }
                    break;

                    default:
                        Console.WriteLine("wrong choice!!!");
                        break;
                    }
                    break;

                case (int)menuChoice.ERASE:
                    BusRoute temp = new BusRoute();
                    Console.WriteLine("enter 1 to erase bus route  2 - to erase stope");
                    realChoice = Program_6077_5711_01.getInt(choice, ref realChoice);
                    switch (realChoice)
                    {
                    case (int)menuDelete.DELETE_BUS_ROUTE:
                        Console.WriteLine("enter bus line key to be deleted:");
                        realChoice = Program_6077_5711_01.getInt(choice, ref realChoice);
                        try
                        {
                            myBusCollection.deleteBusRouteKey(realChoice);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);;
                        }
                        break;

                    case (int)menuDelete.DELETE_STOP:
                        Console.WriteLine("enter the line number details at the station for deletion");
                        FindAndDelete(myBusCollection, ref myUniqueStops);
                        break;

                    default:
                        Console.WriteLine("wrong choice!!!");
                        break;
                    }
                    break;

                case (int)menuChoice.SEARCH:
                    Console.WriteLine("1: print all busses that pass in a bus stop\n2:find all paths");
                    realChoice = Program_6077_5711_01.getInt(choice, ref realChoice);
                    switch (realChoice)
                    {
                    case (int)menuSearch.LINES_THAT_PASS_IN_STOP:
                        Console.WriteLine("enter bus stop key:");
                        realChoice = Program_6077_5711_01.getInt(choice, ref realChoice);
                        foreach (BusRoute bus in myBusCollection.BusStopList(realChoice))
                        {
                            try
                            {
                                bus.print();
                            }
                            catch (ArgumentNullException ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                        }
                        break;

                    case (int)menuSearch.FIND_PATH:
                        do
                        {
                            try
                            {
                                Console.WriteLine("enter first station key");
                                realChoice = Program_6077_5711_01.getInt(choice, ref realChoice);
                                firstStop  = myBusCollection.retStop(realChoice);
                                NotEnd     = true;
                            }
                            catch (NotFoundkey exception)
                            {
                                Console.WriteLine(exception.Message);
                                NotEnd = false;
                            }
                        } while (!NotEnd);
                        do
                        {
                            try
                            {
                                Console.WriteLine("enter last station key");
                                realChoice = Program_6077_5711_01.getInt(choice, ref realChoice);
                                lastStop   = myBusCollection.retStop(realChoice);
                            }
                            catch (NotFoundkey exception)
                            {
                                Console.WriteLine(exception.Message);
                                NotEnd = false;
                            }
                        } while (!NotEnd);
                        if (!(lastStop.m_distance == 0 || firstStop.m_distance == 0))
                        {
                            foreach (BusRoute bus in myBusCollection)
                            {
                                pathCollection.addBusRoute(bus.Section(firstStop, lastStop));
                            }
                            foreach (BusRoute bus in pathCollection)
                            {
                                try
                                {
                                    bus.print();
                                }
                                catch (ArgumentNullException ex)
                                {
                                    Console.WriteLine(ex.Message);
                                }
                                Console.WriteLine(bus.travelTime(bus.m_Stations[0], bus.m_Stations[bus.m_Stations.Count - 1]));
                            }
                        }
                        else
                        {
                            Console.WriteLine("ERROR NO SUCH STOP");
                        }
                        break;

                    default:
                        Console.WriteLine("wrong choice!!!");
                        break;
                    }
                    break;

                case (int)menuChoice.PRINT:

                    try
                    {
                        Console.WriteLine("1: print all busses\n2:print all stops");
                        realChoice = Program_6077_5711_01.getInt(choice, ref realChoice);
                        switch (realChoice)
                        {
                        case (int)menuPrintInfo.printAllBusses:
                            foreach (BusRoute bus in myBusCollection)
                            {
                                Console.WriteLine(bus);
                            }
                            break;

                        case (int)menuPrintInfo.printBusStopInfo:
                            int countBus2 = 0;
                            foreach (BusStop stop in myUniqueStops)
                            {
                                Console.WriteLine(stop);
                                countBus2 = 0;
                                foreach (BusRoute route in myBusCollection.BusStopList(stop.m_mylocation.m_busStationKey))
                                {
                                    Console.WriteLine(route);
                                    countBus2++;
                                }
                                Console.WriteLine("");
                            }
                            break;
                        //case (int)menuPrintInfo.printAllInfo:
                        //    countBus = 0;

                        //    foreach (BusRoute bus in myBusCollection)
                        //    {
                        //        Console.WriteLine("bus number {0}", countBus);
                        //        try
                        //        {
                        //            bus.print();
                        //        }
                        //        catch (NullReferenceException ex)
                        //        {
                        //            Console.WriteLine(ex.Message);
                        //        }
                        //        Console.WriteLine("");
                        //        Console.WriteLine("###  BUS LIST: ###");
                        //        try
                        //        {
                        //            bus.printStops();
                        //        }
                        //        catch (NullReferenceException ex)
                        //        {
                        //            Console.WriteLine(ex.Message);
                        //        }
                        //        countBus++;
                        //    }
                        //    break;
                        default:
                            Console.WriteLine("wrong choice!!!");
                            break;
                        }
                    }
                    catch (ArgumentNullException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    break;

                case (int)menuChoice.EXIT:
                    Console.WriteLine("bye bye!!!");
                    NotEnd = false;
                    break;

                default:
                    Console.WriteLine("wrong choice please try again!");
                    break;
                }
            }
        }