
07-10-2012, 08:53 AM
|
 |
Fabulous Florist
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,419
|
|
Ahh, that's a little different, but if you think about it by analogy it's evident how to solve it.
Imagine you're at a store and there's a sale on soda. Cans are $0.50 per can with a limit of 12. If you buy more than 12, the cost is the regular price of $1.25. This means that price is a function of number of cans purchased.
Code:
' (Let's ignore that Doubles are a bad idea for money.)
Function CostOfCan(ByVal numberPurchased As Integer) As Double
If numberPurchased <= 12 Then
Return 0.5
Else
Return 1.25
End If
Now, you could do some fancy things with clamping functions and do the whole calculation in one swoop, but there's two other ways to go about it: - For each can, ask CostOfCan() what this one costs and add it to a running total.
- Don't have CostOfCan(). Instead, pick up to 6 cans and add that number * 0.5 to the total. Then multiply the rest by 1.25 and add it to the total.
In my opinion, option 1 is nicer, especially as more prices and conditions to meet them are added. It'd look something like this:
Code:
Dim total As Double = 0
For i As Integer = 1 To numberOfCans
total += CostOfCan(i)
Next
|
|