Welcome to Dream.In.Code
Become a VB Expert!

Join 149,760 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,540 people online right now. Registration is fast and FREE... Join Now!




Passenger Booking Application

2 Pages V  1 2 >  
Reply to this topicStart new topic

Passenger Booking Application

KKKKK
17 Feb, 2007 - 12:15 PM
Post #1

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 15


My Contributions
I am doing my computing now and am having real trouble with this program. I have to write a program that:


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 and creates the final ID from the first 6 letters of the ID.


Here is what I have done so far, please look and see if you see any errors and improvements as I don't think the program works.:




CODE
Option Explicit
Dim ids(4) As String
Dim bookings(4) As String
Dim counter As Integer
Dim passengerid As String
Dim finalID As String
Dim seatnumber As Interger
Dim flightnuber As String

Private Sub create_arrays(ByRef ids(), flightnumber, bookings())
ids(0) = "WallaceW48P01"
ids(1) = "LauderH468P02"
ids(2) = "CarnegieA468P3"
ids(3) = "ScottW468P04"
ids(4) = "DeBruisR468P05"
flightnumber = "HA468"
bookings(1) = ""
bookings(2) = ""
bookings(3) = ""
bookings(4) = ""
End Sub

Private Sub check_id(ByRef ids(), passengerid)
Dim found As Boolean
passengerid=InputBox("Please enter your user ID")
If ids(counter) = passengerid Then
found = True
MsgBox ("The ID you entered was accepted")
Else
MsgBox ("The ID you entered was invalid, please re=enter")
End If
Loop Until found = True
End Sub

