Need help with a shipping calculator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aprillynn82
    New Member
    • Nov 2008
    • 4

    Need help with a shipping calculator

    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]
    Last edited by debasisdas; Nov 12 '08, 07:31 AM. Reason: Formatted using code tags
  • smartchap
    New Member
    • Dec 2007
    • 236

    #2
    I think you should modify the below code

    # 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
    #

    as below:

    # 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
    #
    at both the places and get the result. Plz confirm.

    Comment

    • QVeen72
      Recognized Expert Top Contributor
      • Oct 2006
      • 1445

      #3
      Hi,

      In Select Case, you have to Give "AND" not "OR"..
      Check the modified code here :

      [code=vb]
      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

      decPackageWeigh t = Val(txtPackageW eight.Text)
      If decPackageWeigh t > 0 and <= 20 Then
      Else
      MessageBox.Show ("Packages must weigh between 0 and 20 kgs.")
      txtPackageWeigh t.Focus()
      txtPackageWeigh t.SelectAll()
      Exit Sub
      End If
      decShippingDist ance = Val(txtShipping Distance.Text)
      If decShippingDist ance >=10 And decShippingDist ance <=3000 Then
      Else
      MessageBox.Show ("Packages can only be delivered between 10 and 3000 miles.")
      txtShippingDist ance.Focus()
      txtShippingDist ance.SelectAll( )
      Exit Sub
      End If
      If decPackageWeigh t <=2 Then
      decMileageCost = 0.01D
      ElseIf decPackageWeigh t>2 And decPackageWeigh t <= 6 Then
      decMileageCost = 0.15D
      ElseIf decPackageWeigh t>6 And decPackageWeigh t <= 10 Then
      decMileageCost = 0.02D
      Else
      decMileageCost = 0.025D
      End If
      decShippingCost = decMileageCost * decShippingDist ance
      lblShippingCost .Text = Format(decShipp ingCost,"0.00")
      End Sub
      [/code]

      I have removed some repeated codes..

      Regards
      Veena

      Comment

      • aprillynn82
        New Member
        • Nov 2008
        • 4

        #4
        This did work! Thank you for your help, I an new at this and didnt know = sign was needed there.

        April

        Originally posted by smartchap
        I think you should modify the below code

        # 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
        #

        as below:

        # 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
        #
        at both the places and get the result. Plz confirm.

        Comment

        • aprillynn82
          New Member
          • Nov 2008
          • 4

          #5
          Hello,

          Thank you for your help! This worked perfectly!

          April

          Originally posted by QVeen72
          Hi,

          In Select Case, you have to Give "AND" not "OR"..
          Check the modified code here :

          [code=vb]
          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

          decPackageWeigh t = Val(txtPackageW eight.Text)
          If decPackageWeigh t > 0 and <= 20 Then
          Else
          MessageBox.Show ("Packages must weigh between 0 and 20 kgs.")
          txtPackageWeigh t.Focus()
          txtPackageWeigh t.SelectAll()
          Exit Sub
          End If
          decShippingDist ance = Val(txtShipping Distance.Text)
          If decShippingDist ance >=10 And decShippingDist ance <=3000 Then
          Else
          MessageBox.Show ("Packages can only be delivered between 10 and 3000 miles.")
          txtShippingDist ance.Focus()
          txtShippingDist ance.SelectAll( )
          Exit Sub
          End If
          If decPackageWeigh t <=2 Then
          decMileageCost = 0.01D
          ElseIf decPackageWeigh t>2 And decPackageWeigh t <= 6 Then
          decMileageCost = 0.15D
          ElseIf decPackageWeigh t>6 And decPackageWeigh t <= 10 Then
          decMileageCost = 0.02D
          Else
          decMileageCost = 0.025D
          End If
          decShippingCost = decMileageCost * decShippingDist ance
          lblShippingCost .Text = Format(decShipp ingCost,"0.00")
          End Sub
          [/code]

          I have removed some repeated codes..

          Regards
          Veena

          Comment

          Working...