예제 #1
0
 public void CreateTrainID_WhenDepartureIsNull_ShouldThrowArgumentNull()
 {
     Train mytrain = exampleTrainFactory.BuildTrain(
         departure: null,
         destination: "London",
         type: "Express",
         departureTime: TimeSpan.MaxValue,
         departureDay: DateTime.MaxValue,
         firstClass: false,
         intermediate: null,
         sleeperCabin: false);
 }
        private void btnAddTrain_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Validation for Sleeper Cabin train type
                if (!((ComboBoxItem)comboType.SelectedItem).Content.Equals("Sleeper") && rdoSleeperYes.IsChecked == true)
                {
                    throw new Exception("Sleeper cabin is not available for this train type");
                }

                // Loop over each checkbox item and add to intermediates if checked
                List <String> intermediates = null;
                if (stackIntermediates.IsEnabled == true)
                {
                    intermediates = new List <String>();
                    foreach (Control control in stackIntermediates.Children)
                    {
                        if (((CheckBox)control).IsChecked == true)
                        {
                            intermediates.Add(((CheckBox)control).Content.ToString());
                        }
                    }
                    if (comboDeparture.Text.Equals("London (Kings Cross)"))
                    {
                        intermediates.Reverse();
                    }
                }

                // Check if FirstClass and Sleeper have been checked and set value
                bool firstClass   = (rdoFirstClassYes.IsChecked == true) ? true : false;
                bool sleeperCabin = (rdoSleeperYes.IsChecked == true) ? true : false;

                // Build the train using our factory
                Train t = trainFactory.BuildTrain(
                    comboDeparture.Text,
                    comboDestination.Text,
                    comboType.Text,
                    TimeSpan.Parse(comboDepartureTime.Text),
                    DateTime.Parse(dateDepartureDay.Text),
                    firstClass,
                    intermediates,
                    sleeperCabin
                    );

                if (t == null)
                {
                    throw new Exception("Train failed to create");
                }

                // If train has been created sucessfully, store it and prompt user
                trainStore.Add(t);
                MessageBox.Show("Train has been added successfully");

                this.Close();
            }
            catch (Exception ex)
            {
                if (ex is FormatException)
                {
                    MessageBox.Show("Please select a valid time");
                }
                else
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }