By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
461,955 Members | 790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 461,955 IT Pros & Developers. It's quick & easy.

Need help with a shipping calculator

P: 4
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:

Expand|Select|Wrap|Line Numbers
  1. ' Shipping calculator
  2. ' Calculator will tell price for shipping a package when the weight
  3. ' and distance are entered.
  4. Public Class FrmShippingCalculator
  5.  
  6.  
  7.     Private Sub btnClearInformation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearInformation.Click
  8.         ' Clear the Text Boxes and lblShippingCost
  9.         txtPackageWeight.Clear()
  10.         txtShippingDistance.Clear()
  11.         lblShippingCost.Text = String.Empty
  12.     End Sub
  13.  
  14.     Private Sub btnExitApplication_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExitApplication.Click
  15.         'End the application by closing the form
  16.         Me.Close()
  17.     End Sub
  18.  
  19.     Private Sub btnCalculateRate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateRate.Click
  20.  
  21.  
  22.         Dim decPackageWeight As Decimal
  23.         Dim decShippingDistance As Decimal
  24.         Dim decShippingCost As Decimal
  25.         Dim decMileageCost As Decimal
  26.  
  27.         Try
  28.             decPackageWeight = CDec(txtPackageWeight.Text)
  29.         Catch ex As Exception
  30.             MessageBox.Show("Packages must weigh between 0 and 20 kgs.")
  31.             txtPackageWeight.Focus()
  32.             txtPackageWeight.SelectAll()
  33.             Exit Sub
  34.         End Try
  35.  
  36.         Try
  37.             decShippingDistance = CDec(txtShippingDistance.Text)
  38.         Catch ex As Exception
  39.             MessageBox.Show("Packages can only be delivered between 10 and 3000 miles.")
  40.             txtShippingDistance.Focus()
  41.             txtShippingDistance.SelectAll()
  42.         End Try
  43.  
  44.         Try
  45.             decShippingCost = decMileageCost * decShippingDistance
  46.         Catch ex As Exception
  47.             MessageBox.Show("Packages can only be delivered between 10 and 3000 miles.")
  48.             txtShippingDistance.Focus()
  49.             txtShippingDistance.SelectAll()
  50.         End Try
  51.  
  52.         ' Determind the Mileage Cost Rate.
  53.         Select Case decPackageWeight
  54.             Case Is < 2
  55.                 decMileageCost = 0.01D
  56.             Case Is > 2 Or CInt(decPackageWeight < 6)
  57.                 decMileageCost = 0.15D
  58.  
  59.             Case Is > 6 Or CInt(decPackageWeight < 10)
  60.                 decMileageCost = 0.02D
  61.             Case Is > 10 Or CInt(decPackageWeight < 20)
  62.                 decMileageCost = 0.025D
  63.  
  64.         End Select
  65.         decShippingCost = decMileageCost * decShippingDistance
  66.         lblShippingCost.Text = CStr(decShippingCost)
  67.  
  68.         'Check the weight 
  69.         If decPackageWeight < 0 Then
  70.             MessageBox.Show("Packages cannot weigh less than 0 kgs")
  71.         ElseIf decPackageWeight > 20 Then
  72.             MessageBox.Show("Packages cannot weigh more than 20 kgs")
  73.             Return
  74.         End If
  75.  
  76.         'Check the distance
  77.         If decShippingDistance < 10 Then
  78.             MessageBox.Show("We cannot ship to a distance less than 10 miles")
  79.         ElseIf decShippingDistance > 3000 Then
  80.             MessageBox.Show("We cannot ship to a distance more than 3000 miles")
  81.             Return
  82.         End If
  83.  
  84.         ' Determind the Mileage Cost Rate.
  85.         Select Case decPackageWeight
  86.  
  87.             Case Is < 2
  88.                 decMileageCost = 0.01D
  89.  
  90.             Case Is > 2 Or CInt(decPackageWeight < 6)
  91.                 decMileageCost = 0.15D
  92.  
  93.             Case Is > 6 Or CInt(decPackageWeight < 10)
  94.                 decMileageCost = 0.02D
  95.  
  96.             Case Is > 10 Or CInt(decPackageWeight < 20)
  97.                 decMileageCost = 0.025D
  98.  
  99.         End Select
  100.         decShippingCost = decMileageCost * decShippingDistance
  101.         lblShippingCost.Text = CStr(decShippingCost)
  102.  
  103.     End Sub
  104.  
  105. End Class
Nov 11 '08 #1
Share this Question
Share on Google+
4 Replies

smartchap
100+
P: 236
I think you should modify the below code

# Select Case decPackageWeight
# Case Is < 2
# decMileageCost = 0.01D
# Case Is > 2 Or CInt(decPackageWeight < 6)
# decMileageCost = 0.15D
#
# Case Is > 6 Or CInt(decPackageWeight < 10)
# decMileageCost = 0.02D
# Case Is > 10 Or CInt(decPackageWeight < 20)
# decMileageCost = 0.025D
#

as below:

