public void TestFactoryTrain() { TrainFactory.Records = new Dictionary <string, List <string> >(); factory.CreateTrain("SL3P", "Sleeper", "Edinburgh (Waverley)", "London (Kings Cross)", true, new List <string> { "Peterborough", "Darlington" }, new TimeSpan(22, 0, 0), DateTime.Parse("2018-01-01")); }
private void btnAddTrain_Click(object sender, RoutedEventArgs e) { try { var trainFactory = new TrainFactory(); var newTrain = trainFactory.CreateTrain(GetNewTrainInfo()); dataFacade.AddTrain(newTrain); dataFacade.SaveTrains(); MessageBox.Show($"Train {newTrain.ID} added!"); } catch (Exception exception) { MessageBox.Show(exception.Message); } }
//AddBtn_Click: Attempts to add train details entered to Trains private void AddBtn_Click(object sender, RoutedEventArgs e) { TrainFactory factory = TrainFactory.Instance(); Train train; List <string> intermediates = new List <string>(); //Iterates over all the controls in the form, if it's a checkbox, check if its checked, if so add it to the list of intermediates foreach (var ctrl in detailsGrid.Children) { if (ctrl.GetType() == typeof(CheckBox)) { if (((CheckBox)ctrl).IsChecked.Value) { intermediates.Add(((CheckBox)ctrl).Content.ToString()); } } } try { train = factory.CreateTrain(idTxtBox.Text, typeCombo.Text, departCombo.Text, destCombo.Text, bool.Parse(firstclassCombo.Text), intermediates, TimeSpan.ParseExact(timeTxtBox.Text, @"hh\:mm", CultureInfo.InvariantCulture), dayDatePick.SelectedDate.Value.Date); } catch (Exception ex) { MessageBox.Show(ex.Message); successTxtBlk.Foreground = Brushes.Red; successTxtBlk.Text = "Failure, Train was not added."; return; } MainWindow.Trains.Add(train); successTxtBlk.Foreground = Brushes.Green; successTxtBlk.Text = "Success! Train was added."; MessageBox.Show("Added!"); }
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); } }