Instructions In this case, you will create a Visual Basic solution that allows t
ID: 3891171 • Letter: I
Question
Instructions
In this case, you will create a Visual Basic solution that allows the Island Breezes Sea Planes airline to assign seats to passengers. This program demonstrates the use of parallel one-dimensional arrays. One array is used to represent the availability of each seat. The other array is used to manage the RadioButton controls on the form. It introduces the technique of using an array of RadioButton objects. Here are instructions on how to design and code this project:
Step 1: Create the Project:
Create a Visual Basic Project using the project name “AirplaneSeating”.
Step 2 – Design the Form:
Design the form as shown in Figure 1. You will need two button controls, one textbox, one group box, eight radio buttons, one picture box, and two label controls.
Step 3 – Declare the form-level arrays:
Declare an array of Boolean values to indicate the availability of each seat:
Private availableSeats(7) As Boolean
Declare an object array as type RadioButton:
Private buttons(7) As RadioButton
Step 3 – Add code in the Form’s Load event to initialize the arrays:
Each object in the buttons array must be initialized to be a RadioButton object. In the Form’s Load event, write a loop to initialize each array element using this syntax:
buttons(i) = New RadioButton
After this loop, load each individual radio button into an array position, using this syntax (in this example, the first
radio button control’s name is seat10ARadioButton, and it is being loaded into the first object array position):
buttons(0) = seat10ARadioButton
Do this for each radio button control.
Finally, write a loop to set the Checked property of all of the buttons array elements to False.
Step 4 – Create a sub procedure for showing seat availability on the form:
Create a sub procedure named UpdateSeatButtons that will loop through the availableSeats array. If an element in the availableSeats array equals False, set the corresponding buttons array element’s Enabled property to False to visually indicate that this seat is no longer available.
Step 5 – Create a function for determining if there are any seats still available:
Create a Boolean function named CheckForAvailable that will loop through the availableSeats array to determine if there are any seats still available. If there is at least one seat available, return a True value; otherwise, return False.
Step 6 – Add code in the Confirm Seat button’s Click event to update the seating chart:
Loop through the buttons array to determine which button was selected. Set the corresponding availableSeats array element to False. Then call the UpdateSeatButtons sub procedure to update the visual seating chart.
Call the CheckForAvailable function to determine whether you should display a message indicating that the flight is full.
Step 7 – Finish up:
Be sure to add the code for the Exit button.
Step 8: Save and run
Save all files, then start the application. Test the program using various selections. Figure 2 shows a sample run of this program, with the user’s choices shown. Notice that the unavailable seats are grayed.
Extra Credit:
Add a second form to the project that produces a visual “boarding pass”. Design the form to look like a printed boarding pass. Include the passenger name and the seat assignment.
?? Seat Selection Island Breezes Sea Planes Passenger Name: John Smith Seat Selection 10-?? 114 12A C ? 10-? 118 12-B Confirm SeatExplanation / Answer
Screenshot:
Code:
Public Class Form1
Private availableSeats(7) As Boolean 'Array to hold Available Seats information
Private buttons(7) As RadioButton 'Radio Button Array
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim I As Integer
'Following loop initializes the radio buttons
For I = 0 To 7
buttons(I) = New RadioButton
Next I
'Following commands assigns control names
buttons(0) = seat10ARadioButton
buttons(1) = seat11ARadioButton
buttons(2) = seat12ARadioButton
buttons(3) = seat13ARadioButton
buttons(4) = seat10BRadioButton
buttons(5) = seat11BRadioButton
buttons(6) = seat12BRadioButton
buttons(7) = seat13BRadioButton
'Following loop initializes the radio button state and makes available seats as true
For I = 0 To 7
buttons(I).Checked = False
availableSeats(I) = True
Next I
End Sub
Private Sub UpdateSeatButtons()
'Procedure to disable seat button based on availability
Dim I As Integer
For I = 0 To 7
If availableSeats(I) = False Then
buttons(I).Enabled = False
End If
Next I
End Sub
Private Function CheckForAvailable() As Boolean
'Function to check availability of seats
Dim I As Integer
For I = 0 To 7
If availableSeats(I) = True Then
Return True
End If
Next I
MsgBox("There are no seats available. Flight is full.")
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim I As Integer
For I = 0 To 7
If buttons(I).Checked = True Then
availableSeats(I) = False
End If
Next I
UpdateSeatButtons()
CheckForAvailable()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End
End Sub
End Class