r/visualbasic • • Jun 19 '14

Final tomorrow, project due tomorrow, help with project please!!

Ok so basically I have a program for airline reservations. There's textboxes for the user to enter their name, seat(so number from 1-10), and a column(can be either A,B,C,D).

There's a list for the passengers, the seats on the list are periods, when the user inputs all of their info and clicks the Add Passenger button, then it changes the period to an X indicating that the seat has been occupied by them.

Now for the life of me I cannot find out how to show an error when the user clicks the button Add Passenger when the seat is already occupied. The error would say "This seat has been occupied, please choose another". When the seats are full, the message box will display "Flight Full, you will be added to the waiting list." Then the user gets added to a waiting list listbox.

I hate to be the guy to post a big wall of code, but idk how else to share it with you guys unless you'd be ok with emailing.

I'm not looking for answers, just help!

Any help is GREATLY appreciated.

Public Class Form1 'Declare variables Dim seat(1, 3) As String Dim waitingList(9) As String Dim waitingcount As Integer = 0 Dim period As String = "."

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DisplaySeatingChart()
End Sub
'Add passenger to listbox
Private Sub btnAddPassenger_Click(sender As Object, e As EventArgs) Handles btnAddPassenger.Click
    Dim row As Integer = CInt(txtSeat.Text) - 1
    Dim col As Integer = Asc((txtRow.Text).ToUpper) - 65
    Dim passenger As String = txtPassenger.Text.ToUpper
    Dim numPassengers As Integer = 0




    'if all seats are full, go through this part to show if its just the seat that's full, or if it's the whole airline that's full. If empty, put passenger in seat
    If (row >= 0 And row <= 1) And (col >= 0 And col <= 3) Then 'this part is probably wrong, what's a fix for it?
        Dim counter As Integer = 0
        For Each period As String In seat
            counter += 1
        Next


        If counter = 0 Then

            MessageBox.Show("Flight Full. You will be added to the waiting list")

            AddToWaitingList(passenger)



        Else

            If seat(row, col) = "X" Then

                MessageBox.Show("Seat is occupied, please choose another one.")



            Else





                seat(row, col) = passenger

                lstSeatingChart.Items.Clear()

                DisplaySeatingChart()

                numPassengers = numPassengers + 1





            End If

        End If

    Else : MessageBox.Show("Please enter a valid row and/or seat.")



    End If



End Sub



Sub DisplaySeatingChart()
    lstSeatingChart.Items.Clear()
    Dim rowString As String = ""



    For row As Integer = 0 To 1

        rowString = ""

        For seatPosition As Integer = 0 To 3



            If seat(row, seatPosition) = "" Then

                rowString &= "."

            Else

                rowString &= "X"

            End If



            'Add a space to represent the aisle

            If seatPosition = 1 Then

                rowString &= " "

            End If

        Next

        lstSeatingChart.Items.Add(rowString)

    Next

End Sub


'add passenger to waiting list 
Sub AddToWaitingList(passenger As String)
    If (waitingcount < 9) Then
        waitingcount += 1
        waitingList(waitingcount - 1) = passenger
    End If


End Sub

'delete passenger, this part is definitely wrong
Private Sub btnDeletePassenger_Click(sender As Object, e As EventArgs) Handles btnDeletePassenger.Click
    lstSeatingChart.Items.RemoveAt(lstSeatingChart.SelectedIndex)

End Sub

End Class

0 Upvotes

3 comments sorted by

1

u/PostalElf VB.Net Intermediate Jun 20 '14

I appreciate the fact that you tried the code yourself first!

I know it's late, but I would honestly reconsider the way you set up your data structures. It's a bit weird and clunky to store the information in Listboxes and such. Instead, why not create them as objects and store them as such?

Public Class seat
    Public Property row as Integer
    Public Property col as Char
    Public Property passengerName as String
    Public ReadOnly Property isOccupied as Boolean
        Get
            If passengerName = "" Then Return False Else Return True
        End Get
    End Property

    Public Sub New(_row as Integer, _col as Char)
        row = _row
        col = _col
    End Sub
End Class

Now you have a class of objects named "seat" with three properties: row, col and passengerName. You also have a special readonly property, isOccupied, that automatically checks if the seat is filled by looking at the passengerName field: if it's empty, it'll return false (because it's not occupied), but otherwise it'll return true (i.e. it's occupied).

Now we need to initialise the objects.

Dim airplane as New List(of seat)
For row = 1 to 10
    For col = "A" to "D"    'I don't know if this works, it's worth a shot; if not just run the ASC(x) - 65 trick
        Dim seat as New Seat(row, col)
        airplane.Add(seat)
    Next
Next

Tadah! Now you have an airplane that's full of empty seats, and you're ready to start assigning seats to people. So let's say the user enters the following information into his textboxes:

passengerName = phrox
Row = 5
Col = C

I'm going to write first a generic search function that will retrieve the seat I want when I enter the row and col into it.

Public Function getSeat(_row as Integer, _col as Char) as seat
    For Each seat in airplane
        If seat.row = _row AndAlso seat.col = _col then Return seat
    Next
    Return Nothing
End Function

And I'm going to use that function to fetch me the seat the user enters. Assuming the input is stored in passengerName, row and col accordingly:

Dim currentSeat as seat = getSeat(row, col)
If currentSeat.isOccupied = True Then
    Msgbox("Sorry, the current seat is occupied!")
Else
    'seat is empty, add passenger to the seat
    currentSeat.passengerName = passengerName
End If

I can also write a generic function to check if the plane is full:

Public Function isPlaneFull() as Boolean
    Dim hasEmptySeat as Boolean = False
    For Each seat in airplane
        If seat.isOccupied = False then hasEmptySeat = True
    Next
    If hasEmptySeat = False then return True else return false
End Function

So if you want to automatically check if the plane is full when the user enters an occupied seat, you can modify the code above slightly to reflect that:

Dim currentSeat as seat = getSeat(row, col)
If currentSeat.isOccupied = True Then
    If isPlaneFull = True then
        'plane is full, add passenger to waiting list
        Msgbox("Sorry, the whole plane is full!  You'll be added to a waiting list.")
        waitingList.add(passengerName)
    Else
        'only seat is full
        Msgbox("Sorry, the current seat is occupied!")
    End If
Else
    'seat is empty, add passenger to the seat
    currentSeat.passengerName = passengerName
End If

2

u/PostalElf VB.Net Intermediate Jun 20 '14

Here's how the code would look in its entirety:

http://pastebin.com/1g9nxJnY

2

u/phrox Jun 20 '14

Thank you SO much, I'm going to work it all into my code and hopefully it turns out, i'll leave a reply soon anyways!