Mini Kabibi Habibi
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports DevExpress.Web.ASPxEditors
Public Class SearchQuery
Public Sub New()
Stars = New Integer(){}
IsExclusive = False
HotelServices = New String(){}
RoomServices = New String(){}
IsSearch = False
End Sub
Private privateMaxPrice As Decimal
Public Property MaxPrice() As Decimal
Get
Return privateMaxPrice
End Get
Set(ByVal value As Decimal)
privateMaxPrice = value
End Set
End Property
Private privateMinPrice As Decimal
Public Property MinPrice() As Decimal
Get
Return privateMinPrice
End Get
Set(ByVal value As Decimal)
privateMinPrice = value
End Set
End Property
Private privateCountry As String
Public Property Country() As String
Get
Return privateCountry
End Get
Set(ByVal value As String)
privateCountry = value
End Set
End Property
Private privateCity As String
Public Property City() As String
Get
Return privateCity
End Get
Set(ByVal value As String)
privateCity = value
End Set
End Property
Private privateFromDate As Nullable(Of DateTime)
Public Property FromDate() As Nullable(Of DateTime)
Get
Return privateFromDate
End Get
Set(ByVal value As Nullable(Of DateTime))
privateFromDate = value
End Set
End Property
Private privateToDate As Nullable(Of DateTime)
Public Property ToDate() As Nullable(Of DateTime)
Get
Return privateToDate
End Get
Set(ByVal value As Nullable(Of DateTime))
privateToDate = value
End Set
End Property
Private privateStars As Integer()
Public Property Stars() As Integer()
Get
Return privateStars
End Get
Set(ByVal value As Integer())
privateStars = value
End Set
End Property
Private privateHotelServices As String()
Public Property HotelServices() As String()
Get
Return privateHotelServices
End Get
Set(ByVal value As String())
privateHotelServices = value
End Set
End Property
Private privateRoomServices As String()
Public Property RoomServices() As String()
Get
Return privateRoomServices
End Get
Set(ByVal value As String())
privateRoomServices = value
End Set
End Property
Private privateRoomType As String
Public Property RoomType() As String
Get
Return privateRoomType
End Get
Set(ByVal value As String)
privateRoomType = value
End Set
End Property
Private privateAdults As Integer
Public Property Adults() As Integer
Get
Return privateAdults
End Get
Set(ByVal value As Integer)
privateAdults = value
End Set
End Property
Private privateChildren As Integer
Public Property Children() As Integer
Get
Return privateChildren
End Get
Set(ByVal value As Integer)
privateChildren = value
End Set
End Property
Private privateIsExclusive As Boolean
Public Property IsExclusive() As Boolean
Get
Return privateIsExclusive
End Get
Set(ByVal value As Boolean)
privateIsExclusive = value
End Set
End Property
Public Shared ReadOnly Property MostExclusive() As SearchQuery
Get
Return New SearchQuery() With {.IsExclusive = True, .Stars = New Integer() { 5 }}
End Get
End Property
Private privateIsSearch As Boolean
Public Property IsSearch() As Boolean
Get
Return privateIsSearch
End Get
Set(ByVal value As Boolean)
privateIsSearch = value
End Set
End Property
Private Shared ReadOnly Match_Country As Func(Of SearchQuery, Hotel, Boolean) = Function(q, h) String.IsNullOrEmpty(q.Country) OrElse q.Country = h.Country.Name
Private Shared ReadOnly Match_City As Func(Of SearchQuery, Hotel, Boolean) = Function(q, h) String.IsNullOrEmpty(q.City) OrElse q.City = h.City
Private Shared ReadOnly Match_HotelServices As Func(Of SearchQuery, Hotel, Boolean) = Function(q, h) MatchServices(q.HotelServices, h.HotelServices)
Private Shared ReadOnly Match_RoomServices As Func(Of SearchQuery, Hotel, Boolean) = Function(q, h) MatchServices(q.RoomServices, h.RoomServices)
Private Shared ReadOnly Match_Stars As Func(Of SearchQuery, Hotel, Boolean) = Function(q, h) q.Stars.Length = 0 OrElse q.Stars.Contains(h.Stars)
Private Shared ReadOnly Match_Room As Func(Of SearchQuery, Hotel, Boolean) = Function(q, h) MatchRooms(h, q.RoomType, q.Adults, q.Children, q.MinPrice, q.MaxPrice)
Private Shared ReadOnly MatchExpressions() As Func(Of SearchQuery, Hotel, Boolean) = { Match_Country, Match_City, Match_HotelServices, Match_RoomServices, Match_Stars, Match_Room }
Private Shared Function MatchServices(ByVal queryServices() As String, ByVal hotelServices As List(Of String)) As Boolean
For Each qs In queryServices
If (Not hotelServices.Contains(qs)) Then
Return False
End If
Next qs
Return True
End Function
Private Shared Function MatchRooms(ByVal hotel As Hotel, ByVal type As String, ByVal adults As Integer, ByVal children As Integer, ByVal minPrice As Decimal, ByVal maxPrice As Decimal) As Boolean
If String.IsNullOrEmpty(type) Then
Return True
End If
Return hotel.Rooms.Any(Function(r) r.Type = type AndAlso r.Adults >= adults AndAlso (maxPrice = 0 OrElse r.Price <= maxPrice) AndAlso (minPrice = 0 OrElse r.Price >= minPrice))
End Function
Private Shared Function MatchSearchString(ByVal searchString As String, ByVal title As String) As Boolean
Dim words() As String = searchString.Split(New Char() { " "c }, StringSplitOptions.RemoveEmptyEntries)
For Each word In words
If title.IndexOf(word, StringComparison.InvariantCultureIgnoreCase) < 0 Then
Return False
End If
Next word
Return True
End Function
Public Function IsMatch(ByVal hotel As Hotel) As Boolean
For Each expression In MatchExpressions
If (Not expression(Me, hotel)) Then
Return False
End If
Next expression
Return True
End Function
End Class