Esempio n. 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!");
        }
Esempio n. 2
0
        public void TestFactoryBooking()
        {
            Train t = InitFactoryBooking();

            factory.CreateBooking(t, "Test", "Peterborough", "London (Kings Cross)", true, true, 'A', 8);
        }