# Select Case decPackageWeight
# Case Is <= 2
# decMileageCost = 0.01D
# Case Is > 2 Or CInt(decPackageWeight <= 6)
# decMileageCost = 0.15D
#
# Case Is > 6 Or CInt(decPackageWeight <= 10)
# decMileageCost = 0.02D
# Case Is > 10 Or CInt(decPackageWeight <= 20)
# decMileageCost = 0.025D
#
at both the places and get the result. Plz confirm.
Nov 12 '08 #2

QVeen72
Expert 100+
P: 1,445
Hi,

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

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnCalculateRate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateRate.Click
  2.         Dim decPackageWeight As Decimal
  3.         Dim decShippingDistance As Decimal
  4.         Dim decShippingCost As Decimal
  5.         Dim decMileageCost As Decimal
  6.  
  7.     decPackageWeight = Val(txtPackageWeight.Text)
  8.     If decPackageWeight > 0 and <= 20 Then
  9.     Else
  10.              MessageBox.Show("Packages must weigh between 0 and 20 kgs.")
  11.              txtPackageWeight.Focus()
  12.              txtPackageWeight.SelectAll()
  13.              Exit Sub
  14.     End If
  15.     decShippingDistance = Val(txtShippingDistance.Text)
  16.     If decShippingDistance >=10 And decShippingDistance <=3000 Then
  17.     Else
  18.              MessageBox.Show("Packages can only be delivered between 10 and 3000 miles.")
  19.              txtShippingDistance.Focus()
  20.              txtShippingDistance.SelectAll()
  21.          Exit Sub
  22.     End If
  23.         If decPackageWeight <=2 Then
  24.                 decMileageCost = 0.01D
  25.         ElseIf decPackageWeight>2 And decPackageWeight <= 6 Then
  26.                 decMileageCost = 0.15D
  27.         ElseIf decPackageWeight>6 And decPackageWeight <= 10 Then
  28.                 decMileageCost = 0.02D
  29.         Else
  30.                 decMileageCost = 0.025D
  31.         End If
  32.         decShippingCost = decMileageCost * decShippingDistance
  33.         lblShippingCost.Text = Format(decShippingCost,"0.00")
  34. End Sub
  35.  
I have removed some repeated codes..

Regards
Veena
Nov 12 '08 #3

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

April

I think you should modify the below code

# Select Case decPackageWeight
# Case Is < 2
# decMileageCost = 0.01D
# Case Is > 2 Or CInt(decPackageWeight < 6)
# decMileageCost = 0.15D
#
# Case Is > 6 Or CInt(decPackageWeight < 10)
# decMileageCost = 0.02D
# Case Is > 10 Or CInt(decPackageWeight < 20)
# decMileageCost = 0.025D
#

as below:

# Select Case decPackageWeight
# Case Is <= 2
# decMileageCost = 0.01D
# Case Is > 2 Or CInt(decPackageWeight <= 6)
# decMileageCost = 0.15D
#
# Case Is > 6 Or CInt(decPackageWeight <= 10)
# decMileageCost = 0.02D
# Case Is > 10 Or CInt(decPackageWeight <= 20)
# decMileageCost = 0.025D
#
at both the places and get the result. Plz confirm.
Nov 12 '08 #4

P: 4
Hello,

Thank you for your help! This worked perfectly!

April

Hi,

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

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnCalculateRate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateRate.Click
  2.         Dim decPackageWeight As Decimal
  3.         Dim decShippingDistance As Decimal
  4.         Dim decShippingCost As Decimal
  5.         Dim decMileageCost As Decimal
  6.  
  7.     decPackageWeight = Val(txtPackageWeight.Text)
  8.     If decPackageWeight > 0 and <= 20 Then
  9.     Else
  10.              MessageBox.Show("Packages must weigh between 0 and 20 kgs.")
  11.              txtPackageWeight.Focus()
  12.              txtPackageWeight.SelectAll()
  13.              Exit Sub
  14.     End If
  15.     decShippingDistance = Val(txtShippingDistance.Text)
  16.     If decShippingDistance >=10 And decShippingDistance <=3000 Then
  17.     Else
  18.              MessageBox.Show("Packages can only be delivered between 10 and 3000 miles.")
  19.              txtShippingDistance.Focus()
  20.              txtShippingDistance.SelectAll()
  21.          Exit Sub
  22.     End If
  23.         If decPackageWeight <=2 Then
  24.                 decMileageCost = 0.01D
  25.         ElseIf decPackageWeight>2 And decPackageWeight <= 6 Then
  26.                 decMileageCost = 0.15D
  27.         ElseIf decPackageWeight>6 And decPackageWeight <= 10 Then
  28.                 decMileageCost = 0.02D
  29.         Else
  30.                 decMileageCost = 0.025D
  31.         End If
  32.         decShippingCost = decMileageCost * decShippingDistance
  33.         lblShippingCost.Text = Format(decShippingCost,"0.00")
  34. End Sub
  35.  
I have removed some repeated codes..

Regards
Veena
Nov 12 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.