Original post by Dalit:
Hey I'm making a program for my Computing Class that does this
1. Stores 5 usernames in an array on strings
2. Asks user for their ID then validates it
3. Asks them for their desired seat number
4. Displays booking details.
Problem is, one user can keep going around in a loop and book every available seat. I want to display something that says "You have already booked a seat, would you like to change your seat?
Not sure how I would do this, I tried doing it with 2-D arrays but I only done a little on those and I'm not sure how to go about doing so.
Here is my code (I tidied it up as much as I could :S)
CODE
Private Sub Command1_Click()
'Declare Variables
Dim ids(5)
Dim responce As Integer
Dim bookings(5)
Dim inputval(5)
'Call Sub Routines
Call create_arrays(ids(), flightnr, bookings())
Do
Call check_id(ids(), pasid, inputval())
Call book_seat(bookings(), pasid, seatnr, ids())
Call display_results(flightnr, pasid, seatnr)
'Ask user if they want to exit
responce = MsgBox("Do you need to enter another value?", vbYesNo, "Exit?")
Loop Until responce = vbNo
End Sub
Private Sub create_arrays(ByRef ids(), flightnr, bookings())
'Create arrays/store flight details
ids(0) = "WallaceW48P01"
ids(1) = "LauderH468P02"
ids(2) = "CarnegieA468P3"
ids(3) = "ScottW468P04"
ids(4) = "DeBruisR468P05"
flightnr = "HA468"
bookings(1) = ""
bookings(2) = ""
bookings(3) = ""
bookings(4) = ""
bookings(5) = ""
End Sub
Private Sub check_id(ByRef ids(), pasid, inputval())
'Declare Variables
Dim found As Boolean
Dim counter As Integer
Dim counter2 As Integer
'Get user Input
pasid = InputBox("Enter your user ID")
'Input Validations
Do
For counter = 0 To 4
If ids(counter) = pasid Then
found = True
MsgBox ("The ID you was accepted")
If Not found Then MsgBox ("The ID you entered was invalid")
Else
End If
Next
Loop Until found = True
End Sub
Private Sub book_seat(ByRef bookings(), ByVal pasid, ByRef seatnr, ids())
'Declare variables
Dim found As Boolean
'Get input
seatnr = InputBox("Enter your desired seat number Between 1 and 5")
'Input Validation
Do
Do
If seatnr < 1 Or seatnr > 5 Then
seatnr = InputBox("That was an invalid number, Please enter your desired seat number Between 1 and 5")
End If
Loop Until seatnr >= 1 And seatnr <= 5
If bookings(seatnr) = "" Then
found = True
bookings(seatnr) = "taken"
Else
MsgBox ("seat number " & seatnr & " was not available")
End If
Loop Until found = True
If Not found Then MsgBox ("Your seat has been booked")
End Sub
Private Sub display_results(ByVal flightnr, pasid, seatnr)
'Declare Variables
Dim pasid2 As String
'string concatenation
pasid2 = Mid$(pasid, 1, Len(pasid) - 6)
'Display Results
txtbox.AddItem " Booking Confirmation"
txtbox.AddItem "Flight Number :" & flightnr
txtbox.AddItem "ID :" & pasid
txtbox.AddItem "Name :" & pasid2
txtbox.AddItem "Seat Number :" & seatnr
End Sub
I am also doing this program and having a hell of a lot of problems. I was wondering if anyone could perfect this code until it works the best they got in the other topic was:
CODE
Private Sub book_seat(ByRef bookings(), ByVal pasid, ByRef seatnr, ids())
Dim found As Boolean
Dim answer As Integer
Do
seatnr = InputBox("Enter your desired seat number Between 1 and 5")
'Input Validation x > 1, x < 5
Do
If seatnr < 1 Or seatnr > 5 Then
seatnr = InputBox("That was an invalid number, Please enter your desired seat number Between 1 and 5")
End If
Loop Until seatnr >= 1 And seatnr <= 5
'Check if seat is available
Dim seatsBooked As Integer
seatsBooked = 0
' Loop through each booking and count number times pasid has booked a seat
For a = 0 To 4
If bookings(a) = pasid Then
seatsBooked = seatsBooked + 1
End If
Next
' Make sure user hasn't previously booked a seat
If seatsBooked = 0 Or seatsBooked = 99 Then
If bookings(seatnr) = "" Then
found = True
bookings(seatnr) = pasid
MsgBox ("Seat " & seatnr & " was booked for: " & pasid)
Else
MsgBox ("seat number " & seatnr & " was not available, please enter a number between 1 and 5")
End If
Else
' Ask user if they want to change their booked seat.
MsgBox ("A seat has already been booked for your UserID: " & pasid)
answer = MsgBox("Do you want to change your booked seat?", vbYesNo, "Previous booking detected")
If responce = vbYes Then
seatsBooked = 98
Call book_seat(bookings(), pasid, seatnr, ids())
Else
End If
End If
Loop Until found = True
End Sub
I have no clue what the response part does also so could someone explain this.