I have been working on this for several hours, and I still cannot figure it out. I'm close but the Items are not adding correctly. What this form is suppose to do is by clicking on this button will display a message box that shows the sum of the capacity values of all airplanes in the List(of Product) as well as the count of how many airplanes are in the list. Create these totals by looping through the list, and checking for the type of each item in the list. If an item is an Airplane instance:
• Increment the airplane count.
• Cast the Product instance as an Airplane so that it can be referred to as an Airplane (and not just more generically, as a Product instance)
• Once this is done, then you can access the Capacity property that will be used in summing up all capacity values.
The code will have to go through all items in the list. All Cars will be ignored, and only if an item is an Airplane does the code update the count and capacity total variables.
Option Strict On
Option Explicit On
Option Infer Off
Public Class frmProductMaint
Dim products As New List(Of Product)
Private Sub frmProductMaint_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
products.Add(New Car("1000", "VW", "Purple", 10000))
products.Add(New Airplane("2000", "Lear Jet", CStr(12), 500000))
products.Add(New Airplane("3000", "Piper Cub", CStr(4), 75000))
products.Add(New Car("4000", "Cadillac", "Pink", 40000))
Me.FillProductListBox()
End Sub
Private Sub FillProductListBox()
lstProducts.Items.Clear()
For Each p As Product In products
lstProducts.Items.Add(p.GetDisplayText(vbTab))
Next
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnAdd.Click
Dim newProductForm As New frmNewProduct
Dim product As Product = newProductForm.GetNewProduct
If product IsNot Nothing Then
products.Add(product)
Me.FillProductListBox()
End If
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnDelete.Click
Dim i As Integer = lstProducts.SelectedIndex
If i <> -1 Then
Dim product As Product = products(i)
Dim message As String = "Are you sure you want to delete " _
& product.Description & "?"
Dim button As DialogResult = MessageBox.Show(message, _
"Confirm Delete", MessageBoxButtons.YesNo)
If button = DialogResult.Yes Then
products.Remove(product)
Me.FillProductListBox()
End If
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnShowTotalAirplaneCapacity_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowTotalAirplaneCapacity.Click
For Each p As Product In products
Dim Airplane1 As Airplane
Dim a As String = p.GetType.Name
If a = "Airplane" = Then
Airplane1 = CType(p, Airplane)
Airplane1.Capacity = CStr(MessageBox.Show("Total Capacity: " & (CStr(lstProducts.Items.Count) & vbCrLf &
"Total Number of Planes: " & (CStr(lstProducts.Items.Count)))))
End If
Next
End Sub
End Class
This portion of the code it what I am having the most difficult time with:
Private Sub btnShowTotalAirplaneCapacity_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowTotalAirplaneCapacity.Click
For Each p As Product In products
Dim Airplane1 As Airplane
Dim a As String = p.GetType.Name
If a = "Airplane" = Then
Airplane1 = CType(p, Airplane)
Airplane1.Capacity = CStr(MessageBox.Show("Total Capacity: " & (CStr(lstProducts.Items.Count) & vbCrLf &
"Total Number of Planes: " & (CStr(lstProducts.Items.Count)))))
End If
Next
End Sub
End Class
I have also attached a zip file of the homework. The portion that I am confused about is in the FrmProductMaint, under btnShowTotalAirplaneCapacity_Click.
Any help would be appreciated.
Thanks,
Attached File(s)
-
Program8.zip (100.74K)
Number of downloads: 20

New Topic/Question
Reply



MultiQuote




|