Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C#- Create an application that calculates the total cost of a hospital stay. The

ID: 3684594 • Letter: C

Question

C#- Create an application that calculates the total cost of a hospital stay. The daily base charge is $350. The hospital also charges for medication, surgical fees, lab fees, and physical rehab. 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 Create and use the following value-returning methods: -CalcStayCharges: Calculates and returns the base charges for the hospital stay. The 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.

Explanation / Answer

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Problem_6
{
    public partial class Form1 : Form
    {
        const decimal STAY_RATE = 350m;

        public Form1()
        {
            InitializeComponent();
        }

        private bool InputValidation(ref int daysSpent, ref decimal medicationCharges, ref decimal surgicalCharges, ref decimal labFees, ref decimal rehabCharges)
        {
            if(int.TryParse(daysSpentTextBox.Text, out daysSpent))
            {
                if(daysSpent < 1)
                {
                    MessageBox.Show("Days must be at least 1.");
                    daysSpentTextBox.Focus();
                    return false;
                }
            }

            if(decimal.TryParse(medicationChargesTextBox.Text, out medicationCharges))
            {
                if(medicationCharges < 0.00m)
                {
                    MessageBox.Show("Medication Charges must be at least $0.00.");
                    medicationChargesTextBox.Focus();
                    return false;
                }
            }

            if(decimal.TryParse(surgicalChargesTextBox.Text, out surgicalCharges))
            {
                if(surgicalCharges < 0.00m)
                {
                    MessageBox.Show("Surgical Charges must be at least $0.00.");
                    surgicalChargesTextBox.Focus();
                    return false;
                }
            }

            if(decimal.TryParse(labFeesTextBox.Text, out labFees))
            {
                if(labFees < 0.00m)
                {
                    MessageBox.Show("Lab Fees must be at least $0.00.");
                    labFeesTextBox.Focus();
                    return false;
                }
            }

            if(decimal.TryParse(rehabChargesTextBox.Text, out rehabCharges))
            {
                if(rehabCharges < 0.00m)
                {
                    MessageBox.Show("Rehab Charges must be at least $0.00");
                    rehabChargesTextBox.Focus();
                    return false;
                }
            }

            return true;
        }

        private decimal CalcStayCharges(int daysSpent)
        {
            return ((decimal)daysSpent * STAY_RATE);
        }

        private decimal CalcMiscCharges(decimal medicationCharges, decimal surgicalCharges, decimal labFees, decimal rehabCharges)
        {
            return (medicationCharges + surgicalCharges + labFees + rehabCharges);
        }

        private decimal CalcTotalCharges(decimal stayCharges, decimal miscCharges)
        {
            return (stayCharges + miscCharges);
        }

        private void calculateButton_Click(object sender, EventArgs e)
        {
            int daysSpent = 0;

            decimal medicationCharges = 0m;
            decimal surgicalCharges = 0m;
            decimal labFees = 0m;
            decimal rehabCharges = 0m;
            decimal stayCharges = 0m;
            decimal miscCharges = 0m;
            decimal totalCharges = 0m;

            if(InputValidation(ref daysSpent, ref medicationCharges, ref surgicalCharges, ref labFees, ref rehabCharges))
            {
                stayCharges = CalcStayCharges(daysSpent);
                miscCharges = CalcMiscCharges(medicationCharges, surgicalCharges, labFees, rehabCharges);
                totalCharges = CalcTotalCharges(stayCharges, miscCharges);

                clearButton.Focus();
            }

            stayChargesLabel.Text = stayCharges.ToString("c");
            miscChargesLabel.Text = miscCharges.ToString("c");
            totalChargesLabel.Text = totalCharges.ToString("c");
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            daysSpentTextBox.Text = null;
            medicationChargesTextBox.Text = null;
            surgicalChargesTextBox.Text = null;
            labFeesTextBox.Text = null;
            rehabChargesTextBox.Text = null;
            stayChargesLabel.Text = null;
            miscChargesLabel.Text = null;
            totalChargesLabel.Text = null;

            daysSpentTextBox.Focus();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Problem_6
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.Designer.cs
namespace Problem_6
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.daysSpentTextBox = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.medicationChargesTextBox = new System.Windows.Forms.TextBox();
            this.surgicalChargesTextBox = new System.Windows.Forms.TextBox();
            this.rehabChargesTextBox = new System.Windows.Forms.TextBox();
            this.labFeesTextBox = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.stayChargesLabel = new System.Windows.Forms.Label();
            this.miscChargesLabel = new System.Windows.Forms.Label();
            this.label9 = new System.Windows.Forms.Label();
            this.totalChargesLabel = new System.Windows.Forms.Label();
            this.label11 = new System.Windows.Forms.Label();
            this.calculateButton = new System.Windows.Forms.Button();
            this.clearButton = new System.Windows.Forms.Button();
            this.exitButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(29, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(87, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Days at Hospital:";
            //
            // daysSpentTextBox
            //
            this.daysSpentTextBox.Location = new System.Drawing.Point(133, 12);
            this.daysSpentTextBox.Name = "daysSpentTextBox";
            this.daysSpentTextBox.Size = new System.Drawing.Size(139, 20);
            this.daysSpentTextBox.TabIndex = 1;
            this.daysSpentTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(12, 39);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(104, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "Medication Charges:";
            //
            // medicationChargesTextBox
            //
            this.medicationChargesTextBox.Location = new System.Drawing.Point(133, 36);
            this.medicationChargesTextBox.Name = "medicationChargesTextBox";
            this.medicationChargesTextBox.Size = new System.Drawing.Size(139, 20);
            this.medicationChargesTextBox.TabIndex = 3;
            this.medicationChargesTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            //
            // surgicalChargesTextBox
            //
            this.surgicalChargesTextBox.Location = new System.Drawing.Point(133, 60);
            this.surgicalChargesTextBox.Name = "surgicalChargesTextBox";
            this.surgicalChargesTextBox.Size = new System.Drawing.Size(139, 20);
            this.surgicalChargesTextBox.TabIndex = 4;
            this.surgicalChargesTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            //
            // rehabChargesTextBox
            //
            this.rehabChargesTextBox.Location = new System.Drawing.Point(133, 108);
            this.rehabChargesTextBox.Name = "rehabChargesTextBox";
            this.rehabChargesTextBox.Size = new System.Drawing.Size(139, 20);
            this.rehabChargesTextBox.TabIndex = 5;
            this.rehabChargesTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            //
            // labFeesTextBox
            //
            this.labFeesTextBox.Location = new System.Drawing.Point(133, 84);
            this.labFeesTextBox.Name = "labFeesTextBox";
            this.labFeesTextBox.Size = new System.Drawing.Size(139, 20);
            this.labFeesTextBox.TabIndex = 5;
            this.labFeesTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(26, 63);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(90, 13);
            this.label3.TabIndex = 6;
            this.label3.Text = "Surgical Charges:";
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(62, 87);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(54, 13);
            this.label4.TabIndex = 7;
            this.label4.Text = "Lab Fees:";
            //
            // label5
            //
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(32, 111);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(84, 13);
            this.label5.TabIndex = 8;
            this.label5.Text = "Rehab Charges:";
            //
            // label6
            //
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(43, 135);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(73, 13);
            this.label6.TabIndex = 9;
            this.label6.Text = "Stay Charges:";
            //
            // stayChargesLabel
            //
            this.stayChargesLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.stayChargesLabel.Location = new System.Drawing.Point(133, 132);
            this.stayChargesLabel.Name = "stayChargesLabel";
            this.stayChargesLabel.Size = new System.Drawing.Size(139, 20);
            this.stayChargesLabel.TabIndex = 10;
            this.stayChargesLabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            //
            // miscChargesLabel
            //
            this.miscChargesLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.miscChargesLabel.Location = new System.Drawing.Point(133, 156);
            this.miscChargesLabel.Name = "miscChargesLabel";
            this.miscChargesLabel.Size = new System.Drawing.Size(139, 20);
            this.miscChargesLabel.TabIndex = 12;
            this.miscChargesLabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            //
            // label9
            //
            this.label9.AutoSize = true;
            this.label9.Location = new System.Drawing.Point(39, 159);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(77, 13);
            this.label9.TabIndex = 11;
            this.label9.Text = "Misc. Charges:";
            //
            // totalChargesLabel
            //
            this.totalChargesLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.totalChargesLabel.Location = new System.Drawing.Point(133, 180);
            this.totalChargesLabel.Name = "totalChargesLabel";
            this.totalChargesLabel.Size = new System.Drawing.Size(139, 20);
            this.totalChargesLabel.TabIndex = 14;
            this.totalChargesLabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            //
            // label11
            //
            this.label11.AutoSize = true;
            this.label11.Location = new System.Drawing.Point(40, 183);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(76, 13);
            this.label11.TabIndex = 13;
            this.label11.Text = "Total Charges:";
            //
            // calculateButton
            //
            this.calculateButton.Location = new System.Drawing.Point(24, 208);
            this.calculateButton.Name = "calculateButton";
            this.calculateButton.Size = new System.Drawing.Size(75, 23);
            this.calculateButton.TabIndex = 15;
            this.calculateButton.Text = "&Calculate";
            this.calculateButton.UseVisualStyleBackColor = true;
            this.calculateButton.Click += new System.EventHandler(this.calculateButton_Click);
            //
            // clearButton
            //
            this.clearButton.Location = new System.Drawing.Point(105, 208);
            this.clearButton.Name = "clearButton";
            this.clearButton.Size = new System.Drawing.Size(75, 23);
            this.clearButton.TabIndex = 16;
            this.clearButton.Text = "Clea&r";
            this.clearButton.UseVisualStyleBackColor = true;
            this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
            //
            // exitButton
            //
            this.exitButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.exitButton.Location = new System.Drawing.Point(186, 208);
            this.exitButton.Name = "exitButton";
            this.exitButton.Size = new System.Drawing.Size(75, 23);
            this.exitButton.TabIndex = 17;
            this.exitButton.Text = "E&xit";
            this.exitButton.UseVisualStyleBackColor = true;
            this.exitButton.Click += new System.EventHandler(this.exitButton_Click);
            //
            // Form1
            //
            this.AcceptButton = this.calculateButton;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.exitButton;
            this.ClientSize = new System.Drawing.Size(284, 238);
            this.Controls.Add(this.exitButton);
            this.Controls.Add(this.clearButton);
            this.Controls.Add(this.calculateButton);
            this.Controls.Add(this.totalChargesLabel);
            this.Controls.Add(this.label11);
            this.Controls.Add(this.miscChargesLabel);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.stayChargesLabel);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.labFeesTextBox);
            this.Controls.Add(this.rehabChargesTextBox);
            this.Controls.Add(this.surgicalChargesTextBox);
            this.Controls.Add(this.medicationChargesTextBox);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.daysSpentTextBox);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Hospital Charges";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox daysSpentTextBox;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox medicationChargesTextBox;
        private System.Windows.Forms.TextBox surgicalChargesTextBox;
        private System.Windows.Forms.TextBox rehabChargesTextBox;
        private System.Windows.Forms.TextBox labFeesTextBox;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label stayChargesLabel;
        private System.Windows.Forms.Label miscChargesLabel;
        private System.Windows.Forms.Label label9;
        private System.Windows.Forms.Label totalChargesLabel;
        private System.Windows.Forms.Label label11;
        private System.Windows.Forms.Button calculateButton;
        private System.Windows.Forms.Button clearButton;
        private System.Windows.Forms.Button exitButton;
    }
}