Option Strict On
Option Explicit On
Public Class Form1
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateCharges.Click
'declare all variables
Dim decCharge As Decimal
Dim intLengthOfStay As Integer
Dim decMedCharges As Decimal
Dim decSurgicalCharge As Decimal
Dim decDayRate As Decimal
Dim decMiscCharge As Decimal
Dim decSurgicalCharges As Decimal
Dim decTotalCharge As Decimal
Dim decMeds As Decimal
Dim decLabFee As Decimal
If isValid(decMeds, decSurgicalCharge, intLengthOfStay, decLabFee) = True Then
decCharge = CalcStayCharge(intLengthOfStay, decDayRate)
decMiscCharge = CalcMiscCharges(decMedCharges, decSurgicalCharges, decLabFee)
decTotalCharge = CalcTotalCharge(decMiscCharge, decCharge)
txtTotalCost.Text = decTotalCharge.ToString("c2")
End If
End Sub
Private Function isValid(ByRef decMeds As Decimal, ByRef decSurgeicalCharge As Decimal, _
ByRef intLengthOfStay As Integer, ByRef decLabFee As Decimal) As Boolean
Return isValidStay(intLengthOfStay) AndAlso isValidMedication(decMeds) _
AndAlso isValidSurgical(decSurgeicalCharge) AndAlso CBool(isValidLabFee(decLabFee))
Return isValid
End Function
Private Function isValidStay(ByRef intLengthOfStay As Integer) As Boolean
'Declare Boolean
Dim isGood As Boolean
'Declare Constants
Const c_DaysError As String = "Length of stay must be a whole number between 0 and 100"
Try
intLengthOfStay = Convert.ToInt32(txtLengthOfDays.Text)
If intLengthOfStay >= 0 And intLengthOfStay <= 100 Then
isGood = True
Else
isGood = False
End If
Catch ex As Exception
isGood = False
End Try
If isGood = False Then
MessageBox.Show(c_DaysError)
txtLengthOfDays.SelectAll()
txtLengthOfDays.Focus()
End If
Return isGood
End Function
Public Function isValidLabFee(ByVal decLabFee As Decimal) As Decimal
'Declare Variable
Dim isGood As Boolean
'Declare Constant
Const c_LabFeeError As String = "Medication must be between 0 and 10,000"
Try
decLabFee = Convert.ToDecimal(txtLabFees.Text)
If decLabFee >= 0D And decLabFee <= 10000D Then
isGood = True
Else
isGood = False
End If
Catch ex As Exception
isGood = False
MessageBox.Show(c_LabFeeError)
txtLabFees.SelectAll()
txtLabFees.Focus()
End Try
Return CDec(isGood)
End Function
Public Function isValidMedication(ByVal decMedCharges As Decimal) As Boolean
'Declare Variable
Dim isGood As Boolean
'Declare Constant
Const c_MedicationError As String = "Medication must be between 0 and 10,000"
Try
decMedCharges = Convert.ToDecimal(txtMedicationCharges.Text)
If decMedCharges >= 0D And decMedCharges <= 10000D Then
isGood = True
Else
isGood = False
End If
Catch ex As Exception
isGood = False
MessageBox.Show(c_MedicationError)
txtMedicationCharges.SelectAll()
txtMedicationCharges.Focus()
End Try
Return isGood
End Function
Public Function isValidSurgical(ByVal decSurgicalCharges As Decimal) _
As Boolean
'Declare Variables
Dim isGood As Boolean
'Declare constants
Const c_SurgicalError As String = "Surgical must be between 0 and 500,000"
Try
decSurgicalCharges = Convert.ToDecimal(txtSurgicalCharges.Text)
If decSurgicalCharges >= 0D And decSurgicalCharges <= 500000D Then
isGood = True
Else
isGood = False
End If
Catch ex As Exception
isGood = False
MessageBox.Show(c_SurgicalError)
txtSurgicalCharges.SelectAll()
txtSurgicalCharges.Focus()
End Try
Return isGood
End Function
Private Sub txtLabFees_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLabFees.TextChanged
End Sub
End Class
Module modTotalCharge
Public Function CalcTotalCharge(ByRef decMiscCharge As Decimal, ByRef decCharge As Decimal) _
As Decimal
Return decMiscCharge + decCharge
End Function
End Module
Module modMiscCharge
Function CalcMiscCharges(ByRef decMedCharges As Decimal, ByRef decSurgicalCharges As Decimal, ByRef decLabFee As Decimal) _
As Decimal
Return decMedCharges + decSurgicalCharges + decLabFee
End Function
End Module
Module modStayCharge
Public Function CalcStayCharge(ByVal intLengthofStay As Integer, ByVal decDayRate As Decimal) _
As Decimal
decDayRate = 350D
Return intLengthofStay * decDayRate
End Function
End Module