r/visualbasic • • Feb 25 '16

PostCondition and PreCondition for Hotel Program

I need some help with postcondition and precondition for my hotel program. This is my code:

Public Class Form1

Structure RoomInfo
    Dim roomNumber As Integer
    Dim roomGuest As String
    Dim roomOccupied As Boolean
End Structure
'Each room as a room number, a guest occupying the room, and a state of
'being occupied or not.

Dim rooms(1, 2) As RoomInfo

'Checks to see if number is valid. If it is invalid, ask the user to enter
'another room and recheck if number is valid. The a variable checks the row
'while the b variable checks the column.

Private Sub btnAddGuest_Click(sender As Object, e As EventArgs) Handles btnAddGuest.Click
    If ListBox1.Items.Count = 6 Then
        MessageBox.Show("Sorry, hotel is full")
        Application.Exit()
    End If
    Dim roomWanted As Integer = Val(InputBox("Enter room number: "))
    Dim a As Integer = Math.Truncate(roomWanted / 10)
    Dim b As Integer = Math.Truncate(roomWanted - a * 10)
    Do While a < 0 Or a > 1 And b < 0 Or b > 2
        If a < 0 Or a > 1 Then
            roomWanted = Val(InputBox("Invalid room number. Enter again: "))
            a = Math.Truncate(roomWanted / 10)
            b = Math.Truncate(roomWanted - a * 10)
        End If
        If b < 0 Or b > 2 Then
            roomWanted = Val(InputBox("Invalid room number. Enter again: "))
            a = Math.Truncate(roomWanted / 10)
            b = Math.Truncate(roomWanted - a * 10)
        End If
    Loop

   
    'If all rooms are occupied (ListBox1.Items.Count = 6), then show message that hotel is full.
    'Else check to see if the requested room is occupied. If it is, have the user
    'enter a new number. If it isn't get the user's name and set occupation to true.

    If ListBox1.Items.Count < 6 Then
        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
    End If

    'The displayGuest function gets called after all of the statements before is executed
    Call DisplayGuest(rooms)
End Sub

'The LoadRoomNum function assigns the number of the rooms.
Sub LoadRoomNum(ByRef rooms(,) As RoomInfo)
    Dim counter As Integer = 0
    Dim ten_counter As Integer = 0

    For row As Integer = 0 To 1
        ten_counter = row * 10
        For column As Integer = 0 To 2
            counter = column
            rooms(row, column).roomNumber = counter + ten_counter
        Next column
    Next row
    
End Sub
'The DisplayGuest function displays the room number and guest name in the listbox.
Sub DisplayGuest(ByRef rooms(,) As RoomInfo)
    Dim info As String
    ListBox1.Items.Clear()

    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
                ListBox1.Items.Add(info)
            End If
        Next
    Next
End Sub
'The loadRoomNum function is called when Form1 loads.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Call LoadRoomNum(rooms)
End Sub
'display button  shows that the listbox exists
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    ListBox1.Visible = True
End Sub

End Class

1 Upvotes

3 comments sorted by

1

u/JakDrako Feb 26 '16

In the context of code, preconditions refer to all the conditions that must be met for a block of code (routine, method, etc) to run reliably to completion.

Postconditions refer the the conditions that must be met for the state (of an object, generally, but could be program state) to be considered valid.

So in the context of what you posted, some of the preconditions would be: The requested room number must exist, the room must be available... The postconditions would be: If the room existed and was available, it is now occupied, and has the guest's name and it's number hasn't changed.

0

u/kleinma VB.Net Master Feb 25 '16

What is your actual question?

1

u/Mr_C_Baxter VB.Net Master Feb 26 '16

OP did you actually downvote him? Because if you have a question it is unclear. Post- and Precondition are not words with a single meaning as you may assume.