要记住的一件事是,像这样按索引匹配集合是不好的。创建一个更好的
Class
您需要的字段。代替单独的数组
roomFits()
,
roomCosts()
等等,你有一个这样的类:
Public Class Room
Public Property ID As Integer 'System/Database ID
Public Property RoomNumber As String
Public Property MaxOccupants As Integer
Public Property Price As Decimal
Public Property IsAvailable As Boolean
End Class
然后
1
该Room类实例的列表:
Dim Rooms As New List(Of Room)()
当您能够加载该列表的数据时,我们可以开始查看实际的方法:
Function SelectRoom(amountOfPeople As Integer, daysStaying As Integer) As Room
Dim matches = Rooms.Where(Func(r) r.MaxOccupants >= amountOfPeople AndAlso r.IsAvailable).ToList()
'Don't put the higher level program-flow here. Leave that for the calling method!
If matches.Count = 0 Then Return Nothing
Console.WriteLine("Rooms available:")
For Each r As Room in matches
Dim Price As Decimal = r.Price * daysStaying
Console.WriteLine($"{r.RoomNumber}")
Console.WriteLine($"Maximum in this room: {r.MaxOccupants}")
Console.WriteLine($"This room costs £{r.Price} per night")
Console.WriteLine($"Price for the visit: £{Price}")
Console.WriteLine()
Next r
Console.WriteLine("Which room would you like to pick? ")
Console.Write("> ")
Dim selectedRoomNumber As String = Console.ReadLine()
Return matches.FirstOrDefault(Function(r) r.RoomNumber = selectedRoomNumber)
End Function
现在我们也必须更改调用代码。
Dim selectedRoom As Room = Nothing
Dim triesRemaining As Integer = 3
While selectedRoom Is Nothing AndAlso triesRemaining > 0
selectedRoom = SelectRoom(amountOfPeople, daysStaying)
If selectedRoom Is Nothing Then
triesRemaining -= 1
Console.Write("No rooms matched this criteria. Try again (Y/N)?" )
If Console.ReadLine().Trim().ToUpper() = "N" Then
triesRemaining = 0
End If
End If
End While