简体   繁体   中英

I need a recursive function to remove periods of time from other periods of time

I will try to be as clear as possible as this is my first question. I have a project I am working on that handles appointments. I have hit a snag when it comes to filtering out available time slots for people to request an appointment. I have defined classes for a period of time as follows:

Public Class TimePeriod
    #Region "Private Variables"
        Private _startTime As TimeSpan
        Private _endTime As TimeSpan
    #End Region

    #Region "Public Properties"
        Public Property StartTime As TimeSpan
            Get
                Return Me._startTime
            End Get
            Set(value As TimeSpan)
                Me._startTime = value
            End Set
        End Property
        Public Property EndTime As TimeSpan
            Get
                Return Me._endTime
            End Get
            Set(value As TimeSpan)
                Me._endTime = value
            End Set
        End Property
    #End Region

    #Region "Constructors"
        Public Sub New()
            Me.New(New TimeSpan(0, 0, 0), New TimeSpan(0, 0, 0))
        End Sub
        Public Sub New(StartTime As TimeSpan, EndTime As TimeSpan)
            Me._startTime = StartTime
            Me._endTime = EndTime
        End Sub
    #End Region
End Class

Public Class TimePeriodFunctions
    Public Shared Function RemoveTimePeriod(MasterTimePeriod As TimePeriod, TimePeriodToRemove As TimePeriod) As System.Collections.Generic.List(Of TimePeriod)
        'SEE FIRST IF THERE IS ANY OVERLAP
        If (TimePeriodToRemove.StartTime >= MasterTimePeriod.StartTime And TimePeriodToRemove.StartTime <= MasterTimePeriod.EndTime) Or _
           (TimePeriodToRemove.EndTime >= MasterTimePeriod.StartTime And TimePeriodToRemove.EndTime <= MasterTimePeriod.EndTime) Then
            Dim TimePeriods As New System.Collections.Generic.List(Of TimePeriod)

            If TimePeriodToRemove.StartTime >= MasterTimePeriod.StartTime And TimePeriodToRemove.StartTime <= MasterTimePeriod.EndTime And _
               MasterTimePeriod.StartTime <> TimePeriodToRemove.StartTime Then
                'TIME TO RETURN IS FROM MASTERTIMEPERIOD.STARTTIME TO TIMEPERIODTOREMOVE.STARTTIME
                TimePeriods.Add(New TimePeriod(MasterTimePeriod.StartTime, TimePeriodToRemove.StartTime))
            End If
            If TimePeriodToRemove.EndTime >= MasterTimePeriod.StartTime And TimePeriodToRemove.EndTime <= MasterTimePeriod.EndTime And _
               TimePeriodToRemove.EndTime <> MasterTimePeriod.EndTime Then
                'TIME TO RETURN IS FROM TIMEPERIODTOREMOVE.ENDTIME TO MASTERTIMEPERIOD.ENDTIME
                TimePeriods.Add(New TimePeriod(TimePeriodToRemove.EndTime, MasterTimePeriod.EndTime))
            End If

            Return TimePeriods
        Else
            'IF THERE IS NO OVERLAP, THEN RETURN AN EMPTY COLLECTION
            Return New System.Collections.Generic.List(Of TimePeriod)
        End If
    End Function
End Class

Now, if I have a TimePeriod that represents the period of time available to make appointments as StartTime = 9:00 and EndTime = 5:00, and I have a generic list of various currently scheduled appointments (eg. 9:30-10:00, 10:00-11:00), how would I write a recursive function to return a generic list of the still available time periods (like removing chunks from the available time period if it wasn't clear)?

Set a boolean property in your TimePeriod class maybe named IsBusy. You can then write a lambda to query your list where IsBusy = False.

Also as a side note, wouldn't it make more sense to have a pre-built list of time blocks, say 15 minute increments, then if someone wants to block out say 9:00AM - 9:45AM you would only need to loop through the list to find those (which can be done with a lambda as well) to find three TimePeriods and update their IsBusy to True. If each begin and end times are different increments of time you run into the issue of testing every item in the list to see if something conflicts with the scheduled event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM