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
- 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.
Create the following methods:
CalcStayCharges Calculates and returns the base charges for the hospital
stay. The is compound 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.
Input Validation: Do not accept invalid value for length of stay,
medication charges, surgical charges, lab fees, or
rehab charges.
There will be 5 text boxes: Length of Stay (Days), Medication, Surgical
Charges, Lab Fees and rehab. A label should contain the Total Cost.
There will be two buttons: Calculate Charges and Close
**I need help writing the correct code so this problem executes properly. Thanks!**
This is what I have so far. I am lost. Please help
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Hospital_Charges
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// These methods accept the number of days at the hospital
// and medical charges as arguments and return the
// equivalent total of charges.
private double CalcStayCharges(double days)
{
return days * 350.0;
}
private double CalcMiscCharges(double medical, double surgical, double lab, double rehab)
{
return (medical+ surgical + lab + rehab);
}
private void calculateButton_Click(object sender, EventArgs e)
{
// Variables to hold the number of days spent at the hospital, amount of medication, surgical, lab and rehab charges.
double days, staytotal, medical, surgical, lab, rehab, totalCharges;
// Get the number of days.
if (double.TryParse(daysTextBox.Text, out days))
{
// Calculate the stay charge.
staytotal = CalcStayCharges(days);
// Display the stay charge.
totalStayLabel.Text = staytotal.ToString("n1");
// Calculate the other services.
totalCharges = CalcMiscCharges(medical, surgical, lab, rehab);
}
else
{
// Display an error message.
MessageBox.Show("Enter a valid number.");
}
}
private void exitButton_Click(object sender, EventArgs e)
{
// Close the form.
this.Close();
}
}
}
Loading

