473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Creating a Shipping Charge Calculator

26
Hello everybody. I am a complete noob in writing VB code. I don't have a problem drawing a form, but when it comes to code. I am lost.
I have a user interface design class and we have a small project due. I was wondering if anyone could give me some mega help.
The project we have to complete is the following:

The Fast Freight Shipping Company charges the rates listed in the following table:

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

Create an application that allows the user to enter the weight of the package and the distance it is to be shipped, and then displays the charges.

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. Use exception handling to check for non-numeric data.



I'm knd of asking for someone to pretty much write the code I would need to make this function properly. I will be trying to mess around with it while I wait for any response. Thanks very much in advancne
Mar 27 '07 #1
19 9678
Dököll
2,364 Expert 2GB
Ahhh, the good old days :-) Hello rents!

I have had similar projects in the past..I am sure likewise for members here. Would you be able to post what you have thus far so one can take a look?

It'd be helpful to you and to this forum if in need of a quick response...

When is this due?

Dököll
Mar 28 '07 #2
rents
26
Dokoll,

Thanks for the response. So far, I have only created the form itself. With buttons and labels and what not. The only code I have entered is for the exit button to work :)

I'm not exactly sure what you like to me show on here, so I just copied and pasted the part where the code is placed.

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

End Sub

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

End Sub

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'Exit Calculator

Me.Close()

End Sub

Private Sub txtWeight_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWeight.TextChanged

End Sub

Private Sub txtDistance_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDistance.TextChanged

End Sub

Private Sub lblCost_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblCost.TextChanged

End Sub

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

End Sub
End Class

i guess this would give you an idea as to what I have on the form. I feel a little silly not knowing even where to begin to write code. I mean I know where it goes, but just confused as to what to type in first.

If you need more info from me, please let me know. This project is due on Saturday, but I'd like to have it finished by at least Friday.
Thanks
Mar 28 '07 #3
Dököll
2,364 Expert 2GB
OK, Rents...Thanks for your prompt response. I have a simple code you can add to the mix. Let me fetch it and will post. If this does not work, I'll let someone else take a stab at it. In a bit!
Mar 28 '07 #4
Dököll
2,364 Expert 2GB
Also, perhaps you can reduce your button clicks to at least a couple! I just noticed you have buttons for almost every event. Is this necessary? It's likely the project at hand wants you to submit in this fashion and that's ok, if this is a fact, please ignore my comment here.

To begin, and assuming this is what you hope to achieve:

