代码之家  ›  专栏  ›  技术社区  ›  EpicAshman

我正试图从Visual Basic中房间搜索系统的数组转移到列表

  •  0
  • EpicAshman  · 技术社区  · 5 年前

    这是我的功能,搜索房间是否可用,并使其不再可用,因为这是我为我的一个项目办理入住手续系统的一部分。

    Function RoomAvailableFunc()
                    Console.WriteLine("Rooms available:")
                    For i = 0 To UBound(Rooms)
                        If roomFits(i) >= amountOfPeople Then
                            If roomAvailable(i) = True Then
                                Price = roomCosts(i) * daysStaying
                                Console.WriteLine(Convert.ToString(Rooms(i)))
                                Console.WriteLine("Maxinum in this room: " & roomFits(i))
                                Console.WriteLine("This room costs £" & Convert.ToString(roomCosts(i)) & " per night")
                                Console.WriteLine("Price for the visit: £" & Price)
                                roomsFound += 1
                            End If
                        End If
                    Next i
                    If roomsFound <= 0 Then
                        Console.WriteLine("Sorry we have not found any rooms for this criteria!")
                        Console.WriteLine("Please try again!")
                        Console.WriteLine("Press any key to continue..")
                        Console.ReadKey()
                        Main()
                    End If
                    Console.WriteLine("Which room would you like to pick?")
                    Console.Write("> ")
                    roomNumber = Convert.ToInt16(Console.ReadLine())
    
                    For i = 0 To UBound(Rooms)
    
                        If roomNumber = Rooms(i) Then
                            Price = roomCosts(i) * daysStaying
                            roomAvailable(i) = False
                        End If
                    Next i
                End Function
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   Joel Coehoorn    5 年前

    要记住的一件事是,像这样按索引匹配集合是不好的。创建一个更好的 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