I can not seem to get the code correct to calculate shipping charges based on weight and distance. The info is:
Weight of the Package (in kilograms) / Shipping rate per Mile
2kg or less / $0.01
over 2kg, but not more than 6kg /$0.015
over 6kg, but not more than 10kg /$0.02
over 10kg, but not more than 20kg / $0.025
Input validation: Do not accept values of 0 or less for the weight of the package. Do not accept weights of more thann 20 kg(this is the maximum weight the company will ship) Do not accept distances of less than 10 miles or more than 3000 miles. These are the company's minimum and maximum shipping distances.
Every time I run it, the calculation comes out incorrectly. Any help would be appreciated!
The code I have is:
[CODE=vb]' Shipping calculator
' Calculator will tell price for shipping a package when the weight
' and distance are entered.
Public Class FrmShippingCalc ulator
Private Sub btnClearInforma tion_Click(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles btnClearInforma tion.Click
' Clear the Text Boxes and lblShippingCost
txtPackageWeigh t.Clear()
txtShippingDist ance.Clear()
lblShippingCost .Text = String.Empty
End Sub
Private Sub btnExitApplicat ion_Click(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnExitApplicat ion.Click
'End the application by closing the form
Me.Close()
End Sub
Private Sub btnCalculateRat e_Click(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnCalculateRat e.Click
Dim decPackageWeigh t As Decimal
Dim decShippingDist ance As Decimal
Dim decShippingCost As Decimal
Dim decMileageCost As Decimal
Try
decPackageWeigh t = CDec(txtPackage Weight.Text)
Catch ex As Exception
MessageBox.Show ("Packages must weigh between 0 and 20 kgs.")
txtPackageWeigh t.Focus()
txtPackageWeigh t.SelectAll()
Exit Sub
End Try
Try
decShippingDist ance = CDec(txtShippin gDistance.Text)
Catch ex As Exception
MessageBox.Show ("Packages can only be delivered between 10 and 3000 miles.")
txtShippingDist ance.Focus()
txtShippingDist ance.SelectAll( )
End Try
Try
decShippingCost = decMileageCost * decShippingDist ance
Catch ex As Exception
MessageBox.Show ("Packages can only be delivered between 10 and 3000 miles.")
txtShippingDist ance.Focus()
txtShippingDist ance.SelectAll( )
End Try
' Determind the Mileage Cost Rate.
Select Case decPackageWeigh t
Case Is < 2
decMileageCost = 0.01D
Case Is > 2 Or CInt(decPackage Weight < 6)
decMileageCost = 0.15D
Case Is > 6 Or CInt(decPackage Weight < 10)
decMileageCost = 0.02D
Case Is > 10 Or CInt(decPackage Weight < 20)
decMileageCost = 0.025D
End Select
decShippingCost = decMileageCost * decShippingDist ance
lblShippingCost .Text = CStr(decShippin gCost)
'Check the weight
If decPackageWeigh t < 0 Then
MessageBox.Show ("Packages cannot weigh less than 0 kgs")
ElseIf decPackageWeigh t > 20 Then
MessageBox.Show ("Packages cannot weigh more than 20 kgs")
Return
End If
'Check the distance
If decShippingDist ance < 10 Then
MessageBox.Show ("We cannot ship to a distance less than 10 miles")
ElseIf decShippingDist ance > 3000 Then
MessageBox.Show ("We cannot ship to a distance more than 3000 miles")
Return
End If
' Determind the Mileage Cost Rate.
Select Case decPackageWeigh t
Case Is < 2
decMileageCost = 0.01D
Case Is > 2 Or CInt(decPackage Weight < 6)
decMileageCost = 0.15D
Case Is > 6 Or CInt(decPackage Weight < 10)
decMileageCost = 0.02D
Case Is > 10 Or CInt(decPackage Weight < 20)
decMileageCost = 0.025D
End Select
decShippingCost = decMileageCost * decShippingDist ance
lblShippingCost .Text = CStr(decShippin gCost)
End Sub
End Class[/CODE]
Weight of the Package (in kilograms) / Shipping rate per Mile
2kg or less / $0.01
over 2kg, but not more than 6kg /$0.015
over 6kg, but not more than 10kg /$0.02
over 10kg, but not more than 20kg / $0.025
Input validation: Do not accept values of 0 or less for the weight of the package. Do not accept weights of more thann 20 kg(this is the maximum weight the company will ship) Do not accept distances of less than 10 miles or more than 3000 miles. These are the company's minimum and maximum shipping distances.
Every time I run it, the calculation comes out incorrectly. Any help would be appreciated!
The code I have is:
[CODE=vb]' Shipping calculator
' Calculator will tell price for shipping a package when the weight
' and distance are entered.
Public Class FrmShippingCalc ulator
Private Sub btnClearInforma tion_Click(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles btnClearInforma tion.Click
' Clear the Text Boxes and lblShippingCost
txtPackageWeigh t.Clear()
txtShippingDist ance.Clear()
lblShippingCost .Text = String.Empty
End Sub
Private Sub btnExitApplicat ion_Click(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnExitApplicat ion.Click
'End the application by closing the form
Me.Close()
End Sub
Private Sub btnCalculateRat e_Click(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnCalculateRat e.Click
Dim decPackageWeigh t As Decimal
Dim decShippingDist ance As Decimal
Dim decShippingCost As Decimal
Dim decMileageCost As Decimal
Try
decPackageWeigh t = CDec(txtPackage Weight.Text)
Catch ex As Exception
MessageBox.Show ("Packages must weigh between 0 and 20 kgs.")
txtPackageWeigh t.Focus()
txtPackageWeigh t.SelectAll()
Exit Sub
End Try
Try
decShippingDist ance = CDec(txtShippin gDistance.Text)
Catch ex As Exception
MessageBox.Show ("Packages can only be delivered between 10 and 3000 miles.")
txtShippingDist ance.Focus()
txtShippingDist ance.SelectAll( )
End Try
Try
decShippingCost = decMileageCost * decShippingDist ance
Catch ex As Exception
MessageBox.Show ("Packages can only be delivered between 10 and 3000 miles.")
txtShippingDist ance.Focus()
txtShippingDist ance.SelectAll( )
End Try
' Determind the Mileage Cost Rate.
Select Case decPackageWeigh t
Case Is < 2
decMileageCost = 0.01D
Case Is > 2 Or CInt(decPackage Weight < 6)
decMileageCost = 0.15D
Case Is > 6 Or CInt(decPackage Weight < 10)
decMileageCost = 0.02D
Case Is > 10 Or CInt(decPackage Weight < 20)
decMileageCost = 0.025D
End Select
decShippingCost = decMileageCost * decShippingDist ance
lblShippingCost .Text = CStr(decShippin gCost)
'Check the weight
If decPackageWeigh t < 0 Then
MessageBox.Show ("Packages cannot weigh less than 0 kgs")
ElseIf decPackageWeigh t > 20 Then
MessageBox.Show ("Packages cannot weigh more than 20 kgs")
Return
End If
'Check the distance
If decShippingDist ance < 10 Then
MessageBox.Show ("We cannot ship to a distance less than 10 miles")
ElseIf decShippingDist ance > 3000 Then
MessageBox.Show ("We cannot ship to a distance more than 3000 miles")
Return
End If
' Determind the Mileage Cost Rate.
Select Case decPackageWeigh t
Case Is < 2
decMileageCost = 0.01D
Case Is > 2 Or CInt(decPackage Weight < 6)
decMileageCost = 0.15D
Case Is > 6 Or CInt(decPackage Weight < 10)
decMileageCost = 0.02D
Case Is > 10 Or CInt(decPackage Weight < 20)
decMileageCost = 0.025D
End Select
decShippingCost = decMileageCost * decShippingDist ance
lblShippingCost .Text = CStr(decShippin gCost)
End Sub
End Class[/CODE]
Comment