(1) you will need to dimension a place holder for your dollar amounts as currency (i.e. Textbox that will carry $0.00...)
(2) you will also need to dimension a place holder as integer (i.e. Textbox that should carry kg units and so on...

Please stay tuned while I fetch for code.
Mar 28 '07 #5
Dököll
2,364 Expert 2GB
rents, my friend, I do not have good news, but no worries. My small program is almost 5 years old, hadn't touch it since graduation. Anyway, the code is not working properly. I could not find you a good tutorial on this, so I will attempt to help you from memory:

(1) Looks like you will need to tell the program to look for instances where a certain textbox/label has greater or equal to 10 miles (a select case or if statement should help you deal with this).

(2) You will also need to tell VB you want not more but less than 3000 miles, you can add this in the same select or if statement, or a loop of some sort

(3) VB must know you will not accept more than 20kg, add to your statements as well

(4) As mentioned before, you will need to dimension the currency textbox/label as currency so VB know amounts will be added.

Here is part of my old code that works:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     Dim num_one As Currency ''Specific to a textbox called Price.Text
  3.     Dim num_two As Integer    'Specific to a textbox called Items.Text
  4.     Dim Total_results_num As Currency ''Specific to a label called Total_results
  5.  
  6.     num_one = Int(Price.Text)
  7.     num_two = Int(Items.Text)
  8.  
  9.     Total_results_num = num_one + num_two 'calculates amount with items
  10.     Total_results = Total_results_num
  11.  
  12.  
Give this one a whirl, see what pops up.

Good luck!
Mar 28 '07 #6
Dököll
2,364 Expert 2GB
Whoops! I found out the problem, it works now. Since it is a label, I must add .Caption to it, thus:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Total_Click()                        'total button calculator
  3.  
  4.     Dim num_one As Currency                 'see textbox named Price.Text
  5.     Dim num_two As Integer                    'see textbox named Price.Text
  6.     Dim Total_results_num As Currency   'see label named Total_results.Caption
  7.  
  8.     num_one = Int(Price.Text)
  9.     num_two = Int(Items.Text)
  10.     Total_results_num = num_one + num_two
  11.     Total_results.Caption = Total_results_num
  12.  
  13.  
  14. End Sub  
  15.  
  16.  
There, try this. You'll have to do most of the work, Hopefuly this gets you started right :-)
Mar 28 '07 #7
rents
26
Dokoll

Thanks for all the responses, but I still do not understand what is going on.
Like what exact code should I put in where?
Mar 28 '07 #8
rents
26
Private Sub Total_Click() 'total button calculator

Dim num_one As Currency 'see textbox named Price.Text
Dim num_two As Integer 'see textbox named Price.Text
Dim Total_results_num As Currency

ok..for this part..where it says Dim num_one As Currency..

what should I put in for mine..for my weight textbox (txtweight)

Dim decWeight As ???? (it's supposed to run in kg)
Mar 28 '07 #9
rents
26
ok, I've kind of figured out what I am doing. But now I've run into a problem.
I have these IF statements in my calculate button


If IsNumeric(txtWeight.Text) = False Then
lblErrorMessage.Text = "Weight must numeric"
lblErrorMessage.Visible = True
Return
End If

If IsNumeric(txtDistance.Text) = False Then
lblErrorMessage.Text = "Distance must be numeric"
lblErrorMessage.Visible = True
Return
End If

but when I test the Distance with some letter, the error label tells me "Weight must be numeric"

What am I doing wrong?
Mar 29 '07 #10
rents
26
Ok. It seems like everything is running ok. I'm still having trouble with the "numeric" problem as stated in the last post.

The final thing I need to do is this:

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. Use exception handling to check for non-numeric data.

Where would I put this in the code window? and like how would I type it in?
Mar 29 '07 #11
Dököll
2,364 Expert 2GB
Private Sub Total_Click() 'total button calculator

Dim num_one As Currency 'see textbox named Price.Text
Dim num_two As Integer 'see textbox named Price.Text
Dim Total_results_num As Currency

ok..for this part..where it says Dim num_one As Currency..

what should I put in for mine..for my weight textbox (txtweight)

Dim decWeight As ???? (it's supposed to run in kg)
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim decWeight As Integer     'you are adding numerics here
  3.  
  4.  
Mar 29 '07 #12
Dököll
2,364 Expert 2GB
ok, I've kind of figured out what I am doing. But now I've run into a problem.
I have these IF statements in my calculate button

Expand|Select|Wrap|Line Numbers
  1.  
  2.         If IsNumeric(txtWeight.Text) = False Then
  3.             lblErrorMessage.Text = "Weight must numeric"
  4.             lblErrorMessage.Visible = True
  5.             Return
  6.         End If
  7.  
  8.         If IsNumeric(txtDistance.Text) = False Then
  9.             lblErrorMessage.Text = "Distance must be numeric"
  10.             lblErrorMessage.Visible = True
  11.             Return
  12.         End If
  13.  
  14.  
but when I test the Distance with some letter, the error label tells me "Weight must be numeric"

What am I doing wrong?
You are doing fine. You have it set to find numerics, so it's a good catch that your message tells you the program needs numerics. Please continue, Great Job! Add a number in there, you will find you do not get the message therefore :-)
Mar 29 '07 #13
rents
26
Dokoll
Thanks so much for your help!
I'm still working on this last part, so if you could help me out with this one, that would be it :)



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. Use exception handling to check for non-numeric data.

Where would I put this in the code window? and like how would I type it in?
Mar 29 '07 #14
rents
26
'Check the weight
If (intWeight < 1) Or (intWeight > 20) Then
lblErrorMessage.Text = "Our weight range is 1-20 kgs"
lblErrorMessage.Visible = True
Return
End If

'Check the distance
If (decDistance < 10) Or (decDistance > 3000) Then
lblErrorMessage.Text = "Our distance range is 10-3000 mi"
lblErrorMessage.Visible = True
End If


this is what I've come up with for making sure the user enters number within my range.
But when I test it, only the "our weight range.." error message shows up. I can't get the distance error message to show up at all.
In fact even if I put a correct amount in for weight, it still gives me the error message about weight.
What am I doing wrong?
Mar 29 '07 #15
Dököll
2,364 Expert 2GB
'Check the weight
If (intWeight < 1) Or (intWeight > 20) Then
lblErrorMessage.Text = "Our weight range is 1-20 kgs"
lblErrorMessage.Visible = True
Return
End If

'Check the distance
If (decDistance < 10) Or (decDistance > 3000) Then
lblErrorMessage.Text = "Our distance range is 10-3000 mi"
lblErrorMessage.Visible = True
End If


this is what I've come up with for making sure the user enters number within my range.
But when I test it, only the "our weight range.." error message shows up. I can't get the distance error message to show up at all.
In fact even if I put a correct amount in for weight, it still gives me the error message about weight.
What am I doing wrong?
You're doing great, good of you to have patience. Try adding ElseIf into the mix. You may also need to get rid of Return. Give it a whirl with ElseIf first, then delete return if ElseIf alone does not work:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         If (intWeight < 1) Or (intWeight > 20) Then
  3.             lblErrorMessage.Text = "Our weight range is 1-20 kgs"
  4.             lblErrorMessage.Visible = True
  5.             Return
  6.         ElseIf (decDistance < 10) Or (decDistance > 3000) Then
  7.             lblErrorMessage.Text = "Our distance range is 10-3000 mi"
  8.             lblErrorMessage.Visible = True
  9.         End If
  10.  
  11.  
Will test it here as well...
Mar 29 '07 #16
rents
26
ok I re-did the code and stuck the validation in the appropriate boxes
check it out

Private Sub txtWeight_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtWeight.Validating


'Validate the weight range

If IsNumeric(txtWeight.Text) Then
Dim intWeight As Integer
intWeight = CInt(txtWeight.Text)
If intWeight < 1 Or intWeight > 20 Then
lblErrorMessage.Text = "Our weight range is 1-20 kg"
e.Cancel = True
Else
e.Cancel = False
End If
Else
lblErrorMessage.Text = "Weight must be numeric"
e.Cancel = True
End If


End Sub

Private Sub txtDistance_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtDistance.Validating


'Validate the Distance range

If IsNumeric(txtDistance.Text) Then
Dim decDistance As Decimal
decDistance = CDec(txtDistance.Text)
If decDistance < 10 Or decDistance > 3000 Then
lblErrorMessage.Text = "Our distance range is 10-3000 mi"
e.Cancel = True
Else
e.Cancel = False
End If
Else
lblErrorMessage.Text = "The distance must be numeric"
e.Cancel = True
End If


End Sub
End Class




but now, if you put in an letter or go out of the range, you cannot clear anything with the clear button and you cannot exit the form at all. I had to do ctr-alt-del to get rid of it.
Any ideas as to what may have happened???
Mar 30 '07 #17
iburyak
1,017 Expert 512MB
Guys sorry to interrapt.


I would put folloing code in change event instead:

[PHP]Private Sub txtWeight_Change()
If Trim(txtWeight.Text) = "" Then Exit Sub

If IsNumeric(txtWeight.Text) And (Val(Trim(txtWeight.Text)) > 0 And Val(Trim(txtWeight.Text)) < 21) Then
lblErrorMessage.Caption = ""
Else
lblErrorMessage.Caption "Weight must be numeric and between 1 and 20 kg", vbExclamation
txtWeight.Text = ""
txtWeight.SetFocus
End If

End Sub[/PHP]

But better instead of a label i would use a message box. People sometimes don't look at labels and can miss written warning. Message box is more obviouse in this case.

[PHP]Private Sub txtWeight_Change()

If Trim(txtWeight.Text) = "" Then Exit Sub

If IsNumeric(txtWeight.Text) And (Val(Trim(txtWeight.Text)) > 0 And Val(Trim(txtWeight.Text)) < 21) Then

Else
MsgBox "Weight must be numeric and between 1 and 20 kg", vbExclamation
txtWeight.Text = ""
txtWeight.SetFocus
End If

End Sub[/PHP]
Mar 30 '07 #18
rents
26
just in case you wanted to know, I ended up with a 95 out of 100 on this project
*phew*!!
thanks very much for all your help.
keep your eyes peeled, I have another project due at the end of the week :)
Apr 15 '07 #19
Dököll
2,364 Expert 2GB
just in case you wanted to know, I ended up with a 95 out of 100 on this project
*phew*!!
thanks very much for all your help.
keep your eyes peeled, I have another project due at the end of the week :)
Fantastic, good going...

As long as you post your work, your efforts, and are willing do some of the work, you should get sound, solid help...95 eh!, That's gold babee...
Apr 16 '07 #20

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

Similar topics

0
by: Brian Huether | last post by:
I know there are lots of shopping carts out there (ZenCart, etc). But so far none of them seem to have a good built in shipping calculator with various options. Can anyone recommend something? And...
1
by: Jason Burr | last post by:
Has anybody used the UPS tools for calculating shipping and if so how did they go about it? I have seen the articles on 4guysfromrolla but that appears to be using xml component to get the data...
1
by: Tim | last post by:
Anyone know of any good shipping components for adding to a custom built shopping cart. Or if you develop a shipping piece, is it best to ship by weight or by item meaning that each item will cost...
0
by: McKirahan | last post by:
I am developing a shopping cart application and would like to determine (based on weight, dimensions, and destination) the actual shipping charges that USPS, UPS, FedEx, DHL, and/or Airborne would...
10
by: collegestudent | last post by:
hello there, I was wondering if anyone could give me some advice on where to start on a project for college where I have to design and create a hand-held calculator simulation in Visual...
6
by: Chris Buckett | last post by:
Hi, I was just wondering if anyone has had any experience in developing a shipping matrix. We are based in the UK, and we need to ship both locally and internationally. Local shipping is...
4
by: tudyfruity18 | last post by:
I'm suppose to write an applet that contains two buttons Investment calculator and Loan Calculator. When the Investment Calculator button is clicked, a frame appears in a new window for calculating...
4
by: aprillynn82 | last post by:
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...
1
AaronL
by: AaronL | last post by:
Hello, First I would like to say thank you all for your help in the past. I am stumped again. I am creating an e-commerce system and I want to be able to upload images to the server and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.