Ejemplo n.º 1
0
 private void BtnAdd_Click(object sender, RoutedEventArgs e)
 {
     //adding trains to the list and doign additional checks
     try
     {
         TrainFactory tFactory = new TrainFactory();
         var          aTrain   = tFactory.CreateTrain(cboxTrainType.SelectedItem.ToString());
         if (aTrain.GetType() == typeof(StopperTrain))
         {
             aTrain.TrainType = "Stoping";
         }
         else if (aTrain.GetType() == typeof(SleeperTrain))
         {
             aTrain.TrainType = "Sleeper";
         }
         else if (aTrain.GetType() == typeof(ExpressTrain))
         {
             aTrain.TrainType = "Express";
         }
         else
         {
             throw new ArgumentException("Undefined type");
         }
         if (txtTrainID.Text.Length == 4 && txtTrainID.Text.Any(char.IsLetter) && txtTrainID.Text.Any(char.IsDigit))
         {
             aTrain.ID = txtTrainID.Text;
         }
         else
         {
             throw new ArgumentException("Train ID must be 4 characters and cannot contain special characters");
         }
         if (checkFirstClass.IsChecked == true && (aTrain.GetType() == typeof(SleeperTrain) || aTrain.GetType() == typeof(ExpressTrain) || aTrain.GetType() == typeof(StopperTrain)))
         {
             aTrain.FirstClass = true;
         }
         else if (checkFirstClass.IsChecked == false && (aTrain.GetType() == typeof(SleeperTrain) || aTrain.GetType() == typeof(ExpressTrain) || aTrain.GetType() == typeof(StopperTrain)))
         {
             aTrain.FirstClass = false;
         }
         else
         {
             throw new ArgumentException("Problem arised while checking first class");
         }
         if (aTrain.GetType() == typeof(SleeperTrain) && checkSleeper.IsChecked == true)
         {
             aTrain.SleeperBerth = true;
         }
         else if (aTrain.GetType() == typeof(SleeperTrain) && checkSleeper.IsChecked == false)
         {
             aTrain.SleeperBerth = false;
         }
         else if (aTrain.GetType() != typeof(SleeperTrain) && checkSleeper.IsChecked == false)
         {
             aTrain.SleeperBerth = false;
         }
         else
         {
             throw new ArgumentException("Only train type \"Sleeper\" can have sleeperberth");
         }
         if (cboxDeparture.SelectedIndex == 0 && cboxDestination.SelectedIndex == 0)
         {
             aTrain.Departure   = "Edinburgh (Waverley)";
             aTrain.Destination = "London (Kings Cross)";
         }
         else if (cboxDeparture.SelectedIndex == 1 && cboxDestination.SelectedIndex == 1)
         {
             aTrain.Departure   = "London (Kings Cross)";
             aTrain.Destination = "Edinburgh (Waverley)";
         }
         else
         {
             throw new ArgumentException("If departure station is Edinburgh destination must be London and vice versa");
         }
         if (DateTime.TryParseExact(txtSetTime.Text, "HH:mm", null, System.Globalization.DateTimeStyles.None, out dtf))
         {
             aTrain.DepTime = txtSetTime.Text;
         }
         else
         {
             throw new ArgumentException("Time must be in 24h format separeted by \":\"");
         }
         if (this.dpDepartureDay.SelectedDate != null)
         {
             aTrain.DepartureDate = this.dpDepartureDay.Text;
         }
         else
         {
             throw new ArgumentException("Select date");
         }
         if (aTrain.GetType() == typeof(ExpressTrain) && (chkNewcastle.IsChecked == true || chkYork.IsChecked == true || chkDarlington.IsChecked == true || chkPeterborough.IsChecked == true))
         {
             throw new ArgumentException("Express trains cannot have intermediate stops");
         }
         else
         {
             if (chkNewcastle.IsChecked == true)
             {
                 aTrain.AddStop(chkNewcastle.Content.ToString());
             }
             if (chkYork.IsChecked == true)
             {
                 aTrain.AddStop(chkYork.Content.ToString());
             }
             if (chkDarlington.IsChecked == true)
             {
                 aTrain.AddStop(chkDarlington.Content.ToString());
             }
             if (chkPeterborough.IsChecked == true)
             {
                 aTrain.AddStop(chkPeterborough.Content.ToString());
             }
         }
         if (trains.FreeID(aTrain.ID))
         {
             trains.AddTrain(aTrain);
             lbTrains.Items.Add(aTrain);
             MessageBox.Show("Train added!");
         }
         else
         {
             throw new ArgumentException("Train with that ID already exists");
         }
     }
     catch (ArgumentException ex0)
     {
         MessageBox.Show(ex0.Message);
     }
 }