示例#1
0
        private bool ReturnVideo()
        {
            string message       = String.Empty;
            Item   bookingAsItem = new Item();

            try
            {
                ListViewItem  bookingLVI;
                int           bookingID        = 0;
                var           bookingVideoName = String.Empty;
                IVideoBooking _booking;


                while (true)
                {
                    if (lvwBookings.SelectedItems.Count < 1)
                    {
                        message += "No booking is selected\n";
                        break;
                    }

                    bookingLVI         = lvwBookings.SelectedItems[0];
                    bookingID          = int.Parse(bookingLVI.SubItems[6].Text);
                    bookingVideoName   = bookingLVI.SubItems[1].Text;
                    bookingAsItem.Id   = bookingID;
                    bookingAsItem.Name = bookingVideoName;

                    if (!booking.BookingExist(bookingID))
                    {
                        message += "The selected booking does not exist in the database";
                        break;
                    }

                    _booking =
                        (from b in booking.GetBookings()
                         where b.Key.Equals(bookingID)
                         select b).First().Value;

                    if (!booking.VideoExist(bookingVideoName))
                    {
                        message += "The video connected with the selected booking does not exist in the database. VideoId: " + _booking.VideoId;
                        break;
                    }

                    var _video = booking.GetVideos(VideoStatus.All).FirstOrDefault(c => c.Key.Equals(_booking.VideoId)).Value;

                    if (_video.IsRented == false)
                    {
                        message += "The video is not rented";
                        break;
                    }

                    if (dateTimePicker.Value < _booking.Rented)
                    {
                        message += "Incorrect date input. Return date must be equal or later than rental date\n";
                        break;
                    }

                    booking.ReturnVideo(bookingAsItem, dateTimePicker.Value);
                    break;
                }

                if (message.Length > 0)
                {
                    throw new VideoRentalException(bookingAsItem, message);
                }

                return(true);
            }
            catch (VideoRentalException ex)
            {
                MessageBox.Show(ex.Message);
                return(false);
            }
            return(false);
        }