示例#1
0
        //AddBtn_Click: Attempts to add booking details entered to Bookings
        private void AddBtn_Click(object sender, RoutedEventArgs e)
        {
            TrainFactory factory = TrainFactory.Instance();
            Booking      booking;

            //Returns a train which matches the id entered on the form
            try
            {
                //This needs to be done otherwise Find() returns first item in list.
                if (string.IsNullOrEmpty(idTxtBox.Text))
                {
                    throw new ArgumentNullException("ID cannot be empty!");
                }
                Train train = MainWindow.Trains.Find(x => x.TrainID.Contains(idTxtBox.Text));
                if (train == null)
                {
                    throw new ArgumentNullException("Train to book on doesn't exist!");
                }

                booking = factory.CreateBooking(train, nameTxtBox.Text, departCombo.Text, arrivalCombo.Text,
                                                bool.Parse(firstclassCombo.Text), bool.Parse(cabinCombo.Text),
                                                coachCombo.Text[0], (int)seatCombo.SelectedValue);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Failure();
                return;
            }

            BookingDecorator decorator = new BookingDecorator(booking);
            //Opens a message box displaying fare and asking for user's confirmation
            MessageBoxResult result = MessageBox.Show($"Are you sure? The price will be £{decorator.Fare}", "Price Confirmation", MessageBoxButton.YesNo);

            if (result == MessageBoxResult.No)
            {
                Failure();
                TrainFactory.Records[booking.TrainID].RemoveAt(TrainFactory.Records[booking.TrainID].Count - 1);
                return;
            }

            //Actually adds booking to Bookings
            MainWindow.Bookings.Add(booking);
            successTxtBlk.Foreground = Brushes.Green;
            successTxtBlk.Text       = "Success! Booking was added.";
            MessageBox.Show("Added!");
        }
示例#2
0
        //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!");
        }