【问题标题】:I'm trying to transfer over to a list from an array for my room search system in Visual Basic我正在尝试从数组中转移到我在 Visual Basic 中的房间搜索系统的列表
【发布时间】:2020-01-19 20:39:02
【问题描述】:

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

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

【问题讨论】:

    标签: arrays vb.net list sorting


    【解决方案1】:

    要记住的一点是,像这样按索引匹配集合并不好。使用您需要的字段创建 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
    

    然后 ONE 列出该 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-28
      • 2019-06-14
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多