The Problem:
The preparation of the monthly water bill is doing according to the following measures.
Price for first 10 units = 5.00
Price for units 11-20 = 10.00
Price for units 21-30 = 18.00
Price for units 31-40 = 20.00
Price for units 41 onwards = 25.00
Tax (not effected to the monthly rental) = 10%
Monthly rental = 100.00
As an example, the calculation for the total monthly usage of 25 units can be done as follows.
(10*5) + (10*10) + (18*5) + 100 = 340.00
You are asked to develop an application for doing above calculation processes.
Generally we have a designed interface with an input field, a button control and a label control for display output in a windows form. On this demonstration I want to show you how many coding methods exists to solve the same problem. 🙂 To understand below coding blogs you should have some basic knowledge in VB programming such as if condition, case statement, loops, functions and windows forms designing.
Solution 1: (it has used a function to do the calculation)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | Public Class Form1 Dim intUnits As Integer Dim range1 As Double = 5 'Price for units 0-10 Dim range2 As Double = 10 'Price for units 11-20 Dim range3 As Double = 18 'Price for units 21-30 Dim range4 As Double = 20 'Price for units 31-40 Dim range5 As Double = 25 'Price for units 41 onwards Dim tax As Double = 10 'tax as percentage Dim rental As Double = 100 'monthly rental Dim basic, totalpayment As Double Public Function getbasic(ByVal units As Integer) As Double If units < 10 Then basic = units * range1 Return basic Else units -= 10 End If If units < 10 Then basic = (10 * range1) + (units * range2) Return basic Else units -= 10 End If If units < 10 Then basic = (10 * range1) + (10 * range2) + (units * range3) Return basic Else units -= 10 End If If units < 10 Then basic = (10 * range1) + (10 * range2) + (10 * range3) + (units * range4) Return basic Else units -= 10 End If basic = (10 * range1) + (10 * range2) + (10 * range3) + (10 * range4) + (units * range5) Return basic End Function Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click basic = getbasic(Val(txtInput.Text)) basic += basic * (tax / 100) totalpayment = basic + rental lblOutput.Text = totalpayment End Sub End Class |
Public Class Form1 Dim intUnits As Integer Dim range1 As Double = 5 'Price for units 0-10 Dim range2 As Double = 10 'Price for units 11-20 Dim range3 As Double = 18 'Price for units 21-30 Dim range4 As Double = 20 'Price for units 31-40 Dim range5 As Double = 25 'Price for units 41 onwards Dim tax As Double = 10 'tax as percentage Dim rental As Double = 100 'monthly rental Dim basic, totalpayment As Double Public Function getbasic(ByVal units As Integer) As Double If units < 10 Then basic = units * range1 Return basic Else units -= 10 End If If units < 10 Then basic = (10 * range1) + (units * range2) Return basic Else units -= 10 End If If units < 10 Then basic = (10 * range1) + (10 * range2) + (units * range3) Return basic Else units -= 10 End If If units < 10 Then basic = (10 * range1) + (10 * range2) + (10 * range3) + (units * range4) Return basic Else units -= 10 End If basic = (10 * range1) + (10 * range2) + (10 * range3) + (10 * range4) + (units * range5) Return basic End Function Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click basic = getbasic(Val(txtInput.Text)) basic += basic * (tax / 100) totalpayment = basic + rental lblOutput.Text = totalpayment End Sub End Class
Solution 2: (using the “if” statement) Please note that it’s been skipped from posting the variable declarations again and again in below code blocks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Public Class Form1 Private Sub btnCalc2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc2.Click intUnits = Val(txtInput.Text) If intUnits <= 10 Then basic = intUnits * range1 ElseIf intUnits <= 20 Then basic = (10 * range1) + ((intUnits - 10) * range2) ElseIf intUnits <= 30 Then basic = (10 * range1) + (10 * range2) + ((intUnits - 20) * range3) ElseIf intUnits <= 40 Then basic = (10 * range1) + (10 * range2) + (10 * range3) + ((intUnits - 30) * range4) Else basic = (10 * range1) + (10 * range2) + (10 * range3) + (10 * range4) + ((intUnits - 40) * range5) End If basic += basic * (tax / 100) totalpayment = basic + rental lblOutput.Text = totalpayment End Sub End Class |
Public Class Form1 Private Sub btnCalc2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc2.Click intUnits = Val(txtInput.Text) If intUnits <= 10 Then basic = intUnits * range1 ElseIf intUnits <= 20 Then basic = (10 * range1) + ((intUnits - 10) * range2) ElseIf intUnits <= 30 Then basic = (10 * range1) + (10 * range2) + ((intUnits - 20) * range3) ElseIf intUnits <= 40 Then basic = (10 * range1) + (10 * range2) + (10 * range3) + ((intUnits - 30) * range4) Else basic = (10 * range1) + (10 * range2) + (10 * range3) + (10 * range4) + ((intUnits - 40) * range5) End If basic += basic * (tax / 100) totalpayment = basic + rental lblOutput.Text = totalpayment End Sub End Class
Solution 3: (using the “case” statement).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Public Class Form1 Private Sub btnCalc3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc3.Click intUnits = Val(txtInput.Text) Select Case intUnits Case 0 To 10 basic = intUnits * range1 Case 11 To 20 basic = (10 * range1) + ((intUnits - 10) * range2) Case 21 To 30 basic = (10 * range1) + (10 * range2) + ((intUnits - 20) * range3) Case 31 To 40 basic = (10 * range1) + (10 * range2) + (10 * range3) + ((intUnits - 30) * range4) Case Else basic = (10 * range1) + (10 * range2) + (10 * range3) + (10 * range4) + ((intUnits - 40) * range5) End Select basic += basic * (tax / 100) totalpayment = basic + rental lblOutput.Text = totalpayment End Sub End Class |
Public Class Form1 Private Sub btnCalc3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc3.Click intUnits = Val(txtInput.Text) Select Case intUnits Case 0 To 10 basic = intUnits * range1 Case 11 To 20 basic = (10 * range1) + ((intUnits - 10) * range2) Case 21 To 30 basic = (10 * range1) + (10 * range2) + ((intUnits - 20) * range3) Case 31 To 40 basic = (10 * range1) + (10 * range2) + (10 * range3) + ((intUnits - 30) * range4) Case Else basic = (10 * range1) + (10 * range2) + (10 * range3) + (10 * range4) + ((intUnits - 40) * range5) End Select basic += basic * (tax / 100) totalpayment = basic + rental lblOutput.Text = totalpayment End Sub End Class
Solution 4: (by using a “loop”)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | Public Class Form1 Private Sub btnCalc4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc4.Click intUnits = Val(txtInput.Text) basic = 0 'it must reset before start Dim costperunit As Double For intUnits = 1 To intUnits If intUnits <= 10 Then costperunit = range1 ElseIf intUnits <= 20 Then costperunit = range2 ElseIf intUnits <= 30 Then costperunit = range3 ElseIf intUnits <= 40 Then costperunit = range4 Else costperunit = range5 End If basic += costperunit Next basic += basic * (tax / 100) totalpayment = basic + rental lblOutput.Text = totalpayment End Sub End Class |
Public Class Form1 Private Sub btnCalc4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc4.Click intUnits = Val(txtInput.Text) basic = 0 'it must reset before start Dim costperunit As Double For intUnits = 1 To intUnits If intUnits <= 10 Then costperunit = range1 ElseIf intUnits <= 20 Then costperunit = range2 ElseIf intUnits <= 30 Then costperunit = range3 ElseIf intUnits <= 40 Then costperunit = range4 Else costperunit = range5 End If basic += costperunit Next basic += basic * (tax / 100) totalpayment = basic + rental lblOutput.Text = totalpayment End Sub End Class
Solution 5: (it has used “if condition” in a different method than previous one)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | Public Class Form1 Private Sub btnCalc5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc5.Click Dim modulus As Integer Dim range As Integer intUnits = Val(txtInput.Text) range = intUnits \ 10 modulus = intUnits Mod 10 If range = 0 Then basic = intUnits * range1 End If If range = 1 Then basic = 10 * range1 basic += modulus * range2 End If If range = 2 Then basic = 10 * range1 basic += 10 * range2 basic += modulus * range3 End If If range = 3 Then basic = 10 * range1 basic += 10 * range2 basic += 10 * range3 basic += modulus * range4 End If If range > 4 Then basic = 10 * range1 basic += 10 * range2 basic += 10 * range3 basic += 10 * range4 basic += (intUnits - 40) * range5 End If basic += basic * (tax / 100) totalpayment = basic + rental lblOutput.Text = totalpayment End Sub End Class |
Public Class Form1 Private Sub btnCalc5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc5.Click Dim modulus As Integer Dim range As Integer intUnits = Val(txtInput.Text) range = intUnits \ 10 modulus = intUnits Mod 10 If range = 0 Then basic = intUnits * range1 End If If range = 1 Then basic = 10 * range1 basic += modulus * range2 End If If range = 2 Then basic = 10 * range1 basic += 10 * range2 basic += modulus * range3 End If If range = 3 Then basic = 10 * range1 basic += 10 * range2 basic += 10 * range3 basic += modulus * range4 End If If range > 4 Then basic = 10 * range1 basic += 10 * range2 basic += 10 * range3 basic += 10 * range4 basic += (intUnits - 40) * range5 End If basic += basic * (tax / 100) totalpayment = basic + rental lblOutput.Text = totalpayment End Sub End Class
After the implementation of all above methods on one single windows form it is looks as below. Download this application with it’s source code from here: BillCalc Source Code (requires .NET 2.0 framework)