Private Sub book_seat(ByRef bookings(), seatnumber, id() ByVal passengerid
Dim found As Boolean
Do
seatnumber=InputBox("Enter the seat number you want between 1 and 5")
Do
If seatnumber<1 Or seatnumber>5 Then
seatnumber=InputBox("The seat you have entered is not between 1 and 5.")
End If
Loop Until seatnumber >1 And seatnumber <5
If bookings(seatnumber)=""Then
found=True
bookings(seatnumber)="Booked"
MsgBox("Your seat has been booked")
Else
MsgBox("Seat is not available, please enter another number")
End If
Loop Until found = True
End Sub

Private Sub display_results(ByVal flightnumber, passengerid, seatnumber)
finalid=Mid$(passengerid, 1, Len(passengerid), 6)
label1.Caption = flightnumber
label2.Caption = passengerid
label3.Caption = finalid
label4.Caption = seatnumber
End Sub

User is offlineProfile CardPM
+Quote Post

KeyWiz
RE: Passenger Booking Application
17 Feb, 2007 - 01:09 PM
Post #2

D.I.C Regular
Group Icon

Joined: 26 Oct, 2006
Posts: 428


Dream Kudos: 125
My Contributions
ok, first of all, there is no Start point in this code, so nothing here will be executed as is.

You need to initiate activation of these code blocks from the main form, using a button or checking for a period of time, etc.

Your Loop until SeatNumber >1 and SeatNumber < 5 will only pass 2, 3 and 4
change it to - >0 and <6 or change it to - >=1 and <=5 to trap 1 thru 5

Your Sub BookSeat appears to be attempting to pass in values and return a value is this correct? Your return value was Dimensioned as a String.

This post has been edited by KeyWiz: 17 Feb, 2007 - 01:10 PM
User is offlineProfile CardPM
+Quote Post

KKKKK
RE: Passenger Booking Application
17 Feb, 2007 - 01:23 PM
Post #3

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 15


My Contributions
Yeah, sorry wrote this in notepad as I don't have VB on this com. The seatsbooked should take in the number of the seat the user wants and if available give out this seat but if it can't it should tell the user the seat is taken and another number should be put in.
Corrected code:

CODE
Option Explicit
Dim ids(4) As String
Dim bookings(4) As String
Dim counter As Integer
Dim passengerid As String
Dim finalID As String
Dim seatnumber As Interger
Dim flightnumber As String
Private Sub Command1_Click()

Private Sub create_arrays(ByRef ids(), flightnumber, bookings())
ids(0) = "WallaceW48P01"
ids(1) = "LauderH468P02"
ids(2) = "CarnegieA468P3"
ids(3) = "ScottW468P04"
ids(4) = "DeBruisR468P05"
flightnumber = "HA468"
bookings(1) = ""
bookings(2) = ""
bookings(3) = ""
bookings(4) = ""
End Sub

Private Sub check_id(ByRef ids(), passengerid)
Dim found As Boolean
passengerid=InputBox("Please enter your user ID")
If ids(counter) = passengerid Then
found = True
MsgBox ("The ID you entered was accepted")
Else
MsgBox ("The ID you entered was invalid, please re=enter")
End If
Loop Until found = True
End Sub

Private Sub book_seat(ByRef bookings(), seatnumber, id() ByVal passengerid
Dim found As Boolean
Do
seatnumber=InputBox("Enter the seat number you want between 1 and 5")
Do
If seatnumber <1 Or seatnumber >5 Then
seatnumber=InputBox("The seat you have entered is not between 1 and 5.")
End If
Loop Until seatnumber >=1 And seatnumber <=5
If bookings(seatnumber)=""Then
found=True
bookings(seatnumber)="Booked"
MsgBox("Your seat has been booked")
Else
MsgBox("Seat is not available, please enter another number")
End If
Loop Until found = True
End Sub

Private Sub display_results(ByVal flightnumber, passengerid, seatnumber)
finalid=Mid$(passengerid, 1, Len(passengerid), 6)
label1.Caption = flightnumber
label2.Caption = passengerid
label3.Caption = finalid
label4.Caption = seatnumber
End Sub


This post has been edited by KKKKK: 17 Feb, 2007 - 01:25 PM
User is offlineProfile CardPM
+Quote Post

KKKKK
RE: Passenger Booking Application
17 Feb, 2007 - 04:20 PM
Post #4

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 15


My Contributions
Got VB on this com now, still need help would be much appreciated. Can't add anymore to code above, please help.
User is offlineProfile CardPM
+Quote Post

KKKKK
RE: Passenger Booking Application
18 Feb, 2007 - 09:30 AM
Post #5

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 15


My Contributions
Any more help?
User is offlineProfile CardPM
+Quote Post

KKKKK
RE: Passenger Booking Application
18 Feb, 2007 - 12:34 PM
Post #6

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 15


My Contributions
I know I've made a stupid mistake here but I've been working for hours now and my head is fried. I get the message 'Wrong number of arguments or invalid property assignment'. This is indicated in the display_results part and I have no clue what I have done wrong. The program will also only accept the ID, 'WallaceW48P01' and none of the other ones any suggestions how to fix this?



Here is the whole code if needed:

CODE
Option Explicit
Dim ids(4) As String
Dim bookings(4) As String
Dim counter As Integer
Dim passengerid As String
Dim finalID As String
Dim seatnumber As Integer
Dim flightnumber As String
Private Sub Command1_Click()
Call create_arrays
Call check_id
Call book_seat
Call display_results
End Sub
Private Sub create_arrays()
ids(0) = "WallaceW48P01"
ids(1) = "LauderH468P02"
ids(2) = "CarnegieA468P3"
ids(3) = "ScottW468P04"
ids(4) = "DeBruisR468P05"
flightnumber = "HA468"
bookings(1) = ""
bookings(2) = ""
bookings(3) = ""
bookings(4) = ""
End Sub

Private Sub check_id()
Dim found As Boolean
Do
passengerid = InputBox("Please enter your user ID")
If ids(counter) = passengerid Then
found = True
MsgBox ("The ID you entered was accepted")
Else
MsgBox ("The ID you entered was invalid, please re=enter")
End If
Loop Until found = True
End Sub

Private Sub book_seat()
Dim found As Boolean
Do
seatnumber = InputBox("Enter the seat number you want between 1 and 5")
Do
If seatnumber < 1 Or seatnumber > 5 Then
seatnumber = InputBox("The seat you have entered is not between 1 and 5.")
End If
Loop Until seatnumber >= 1 And seatnumber <= 5
If bookings(seatnumber) = "" Then
found = True
bookings(seatnumber) = "Booked"
MsgBox ("Your seat has been booked")
Else
MsgBox ("Seat is not available, please enter another number")
End If
Loop Until found = True
End Sub

Private Sub display_results()
finalID = Mid$(passengerid, 1, Len(passengerid), 6)
Label1.Caption = flightnumber
Label2.Caption = passengerid
Label3.Caption = finalID
Label4.Caption = seatnumber
End Sub



Thankyou in advance, any help will be GREATLY appreciated.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Passenger Booking Application
18 Feb, 2007 - 02:46 PM
Post #7

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
A couple of errors...

#1 The Mid$ function returns a sub string as follows

subSt=Mid$(WholeSt, Begining, length)

so say WholeSt="This is a test" and I wanted "a test" I would do

subSt=Mid$(WholeSt, 9, 6)

since it looked like you were just trying to get the last bit of the passengerID "WallaceW48P01" --> "W48P01" you can change things to Right$(PassemgerId,6)

(Edit) OOpse it lookes like the FinalID is supposed to be the "first 6 letters of the ID." So you would want to use the Left$() function. Never could tell my left from my right...

#2 In Command1_Click you call create_arrays... this is BAD!!!
Each time you were clicking the button you were eracing all of your booking info. So I was able to book a seat for Wallace over and over and over... because each time it checked the seat was open.

#3 The array Bookings is dimentioned 0 to 4... yes there are 5 positions but they are not 1 though 5 so you have to scale you seatnumbers to match. Bookings(seatnumber -1)="booked" etc. BTW as written when you choose seat 5 you get an error.

#4 You took out the loop in Check_Id... and this meant that only the id "WallaceW48P01" worked...

#5 You use a Lot of Global Variables. Although it is possible to write perfect code with lots of Globals programmers have found that it is best to keep them to a minimum. The worst one is the Counter... as it is possable in event driven programming for Counter to be reassigned durring a loop. It would be a hard error to catch as you could only see it when the right event was triggered.

Now that I have said all of that let me say that you are not doing bad. The code you have written is better than some of the code I have seen in college classes. One thing that will hep you is to TRY TO BREAK your program when you run it. Type in wrong input and see what happens... I found most of the above bugs from trying out differnet inputs into you code. Sometimes you need to see the error condition to know something is wrong.


Here is what I found ironed out:

CODE

Option Explicit

Dim ids(4) As String '(0 to 4)
Dim bookings(4) As String '(0 to 4 not 1 to 5)
Dim counter As Integer '*** Global Counter! Bad idea...
Dim passengerid As String
Dim finalID As String
Dim seatnumber As Integer
Dim flightnumber As String

Private Sub Command1_Click()
'**** BIG MISTAKE!!! Each time you click command1 the arrays
'  reset and you loose all booking information!!!
'  This belongs in Form_Load event handeler Or we need to
'  see if the arrays are already initalized.
'Call create_arrays
If ids(0) <> "WallaceW48P01" Then create_arrays
Call check_id
Call book_seat
Call display_results
End Sub

Private Sub create_arrays()
    ids(0) = "WallaceW48P01"
    ids(1) = "LauderH468P02"
    ids(2) = "CarnegieA468P3"
    ids(3) = "ScottW468P04"
    ids(4) = "DeBruisR468P05"
    flightnumber = "HA468"
    '*** You forgot about bookings(0) ***
    bookings(0) = ""
    bookings(1) = ""
    bookings(2) = ""
    bookings(3) = ""
    bookings(4) = ""
End Sub

Private Sub check_id()
    Dim found As Boolean
    Do
        passengerid = InputBox("Please enter your user ID")
        '*** We have to check the entire array... not just element 0
        For counter = 0 To 4
            If ids(counter) = passengerid Then
                found = True
                MsgBox ("The ID you entered was accepted")
                Exit For
            End If
        Next counter
        If Not found Then MsgBox ("The ID you entered was invalid, please re=enter")
    Loop Until found = True
End Sub

Private Sub book_seat()
    Dim found As Boolean
    Dim TheInput As String
    Do
        Do
            TheInput = InputBox("Enter the seat number you want between 1 and 5")
            seatnumber = Val(TheInput)
            If seatnumber < 1 Or seatnumber > 5 Then
                MsgBox "Invalid seat number!"
            End If
        Loop Until seatnumber >= 1 And seatnumber <= 5
        '*** Note: the seats are numbered 1 though 5
        '*** and the array is numbered 0 though 4
        If bookings(seatnumber - 1) = "" Then
            found = True
            bookings(seatnumber - 1) = "Booked"
            MsgBox ("Your seat has been booked")
        Else
            MsgBox ("Seat is not available, please enter another number")
        End If
    Loop Until found
End Sub

Private Sub display_results()
'finalID = Right$(passengerid, 6)
finalID=Left$(passengerid,6 )
Label1.Caption = flightnumber
Label2.Caption = passengerid
Label3.Caption = finalID
Label4.Caption = seatnumber
End Sub



This post has been edited by NickDMax: 18 Feb, 2007 - 02:54 PM
User is offlineProfile CardPM
+Quote Post

KKKKK
RE: Passenger Booking Application
19 Feb, 2007 - 09:49 AM
Post #8

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 15


My Contributions
Thanks for the help you are an actual life saver! I may have failed without this help thanks a lot!
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: Passenger Booking Application
19 Feb, 2007 - 09:51 AM
Post #9

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 15,262



Thanked: 61 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
Nick, thanks for helping KKKKK out and taking the time to explain. That's what it's all about here at DIC!!


User is online!Profile CardPM
+Quote Post

KKKKK
RE: Passenger Booking Application
19 Feb, 2007 - 11:03 AM
Post #10

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 15


My Contributions
Ok just one more question is there anyway I could simplify these two bits of code as I am not entirely sure how these work myself so don't really think I should use them in my coding.

CODE
If Not found Then MsgBox ("The ID you entered was invalid, please reenter")

CODE
If ids(0) <> "WallaceW48P01"


And in the book_seat why is it seatnumber - 1. Thats me finished if I know this the program is done and I'm happy. Thankyou once again!

This post has been edited by KKKKK: 19 Feb, 2007 - 11:05 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Passenger Booking Application
19 Feb, 2007 - 02:17 PM
Post #11

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
QUOTE(KKKKK @ 19 Feb, 2007 - 12:03 PM) *

Ok just one more question is there anyway I could simplify these two bits of code as I am not entirely sure how these work myself so don't really think I should use them in my coding.

CODE
If Not found Then MsgBox ("The ID you entered was invalid, please reenter")

CODE
If ids(0) <> "WallaceW48P01"


And in the book_seat why is it seatnumber - 1. Thats me finished if I know this the program is done and I'm happy. Thankyou once again!


You are right, I don't think you should use them if you don't understand them either.

CODE

If Not Found then MsgBox("...")
'same as
If Found=False then
    MsgBox("...")
    End if
'Found was set to TRUE when we found a valid ID, if found is NOT TRUE (i.e. FLASE) then
'  we need to tell the user to select again. This is not an important line, the code will work with out it
'  but the user might be confused.


CODE

If ids(0) <> "WallaceW48P01"
'in the sub create_arrays we have the line ids(0)="WallaceW48P01"
'   so if ids(0) is not equal (<>) to "WallaceW48P01" then the sub create_arrays has not run yet.
'   This is not the BEST way to do this, but it works and I only wanted to edit your code not write
'   a new program.


Lastly the seatnumber - 1: The user enters in a number 1-5 but the array is numbered 0-4 so if the user entered "5" the program would crash since there is no bookings(5). So what do we do? There are two choices:

seatnumber - 1 makes the 5 a 4, the 4 a 3, the 3 a 2, 2 a 1, 1 to 0. That works.

The other thing you could do is dimention bookings(1 to 5) but this really is a lame way of getting by it since VB is the only language that really lets you get away with this.


User is offlineProfile CardPM
+Quote Post

KKKKK
RE: Passenger Booking Application
20 Feb, 2007 - 09:37 AM
Post #12

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 15


My Contributions
Okay thanks a lot thats all fine! One more problem however the String at the end where the first 6 letters have been kept I have just found out that the last 6 letters have to be removed from the ID and the rest kept. So this bit:

CODE
finalID=Left$(passengerid,6 )


Is incorrect. Could you please change it to remove the last 6 characters of the 'passengerid' then it will work fine. Thankyou once again! smile.gif
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 06:12AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month