VulpesPosted Nov 18, 2013, 5:57 AM
VulpesPosted Mar 6, 2015, 6:37 PM
Justin WashburnPosted Mar 6, 2015, 4:01 PM
Tiago LeaoPosted Nov 25, 2013, 8:15 PM
It works now and the final code looks like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Hospital_Charges
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// These methods accept the number of days at the hospital
// and medical charges as arguments and return the
// equivalent total of charges.
private double CalcStayCharges(double days)
{
return days * 350.0;
}
private double CalcMiscCharges(double medical, double surgical, double lab, double rehab)
{
return (medical + surgical + lab + rehab);
}
private double CalcTotalCharges(double days, double medical, double surgical, double lab, double rehab)
{
return CalcStayCharges(days) + CalcMiscCharges(medical, surgical, lab, rehab);
}
private void calculateButton_Click(object sender, EventArgs e)
{
// Variables to hold the number of days spent at the hospital, amount of medication, surgical, lab and rehab charges.
double days, stayTotal, medical, surgical, lab, rehab, totalFees, totalCharges;
// Get the number of days.
if (!double.TryParse(daysTextBox.Text, out days) || days <= 0.0)
{
MessageBox.Show("Length of stay (days) is invalid. Please enter a valid number");
daysTextBox.Focus();
return;
}
// Get the medication charges
if (!double.TryParse(medicationTextBox.Text, out medical) || medical < 0.0)
{
MessageBox.Show("Medication charges are invalid. Please enter a valid number");
medicationTextBox.Focus();
return;
}
// Get the surgical charges
if (!double.TryParse(surgicalTextBox.Text, out surgical) || surgical < 0.0)
{
MessageBox.Show("Surgical charges are invalid. Please enter a valid number");
surgicalTextBox.Focus();
return;
}
// Get the lab fees
if (!double.TryParse(labTextBox.Text, out lab) || lab < 0.0)
{
MessageBox.Show("Lab fees are invalid. Please enter a valid number");
labTextBox.Focus();
return;
}
// Get the physical rehabilitation charges
if (!double.TryParse(rehabTextBox.Text, out rehab) || rehab < 0.0)
{
MessageBox.Show("Phycial rehabilitation charges are invalid. Please enter a valid number");
rehabTextBox.Focus();
return;
}
stayTotal = CalcStayCharges(days);
totalStayLabel.Text = stayTotal.ToString("n2");
// Calculate the total charges.
totalCharges = CalcTotalCharges(days, medical, surgical, lab, rehab);
// Display the total charges.
totalLabel.Text = totalCharges.ToString("n2");
// Calculate the other fees.
totalFees = CalcMiscCharges(medical, surgical, lab, rehab);
// Display the total Fees.
miscLabel.Text = totalFees.ToString("n2");
// Display the amount for medical
listBox1.Items.Add("Medication $ " + medical);
// Display the amount for surgical
listBox1.Items.Add("Surgical $ " + surgical);
// Display the amount for lad fees
listBox1.Items.Add("Lab fees $ " + lab);
// Display the amount for rehab
listBox1.Items.Add("Rehab $ " + rehab);
}
private void exitButton_Click(object sender, EventArgs e)
{
// Close this form.
this.Close();
}
}
}
VulpesPosted Nov 19, 2013, 4:45 AM
Tiago LeaoPosted Nov 18, 2013, 8:11 PM
I added a list box cuz i need to show the medication, surgical, lab and rehab fee there.
so that would look lke:
Medication $1
Surgical $1
Lab $1
Rehab $1
also I called the MiscCharge method so I could display it in one textlabel.
1. Why do you use "n2" in the code instead of 'n1'
2.The only problem is I cant display the result for the stay in one label. Look at the pic and see, the stay charges label isnt showing the result:
the code now looks like:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Hospital_Charges
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// These methods accept the number of days at the hospital
// and medical charges as arguments and return the
// equivalent total of charges.
private double CalcStayCharges(double days)
{
return days * 350.0;
}
private double CalcMiscCharges(double medical, double surgical, double lab, double rehab)
{
return (medical + surgical + lab + rehab);
}
private double CalcTotalCharges(double days, double medical, double surgical, double lab, double rehab)
{
return CalcStayCharges(days) + CalcMiscCharges(medical, surgical, lab, rehab);
}
private void calculateButton_Click(object sender, EventArgs e)
{
// Variables to hold the number of days spent at the hospital, amount of medication, surgical, lab and rehab charges.
double days, stayTotal, medical, surgical, lab, rehab, totalFees, totalCharges;
// Get the number of days.
if (!double.TryParse(daysTextBox.Text, out days) || days <= 0.0)
{
MessageBox.Show("Length of stay (days) is invalid. Please enter a valid number");
daysTextBox.Focus();
return;
}
// Get the medication charges
if (!double.TryParse(medicationTextBox.Text, out medical) || medical < 0.0)
{
MessageBox.Show("Medication charges are invalid. Please enter a valid number");
medicationTextBox.Focus();
return;
}
// Get the surgical charges
if (!double.TryParse(surgicalTextBox.Text, out surgical) || surgical < 0.0)
{
MessageBox.Show("Surgical charges are invalid. Please enter a valid number");
surgicalTextBox.Focus();
return;
}
// Get the lab fees
if (!double.TryParse(labTextBox.Text, out lab) || lab < 0.0)
{
MessageBox.Show("Lab fees are invalid. Please enter a valid number");
labTextBox.Focus();
return;
}
// Get the physical rehabilitation charges
if (!double.TryParse(rehabTextBox.Text, out rehab) || rehab < 0.0)
{
MessageBox.Show("Phycial rehabilitation charges are invalid. Please enter a valid number");
rehabTextBox.Focus();
return;
}
// Display the stay charge.
totalStayLabel.Text = stayTotal.ToString("n2");
(Why isnt this working)
// Calculate the total charges.
totalCharges = CalcTotalCharges(days, medical, surgical, lab, rehab);
// Display the total charges.
totalLabel.Text = totalCharges.ToString("n2");
// Calculate the other fees.
totalFees = CalcMiscCharges(medical, surgical, lab, rehab);
// Display the total Fees.
miscLabel.Text = totalFees.ToString("n2");
// Display the amount for medical
listBox1.Items.Add("Medication $ " + medical);
// Display the amount for surgical
listBox1.Items.Add("Surgical $ " + surgical);
// Display the amount for lad fees
listBox1.Items.Add("Lab fees $ " + lab);
// Display the amount for rehab
listBox1.Items.Add("Rehab $ " + rehab);
}
private void exitButton_Click(object sender, EventArgs e)
{
// Close this form.
this.Close();
}
}
}