避免在我的预订系统应用程序中重叠预订的问题

时间:2011-08-22 12:03:18

标签: vb.net datetime

如果2011年12月27日下午5:00在数据库中预订2小时,我尝试在2011年12月27日下午5点到7点之间进行新预订,那么我的代码会生成错误消息。即使我尝试在下午4点预订2小时,也会产生错误信息,因为第2小时将在5:00到7:00之间与已预订的预订重叠。

现在问题部分来了。当天更改它不会生成错误消息,即如果预订在12/27/2011晚上11:00持续3小时,那么它不应该允许新的预订到12/22/2011凌晨2:00但是当我尝试在凌晨1:00预订12/28/2011,将其保存在数据库中,不会生成错误消息。我希望在这种情况下生成错误消息。

我在数据库中使用两个单独的字段用于时间,一个用于日期。它们都有DateTime数据类型。

newtime是指我正在尝试进行新预订的时间

addednewtime指的是将持续时间添加到我尝试进行新预订的时间之后的时间

addeddbtime只包含从数据库中存储的datetime字段中提取的时间值(在数据库中添加预留时间之后)

newdate指的是下一个日期,因为当天在凌晨12:00更改,所以如果数据库预订是在2011年12月12日晚上11点,则新日期将在12/13/2011中

问题在于If条件的最后一部分,当预订超过两天时检查重叠预订

这是我的代码:

Dim newtime, addednewtime, addeddbtime, changetime, newdate As DateTime 'defines variables
addeddbtime = dbonlytime.AddHours(dbdur)
newtime = TxtBookTime.Text
addednewtime = newtime.AddHours(TxtBookDur.Text)



changetime = "12:00:00 AM"
newdate = dbdate.AddDays(1)

If TxtBookDate.Text = dbdate And TxtPoolNo.Text = dbpoolno And TxtCrtNo.Text = dbcrtno And TxtTblNo.Text = dbtblno And CmboGame.SelectedItem = dbgame And ((newtime > dbonlytime And newtime < addeddbtime) Or (addednewtime > dbonlytime And addednewtime < addeddbtime) Or (newtime < dbonlytime And addeddbtime > changetime And addednewtime < dbonlytime And addednewtime <= addeddbtime And TxtBookDate.Text = newdate)) Then
MessageBox.Show("The date and time you have entered has already been booked " & vbCrLf & " Try Again!", "Bookings Overlapped", MessageBoxButtons.OK, MessageBoxIcon.Stop)

Exit Sub

End If

1 个答案:

答案 0 :(得分:1)

您想要做的是检查当前预订与现有预订之间的重叠程序。您应该有一个方法,具有以下输入值:

  1. 当前预订日期
  2. 现有预订清单
  3. 假设您有一个名为Booking的类,它具有开始和结束日期时间(不需要为日期和时间设置单独的字段,DateTime对象包含两者)。

    Public Class Booking
    
        Public Sub New(ByVal beginDate As DateTime, ByVal endDate As DateTime)
            Me.BeginDate = beginDate
            Me.EndDate = endDate
        End Sub
    
        Public Property BeginDate As Date
    
        Public Property EndDate As Date
    
        'Other booking properties here
    
    End Class
    

    您可以使用这样的例程来检查现有预订是否存在重叠:

    Private Function BookingOverlapping(ByVal booking As Booking, ByVal existingBookings As IEnumerable(Of Booking)) As Boolean
        For Each existingBooking In existingBookings
            If booking.BeginDate < existingBooking.EndDate AndAlso booking.EndDate > existingBooking.BeginDate Then
                Return True
            End If
        Next
        Return False
    End Function
    

    然后你会使用这样的方法:

    ' Figure out begin and end DateTimes from user input, 
    ' then create a booking object.
    Dim currentBooking As New Booking(beginDate, endDate)
    
    ' The GetExistingBookings method would retrieve bookings from the database 
    ' and add new Booking objects to a list.
    Dim existingBookings As List(Of Booking) = GetExistingBookings()
    
    If BookingOverlapping(currentBooking, existingBookings)
        MessageBox.Show("The date and time you have entered has already been booked " & vbCrLf & " Try Again!", "Bookings Overlapped", MessageBoxButtons.OK, MessageBoxIcon.Stop)
    End If