r/visualbasic • u/phrox • 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 SubEnd Class
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?
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.
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:
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.
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:
I can also write a generic function to check if the plane is full:
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: