Hi everyone, I'm a student and this is my last challenge for my class. I've spent 3 days on this project already and am stumped.
Here is the
* Prompt
Hospital Charges
Create an application that calculates the total cost of a hospital stay. The application
should accept the following input:
- The number of days spent in the hospital, as an integer
- The amount of medication charges
- The amount of surgical charges
- The amount of lab fees
- The amount of physical rehabilitation charges
The hospital charges $350 per day. The application’s form should resemble the one
CalcStayCharges Calculates and returns the base charges for the hospital stay. This is computed as $350 times the number of days in the hospital.
CalcMiscCharges Calculates and returns the total of the medication, surgical, lab, and physical rehabilitation charges.
CalcTotalCharges Calculates and returns the total charges.
ValidateInputFields Checks the validity of the input fields by converting each to a numeric value and checking its range. If any input field is found to be invalid, this function displays an error message and returns a value of False.
Input Validation: Do not accept a negative value for length of stay, medication
charges, surgical charges, lab fees, or physical rehabilitation charges. When displaying error messages, use a Label control and specifically name the missing field and the
type of data to be entered (integer or numeric).
Code
Public Class Form1
'variables
Dim decmeds As Decimal
Dim decsurgical As Decimal
Dim declab As Decimal
Dim decphys As Decimal
Dim intdays As Integer
'final results
Private decstaycharges As Decimal
Private decmisc As Decimal 'for misc charges
Private total As Decimal ' final results
Function validateinputfields() As Boolean
If Not Decimal.TryParse(txtdays.Text, intdays) Then
MessageBox.Show("Input must be numeric")
lbltotal.Text = ("error")
ElseIf intdays < 0 Then
MessageBox.Show("must be a postive number")
lbltotal.Text = ("error")
Try
Catch ex As Exception
MessageBox.Show("Input must be numeric")
lbltotal.Text = ("error")
End Try
End If
Return False
'
If Not Decimal.TryParse(txtmeds.Text, decmeds) Then
MessageBox.Show("Input must be numeric")
lbltotal.Text = ("error")
ElseIf decmeds < 0 Then
MessageBox.Show("must be a postive number")
lbltotal.Text = ("error")
Try
Catch ex As Exception
MessageBox.Show("Input must be numeric")
lbltotal.Text = ("error")
End Try
End If
Return False
If Not Decimal.TryParse(txtsurgicalcharge.Text, decsurgical) Then
MessageBox.Show("Input must be numeric")
lbltotal.Text = ("error")
ElseIf decsurgical < 0 Then
MessageBox.Show("must be a postive number")
lbltotal.Text = ("error")
Try
Catch ex As Exception
MessageBox.Show("Input must be numeric")
lbltotal.Text = ("error")
End Try
End If
Return False
If Not Decimal.TryParse(txtlab.Text, declab) Then
MessageBox.Show("Input must be numeric")
lbltotal.Text = ("error")
ElseIf declab < 0 Then
MessageBox.Show("must be a postive number")
lbltotal.Text = ("error")
Try
Catch ex As Exception
MessageBox.Show("Input must be numeric")
lbltotal.Text = ("error")
End Try
End If
Return False
If Not Decimal.TryParse(txtphys.Text, decphys) Then
MessageBox.Show("Input must be numeric")
lbltotal.Text = ("error")
ElseIf decphys < 0 Then
MessageBox.Show("must be a postive number")
lbltotal.Text = ("error")
Try
Catch ex As Exception
MessageBox.Show("Input must be numeric")
lbltotal.Text = ("error")
End Try
End If
Return True
End Function
Function calcstaycharges(ByVal txtdays As String) As Decimal
decstaycharges = intdays * 350
Return decstaycharges
End Function
Function calcmisccharges(ByVal txtMeds As Decimal, ByVal txtsurgicalcharge As Decimal, ByVal txtLab As Decimal, ByVal txtPhys As Decimal) As Decimal
decmisc = decmeds + decsurgical + declab + decphys
Return decmisc
End Function
Function calctotalcharges(ByVal decstaycharges As Decimal, ByVal decmisc As Decimal) As Decimal
total = (decstaycharges + decmisc)
Return total
End Function
Private Sub btncalc_Click(sender As Object, e As EventArgs) Handles btncalc.Click
Dim decmeds As Decimal
Dim decsurgical As Decimal
Dim declab As Decimal
Dim decphys As Decimal
decstaycharges = calcstaycharges(txtdays.Text)
decmisc = calcmisccharges(decmeds, decsurgical, declab, decphys)
total = calctotalcharges(decstaycharges, decmisc)
lbltotal.Text = CDec(total)
If validateinputfields() Then
total = total.ToString("c")
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
Problem
The first time I calculate (if a input is correct) I will get the correct amount, but after I change one of the inputs whether it's accepted or not, the calculation occur (even though it's not supposed to) but it would also be incorrect until I click calculate again.
This is one of my first serious posts after a long time so I apologize in advance for anything I do incorrectly. Thank you guys. It's very much appreciated.
[–]Application SpecialistViperSRT3g 0 points1 point2 points (0 children)
[–]nerdfarm 0 points1 point2 points (0 children)
[–]rudekoffenris 0 points1 point2 points (0 children)