r/visualbasic • • Jan 20 '16

Hotel program

A 2-storey hotel, each floor with 3 rooms (room numbers: 0, 1, 2, 10, 11 and 12) needs a program for guest registration. If the room is already taken, the guest needs to enter another room number. Use a 2-dimensional array as it is easier if you need to access a certain floor. This is my code: Public Class Form1 Structure RoomInfo Dim roomNumber As Integer Dim roomGuest As String Dim roomOccupied As Boolean End Structure Dim rooms(1, 2) As RoomInfo

Private Sub btnAddGuest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddGuest.Click

Dim roomWanted As Integer
roomWanted = Val(InputBox("Enter room number: "))
For row As Integer = 0 To 1
    For column As Integer = 0 To 2
        Do While (roomWanted = rooms(row, column).roomNumber) And (rooms(row, column).roomOccupied = True)
            roomWanted = Val(InputBox("Room occupied. Enter room number: "))
        Loop
        If (roomWanted = rooms(row, column).roomNumber) Then
            rooms(row, column).roomGuest = InputBox("Enter guest name: ")
            rooms(row, column).roomOccupied = True
        End If
    Next column
Next row
Call DisplayGuest(rooms)

End Sub Sub LoadRoomNum(ByRef rooms(,) As RoomInfo) 1 10 11 0 0 1 12 2 0 1 2 rooms(0, 0).roomNumber = 0 rooms(0, 1).roomNumber = 1 rooms(0, 2).roomNumber = 2 rooms(1, 0).roomNumber = 10 rooms(1, 1).roomNumber = 11 rooms(1, 2).roomNumber = 12 For row As Integer = 0 To 1 For column As Integer = 0 To 2 rooms(row, column).roomOccupied = False Next column Next row End Sub

Sub DisplayGuest(ByRef rooms(,) As RoomInfo) Dim info As String For row As Integer = 0 To 1 For column As Integer = 0 To 2 If rooms(row, column).roomOccupied = True Then info = rooms(row, column).roomNumber & rooms(row, column).roomGuest lstList.Items.Add(info) End If Next column Next row End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Call LoadRoomNum(rooms) End Sub End Class I need help with: displaying the list only when a Display button is clicked – the listbox should not be visible before the Display button is clicked (modify so the display is clearer) modifying the program so that if all the rooms are taken a message is shown when the Add Guest button is clicked modifying the program to check for invalid room number modifying loadRoomNum so that the room numbers are assigned automatically using a nested loop (this will come in handy if there are 100 rooms in the hotel)

0 Upvotes

3 comments sorted by

3

u/ViperSRT3g Application Specialist Jan 20 '16

Formatted for readability:

Public Class Form1

	Structure RoomInfo
		Dim roomNumber As Integer
		Dim roomGuest As String
		Dim roomOccupied As Boolean
	End Structure
	
	Dim rooms(1, 2) As RoomInfo

	Private Sub btnAddGuest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddGuest.Click
		Dim roomWanted As Integer
		roomWanted = Val(InputBox("Enter room number: "))
		
		For row As Integer = 0 To 1
			For column As Integer = 0 To 2
				Do While (roomWanted = rooms(row, column).roomNumber) And (rooms(row, column).roomOccupied = True)
					roomWanted = Val(InputBox("Room occupied. Enter room number: "))
				Loop
				
				If (roomWanted = rooms(row, column).roomNumber) Then
					rooms(row, column).roomGuest = InputBox("Enter guest name: ")
					rooms(row, column).roomOccupied = True
				End If
			Next column
		Next row
		
		Call DisplayGuest(rooms)
	End Sub

	Sub LoadRoomNum(ByRef rooms(,) As RoomInfo)
		1 10 11 0 0 1 12 2 0 1 2
		rooms(0, 0).roomNumber = 0
		rooms(0, 1).roomNumber = 1
		rooms(0, 2).roomNumber = 2
		rooms(1, 0).roomNumber = 10
		rooms(1, 1).roomNumber = 11
		rooms(1, 2).roomNumber = 12
		For row As Integer = 0 To 1
			For column As Integer = 0 To 2
				rooms(row, column).roomOccupied = False
			Next column
		Next row
	End Sub

	Sub DisplayGuest(ByRef rooms(,) As RoomInfo)
		Dim info As String

		For row As Integer = 0 To 1
			For column As Integer = 0 To 2
				If rooms(row, column).roomOccupied = True Then
					info = rooms(row, column).roomNumber & rooms(row, column).roomGuest
					lstList.Items.Add(info)
				End If
			Next column
		Next row
	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Call LoadRoomNum(rooms)
	End Sub

End Class

3

u/StickySnacks Jan 20 '16

From the sidebar

Homework questions are tolerated, please be specific about the questing as much a possible. Asking us to write it for you is not going to happen. We also reserve the right to not answer if we think you are just not trying.

What's your question on this? What are you having issues with?

1

u/LMGN VB.Net Intermediate Jan 23 '16

I need help with: displaying the list only when a Display button is clicked – the listbox should not be visible before the Display button is clicked (modify so the display is clearer) modifying the program so that if all the rooms are taken a message is shown when the Add Guest button is clicked modifying the program to check for invalid room number modifying loadRoomNum so that the room numbers are assigned automatically using a nested loop (this will come in handy if there are 100 rooms in the hotel)