Example #1
0
        /// <summary>
        /// Validate that the from and to station fileds contain valid stations names, are not the same, and
        /// do not represent a valid trip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddButtonClicked(object sender, System.EventArgs e)
        {
            bool   validTrip    = true;
            string errorMessage = "";

            // Check for valid station names
            if (Array.IndexOf(StationStorage.StationNames, fromView.Text) < 0)
            {
                errorMessage += string.Format("'{0}' is not a valid station name\n", fromView.Text);
                validTrip     = false;
            }

            if (Array.IndexOf(StationStorage.StationNames, toView.Text) < 0)
            {
                errorMessage += string.Format("'{0}' is not a valid station name\n", toView.Text);
                validTrip     = false;
            }

            if (validTrip == true)
            {
                // Make sure they are not the same
                if (fromView.Text == toView.Text)
                {
                    errorMessage += "Station names cannot be the same\n";
                    validTrip     = false;
                }
            }

            if (validTrip == true)
            {
                // Check for a duplicate trip
                if (TrainTrips.IsDuplicateTrip(fromView.Text, toView.Text) == true)
                {
                    errorMessage += string.Format("A trip from {0} to {1} already exists\n", fromView.Text, toView.Text);
                    validTrip     = false;
                }
            }

            if (validTrip == false)
            {
                // Display the reason why this trip cannot be added
                new AlertDialog.Builder(this)
                .SetTitle("Cannot add trip")
                .SetMessage(errorMessage)
                .SetPositiveButton("OK", (EventHandler <DialogClickEventArgs>)null)
                .Create()
                .Show();
            }
            else
            {
                // Add the trip and close this activity
                TrainTrips.AddTrip(new TrainTrip {
                    From   = fromView.Text, To = toView.Text, FromCode = StationStorage.GetCode(fromView.Text),
                    ToCode = StationStorage.GetCode(toView.Text)
                });
                RecentStations.AddStation(fromView.Text);
                RecentStations.AddStation(toView.Text);

                SetResult(Result.Ok);
                Finish();
            }
        }