|
-
Jul 26th, 2005, 02:46 AM
#1
Thread Starter
New Member
Question about Decision structures and logic
I need to write a program to compute a monthly gas bill
basicily i am having a problem , writing the logic for the program
The gas companys schedule of fees is as follows:
0-80 Cubic metres $10.00 Minium
Next 120 CU metres $0.10 per Cu Meter
next 200 Cu metres $0.05 per CU Meter
400 and above $0.025 per Cu meter
So i am having a problem trying to figure out how to make the program charge According to that schedule of fees/
What decision structure should i use ? can any give me the basic set up ? Thanks
-
Jul 26th, 2005, 05:39 AM
#2
Re: Question about Decision structures and logic
You could use arrays or something to store the thresholds and charges but here is a simple example with those values hard-coded:
VB Code:
Private Function GetCost(ByVal quantity As Decimal) As Decimal
Const minCharge As Decimal = 10D
Const level1Charge As Decimal = 0.1D
Const level2Charge As Decimal = 0.05D
Const level3Charge As Decimal = 0.025D
Const level1Threshold As Decimal = 80D
Const level2Threshold As Decimal = 200D
Const level3Threshold As Decimal = 400D
Dim cost As Decimal = minCharge
If quantity > level1Threshold Then
cost += (Math.Min(quantity, level2Threshold) - level1Threshold) * level1Charge
End If
If quantity > level2Threshold Then
cost += (Math.Min(quantity, level3Threshold) - level2Threshold) * level2Charge
End If
If quantity > level3Threshold Then
cost += (quantity - level3Threshold) * level3Charge
End If
Return cost
End Function
-
Jul 26th, 2005, 12:23 PM
#3
Thread Starter
New Member
Re: Question about Decision structures and logic
jmcilhinney Thanks alot for the post.
Great info , that should work perfectly , Exactly what i was looking for.
Thanks for the code too ...\\
What exactly do the D's at the end of each assignment of the Constants do ? Decimal ?
And should there be a infront of the
VB Code:
(quantity - level3Threshold)
tag>?
-TaylorVBNET
Last edited by TaylorVBNET; Jul 26th, 2005 at 12:46 PM.
-
Jul 26th, 2005, 01:49 PM
#4
Thread Starter
New Member
Re: Question about Decision structures and logic
I used the method of hard coding the schedule of fees
The "gas Meter" only gives out a 4 digit reading
so this months gas meter reading could be less than the previous months
ex. lastmonth : 9200 this month : 1503
diffrence of 9999-9200=799
now add the 799 to 1503 to get the amount of gas used this month
Well i finsihed the code for this prog and no matter the numbers i get an in correct value of 0 for amount of gas used of total cost of gas.
Here is my code
[Highlight=VB]Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const decMinCharge As Decimal = 10D
Const decLevel1Charge As Decimal = 0.1D
Const decLevel2Charge As Decimal = 0.05D
Const decLevel3Charge As Decimal = 0.025D
Const decLevel1Threshold As Decimal = 80D
Const decLevel2Threshold As Decimal = 200D
Const decLevel3Threshold As Decimal = 400D
Const MaxMeter As Decimal = 9999D
Dim Cost As Decimal, decGasUsage As Decimal, decMeterThisMonth As Decimal, _
decMeterLastMonth As Decimal
If decMeterThisMonth < decMeterLastMonth Then
decGasUsage = (MaxMeter - decMeterLastMonth) + decMeterThisMonth
ElseIf decMeterThisMonth > decMeterLastMonth Then
decGasUsage = decMeterLastMonth - decMeterThisMonth
End If
If decGasUsage > decLevel1Threshold Then
Cost += (Math.Min(decGasUsage, decLevel2Threshold) - decLevel1Charge)
End If
If decGasUsage > decLevel2Threshold Then
Cost += (Math.Min(decGasUsage, decLevel3Threshold) - decLevel2Charge)
End If
If decGasUsage > decLevel3Threshold Then
Cost += (decGasUsage - decLevel3Threshold) * decLevel3Charge
End If
txtBill.Text = CStr(Format(Cost, "C"))
[Highlight=VB]
I have looked it over and over and cant seem to find the problem , anyone have any ideas ?
-
Jul 26th, 2005, 06:19 PM
#5
Re: Question about Decision structures and logic
To answer your questions, the "D" does mean that the numeric literal is a Decimal. Given that you are assigning it to a Decimal variable it is not really necessary but I like to include it anyway. You can force a numeric literal to various types using a suffix: S for Short, I for Integer (or % for non-numerics), L for Long, F for Single, R for Double, D for Decimal, C for Char, # for Date (before and after). As for the Math.Min, you don't need it for the last calculation because there is no upper limit to that charge level.
As for the code, I did whip it up fairly quickly so I won't make a 100% guarantee that it was perfect. I'll have a look at your adaptation and get back to you.
One more small thing. The Format function returns a string so there is no need to use CStr. I'd be inclined to call ToString("c") instead of Format also. There have been great debates raging on this and other forums with regards to the relative merits of VB Runtime functions. I have been an advocate for both sides at various times, but I generally like to use a method from a System-based namespace (like System.Decimal.ToString) if one is available over a VB Runtime function (like Microsoft.VisualBasic.Strings.Format), and I think you'll find that that is the general feeling around here.
-
Jul 26th, 2005, 06:21 PM
#6
Re: Question about Decision structures and logic
OK. Just looked at your code and it looks like you never set the meter values, so your usage is always going to be zero.
Edit:
In situations like this you should set a break point at the start of the method and step through the code line by line. You can use the Autos, Locals and Watch windows to check the values of your variables, which would have quickly pointed to the issue.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|