Passenger Booking Application

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1758 Views - Last Post: 08 March 2007 - 01:56 PM Rate Topic: -----

#1 KKKKK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-February 07

Passenger Booking Application

Posted 17 February 2007 - 01:15 PM

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.:




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


Is This A Good Question/Topic? 0
  • +

Replies To: Passenger Booking Application

#2 KeyWiz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 438
  • Joined: 26-October 06

Re: Passenger Booking Application

Posted 17 February 2007 - 02:09 PM

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 February 2007 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 KKKKK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-February 07

Re: Passenger Booking Application

Posted 17 February 2007 - 02:23 PM

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:

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 February 2007 - 02:25 PM

Was This Post Helpful? 0
  • +
  • -

#4 KKKKK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-February 07

Re: Passenger Booking Application

Posted 17 February 2007 - 05:20 PM

Got VB on this com now, still need help would be much appreciated. Can't add anymore to code above, please help.
Was This Post Helpful? 0
  • +
  • -

#5 KKKKK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-February 07

Re: Passenger Booking Application

Posted 18 February 2007 - 10:30 AM

Any more help?
Was This Post Helpful? 0
  • +
  • -

#6 KKKKK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-February 07

Re: Passenger Booking Application

Posted 18 February 2007 - 01:34 PM

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:

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.
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Passenger Booking Application

Posted 18 February 2007 - 03:46 PM

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:

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 February 2007 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#8 KKKKK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-February 07

Re: Passenger Booking Application

Posted 19 February 2007 - 10:49 AM

Thanks for the help you are an actual life saver! I may have failed without this help thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#9 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1812
  • View blog
  • Posts: 20,232
  • Joined: 17-March 01

Re: Passenger Booking Application

Posted 19 February 2007 - 10:51 AM

Nick, thanks for helping KKKKK out and taking the time to explain. That's what it's all about here at DIC!!
Was This Post Helpful? 0
  • +
  • -

#10 KKKKK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-February 07

Re: Passenger Booking Application

Posted 19 February 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.

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

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 February 2007 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Passenger Booking Application

Posted 19 February 2007 - 03:17 PM

View PostKKKKK, on 19 Feb, 2007 - 12:03 PM, said:

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.

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

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.

 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.



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.
Was This Post Helpful? 0
  • +
  • -

#12 KKKKK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-February 07

Re: Passenger Booking Application

Posted 20 February 2007 - 10:37 AM

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:

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! :)
Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Passenger Booking Application

Posted 20 February 2007 - 11:41 AM

Well I will leave this one as a "project for the student."

You want to go back to your idea of using the MID$ function

sA=MID$("This is a test", begin, length)

So if I wanted to extract the word is I would use:

sA=MID$("This is a test", 6, 2)

A note on the numbering. In an array of chars the first letter would be in element 0, but in a string the first letter is numbered 1. I mention this because I have, from time to time, forgotten to take this into account.

So the starting positions are: T=1, h=2, i=3, s=4, space=5, i=6, etc.

So if I wanted to get the middle two chars of an even length string I might use the following

Dim sEven as String
Dim iStart as Integer
Dim sOutput as String

sEven="ABCDEF"
iStart=Len(sEven)/2   '6/2=3 the starting position of C=3
sOutput = Mid$(sEven, iStart, 2)



(WARNING! Pedantic Statement)As a programmer you should begin to look at problems in terms of the tools you have to solve them. Your problem breaks into two questions: How do I find the starting point? How do I find the length?

Hint: In your original post you only need to change one char to get the right result.

This post has been edited by NickDMax: 20 February 2007 - 11:45 AM

Was This Post Helpful? 0
  • +
  • -

#14 KKKKK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-February 07

Re: Passenger Booking Application

Posted 20 February 2007 - 12:29 PM

Ok thanks for all the help!
Was This Post Helpful? 0
  • +
  • -

#15 KKKKK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 16-February 07

Re: Passenger Booking Application

Posted 26 February 2007 - 10:17 AM

Yet another problem, when I input my first user name and seat number the program works perfectly however when I enter the second, third, fourth and fifth the program will accept any input. I think there is a problem with my loop but I am not sure. Also the 'finalID' part which is made from the letters that are left after the last 6 letters are removed from the 'passengerid' string has not been completed so please ignore this. The layout has changed due to advice from my teacher. Please help!

Option Explicit

Dim ids(5) As String
Dim bookings(5) As String
Dim counter As Integer
Dim passengerid As String
Dim finalID As String
Dim seatnumber As Integer
Dim flightnumber As String
Dim idfound As Boolean
Dim seatfound As Boolean

Private Sub Command1_Click()
	ids(1) = "WallaceW48P01"
	ids(2) = "LauderH468P02"
	ids(3) = "CarnegieA468P3"
	ids(4) = "ScottW468P04"
	ids(5) = "DeBruisR468P05"
	flightnumber = "HA468"
	bookings(1) = ""
	bookings(2) = ""
	bookings(3) = ""
	bookings(4) = ""
	bookings(5) = ""
	
	Do
		passengerid = InputBox("Please enter your user ID")
		For counter = 1 To 5
			If ids(counter) = passengerid Then
				idfound = True
				MsgBox ("The ID you entered was accepted")
			End If
		Next counter
		If idfound = False Then
	MsgBox ("The ID you entered was invalid, please re-enter")
	End If
	Loop Until idfound = True

	Do
		Do
			seatnumber = InputBox("Enter the seat number you want between 1 and 5")
			If seatnumber < 1 Or seatnumber > 5 Then
				MsgBox "Invalid seat number!"
			End If
		Loop Until seatnumber >= 1 And seatnumber <= 5
		If bookings(seatnumber) = "" Then
			seatfound = True
			bookings(seatnumber) = "Booked"
			MsgBox ("Your seat has been booked")
		Else
			MsgBox ("Seat is not available, please enter another number")
		End If
	Loop Until seatfound = True
	
Label1.Caption = flightnumber
Label2.Caption = passengerid
Label3.Caption = finalID
Label4.Caption = seatnumber
End Sub

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2