Exemple #1
0
        }//constructor

        /// <summary>
        /// This method kicks off the train
        /// </summary>
        /// <param name="journey"></param>
        /// <returns></returns>
        public async Task <string> StartTrain(Journey journey)
        {
            TrainJourney = journey;                 //Set the journey passed in from the parameters
            if (CurrStop == null)                   //If the current stop is empty
            {
                CurrStop = TrainJourney.FirstStop;  //Set the current stop to the first
            }

            Task <string> getUpdate = Task.Run(() => MoveTrain()); //Setup task to simulate train
            String        update    = await getUpdate;             //Save result in string

            return(update);                                        //Return the result
        }//StartTrain
        }     //testString

        /// <summary>
        /// This method is what does the processing for the journey and displaying the output
        /// </summary>
        /// <param name="train"></param>
        /// <returns></returns>
        private async Task <string> begin(Train train)
        {
            Journey journey = train.TrainJourney;

            btnAddStop.Enabled = false;                                //Disable addstop for duration of journey
            Stop Ttracker;                                             //Tracks what stop the train is at
            //Tests if the ID is valid
            String res = "";                                           //String that holds the result of the update

            if (journey != null && journey.FirstStop.NextStop != null) //Tests if the journey is not null or if the first stop has no other stops
            {
                Ttracker      = journey.FirstStop;                     //Set temporary stop to the first stop in the journey
                lblStops.Text = "Stops:";                              //Set the text for the stops overview label
                do
                {
                    //The next stop is null
                    if (Ttracker.NextStop == null)
                    {
                        lblStops.Text += Ttracker.getLastStopDetails();                 //Provide alternate details text for last stop
                        MessageBox.Show("Detected " + Ttracker.Name + " as last stop"); //Alert the user it detected the current stop as the last
                    }
                    else
                    {
                        lblStops.Text += Ttracker.getStopDetails();       //Else provide regular details text
                    }
                    Ttracker = Ttracker.NextStop;                         //Incremenet the stop to the next
                } while (Ttracker != null);                               //Do while

                Ttracker       = journey.FirstStop;                       //Set the Tracker back to first stop
                lblOutput.Text = "First Stop: " + journey.FirstStop.Name; //Change the output text to the first stop
                do
                {
                    Ttracker = Ttracker.NextStop;                           //Set the tracker to the next stop
                    res      = await train.StartTrain(journey);             //Start the journey to the next stop and await the response

                    lblOutput.Text += "\n" + res;                           //Return the result
                } while (Ttracker.NextStop != null);                        //While the next stop is not null
            }
            else
            {
                MessageBox.Show("Journey is not Set!");                     //if no journey is found - meaning no stops
                return("");
            }//if else
            return("Journey complete");
        }//begin
        }//btnGo_Click

        /// <summary>
        ///  This method handles adding a new stop into the journey
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddStop_Click(object sender, EventArgs e)
        {
            if (detectCurrTrain() != null)
            {
                Journey  journey = detectCurrTrain().TrainJourney;
                Add_Stop newStop = new Add_Stop(journey);           //Create temp new stop
                newStop.ShowDialog();
                //If the journey is null instantiate it, otherwise add node
                if (journey == null)
                {
                    detectCurrTrain().TrainJourney = new Journey(newStop.NewStop, newStop.Speed);
                    lblSpeed.Text += newStop.Speed;
                }
                else
                {
                    journey.AddStop(newStop.NewStop);
                }//else

                displayJourney();
            } //if
        }     //btnAddStop_Click