Exemplo n.º 1
0
        //adds new booking to the list
        private void BtnAddBooking_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //generate booking reference number auto if the text box is empty(Always empty because is readonly)
                if (String.IsNullOrEmpty(TxtBookingRefNum.Text))
                {
                    MessageBox.Show("Booking Reference Number auto generated!");
                    //get access to datalayer to use singleton pattern
                    bookingReference      = DataSingleton.getBookingReference();
                    TxtBookingRefNum.Text = bookingReference.ToString();
                }
                //checks if the the form is filled up
                else if (String.IsNullOrEmpty(TxtArrivalDate.Text) ||
                         String.IsNullOrEmpty(TxtDepartureDate.Text) ||
                         String.IsNullOrEmpty(TxtBookingChaletID.Text))
                {
                    throw new ArgumentException("Error empty fields. Please fill them up to proceed!");
                }
                else if (Int32.Parse(TxtBookingChaletID.Text) < 1 || Int32.Parse(TxtBookingChaletID.Text) > 10)
                {
                    MessageBox.Show("ChaletID should be in range of 1-10");
                }
                else if (DateTime.Parse(TxtArrivalDate.Text) > DateTime.Parse(TxtDepartureDate.Text))
                {
                    MessageBox.Show("Departure date cannot be before your arrival date");
                }
                else if (ComboBoxCustomerRef.SelectedItem == null)
                {
                    MessageBox.Show("Please select your customer reference number to proceed!");
                }
                else
                {
                    try
                    {
                        DateTime arrivalDate   = DateTime.Parse(TxtArrivalDate.Text);
                        DateTime departureDate = DateTime.Parse(TxtDepartureDate.Text);
                        int      chaletID      = Int32.Parse(TxtBookingChaletID.Text);
                        //calculate how many days is the booking made for
                        TimeSpan span = departureDate.Subtract(arrivalDate);
                        //assigns the total number of days for current booking to variable _totalDays
                        _totalDays = span.Days;
                        MessageBox.Show("Number of days" + span.Days);

                        //store new booking
                        BusinessObjects.Booking newBooking = new BusinessObjects.Booking(chaletID, bookingReference, arrivalDate, departureDate);//arguments
                        Bookings.Add(newBooking);
                        //add the booking reference number to the drop down menu for guests
                        ComboBoxBookingRef.Items.Add(newBooking.BookingRefNumber);

                        //empty text boxes
                        TxtBookingChaletID.Text = String.Empty;
                        TxtArrivalDate.Text     = String.Empty;
                        TxtDepartureDate.Text   = String.Empty;
                        TxtBookingRefNum.Text   = String.Empty;
                        //clears the selection from the combobox-drop down menu
                        ComboBoxCustomerRef.SelectedIndex = -1;
                    }
                    catch (ArgumentException)
                    {
                        MessageBox.Show("Errors within the form! Booking was not added!");
                    }
                }
            }
            catch (ArgumentException)
            {
                MessageBox.Show("Empty fields withing Booking section.\nFill Booking section to proceed.");
            }
        }