Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Create a class named PatientDemo that does the following:

Create a Patient class for Wrightstown Hospital Billing Department. Include the following get and set properties and fields: patient ID number, name, age, and amount due to the hospital.
Add a constructor for the Patient class that accepts ID, name, age, and amount due.
Override the ToString() method to return all the details for a patient printed out in record format.
Using the Patient class as the base class, derive an InsuredPatient class. An InsuredPatient contains all the data of a Patient, plus fields to hold an insurance company name and the percentages of the hospital bill the insurance company will pay.
You will need a constructor for this class as well.
You will need to get and set those values in the InsuredPatient class. Insurance payments are based on the following table:
Insurance Company Portion of the bill paid by insurance company
Wrightstown Mutual 80%
Red Umbrella 60%
All other companies 25%
Override the parent class ToString() method to include the name of the insurance company, the percentage paid, and the amount due after the insurance has been applied to the bill. When i run the project I get everything but % off of the insurance. Can someone tell me what I am doing wrong?

What I have tried:

C#
using System;
public class PatientDemo
{
    public static void Main()
    {
        InsuredPatient[] patient = new InsuredPatient[5];
        int x, y;
        double grandAmountDue = 0;
        bool goodNum;
        for (x = 0; x < patient.Length; ++x)
        {
            patient[x] = new InsuredPatient();
            Console.Write("Enter patient number ");
            patient[x].PatientNumber = Convert.ToInt32(Console.ReadLine());
            goodNum = true;
            for (y = 0; y < x; ++y)
            {
                if (patient[x].Equals(patient[y]))
                    goodNum = false;
            }
            while (!goodNum)
            {
                Console.Write("Sorry, the patient number " +
                   patient[x].PatientNumber + " is a duplicate. " +
                   "\nPlease reenter ");
                patient[x].PatientNumber = Convert.ToInt32(Console.ReadLine());
                goodNum = true;
                for (y = 0; y < x; ++y)
                {
                    if (patient[x].Equals(patient[y]))
                        goodNum = false;
                }
            }
            Console.Write("Enter name ");
            patient[x].Name = Console.ReadLine();
            Console.Write("Enter age ");
            patient[x].Age = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter insurance company ");
            patient[x].InsCompany = Console.ReadLine();
            Console.Write("Enter amount due ");
            patient[x].AmountDue = Convert.ToDouble(Console.ReadLine());
        }
        Console.WriteLine("\nSummary:\n");
        Array.Sort(patient);
        for (x = 0; x < patient.Length; ++x)
        {
            Console.WriteLine(patient[x].ToString());
            grandAmountDue += patient[x].AmountDue;
        }
        Console.WriteLine("\nAmountDue for all patients is " + grandAmountDue.ToString("C"));
    }

}
class Patient
{
    public Patient(int num, string name, int age, int amt)
    {
        PatientNumber = num;
        Name = name;
        Age = age;
        AmountDue = amt;
    }
    public Patient() : this(9, "ZZZ", 0, 0)
    {
    }
    public int PatientNumber { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public double AmountDue { get; set; }
    public override string ToString()
    {
        return (GetType() + " " + PatientNumber + " " + Name +
           " " + Age + " AmountDue is " + AmountDue.ToString("C"));
    }
    public override bool Equals(Object e)
    {
        bool equal;
        Patient temp = (Patient)e;
        if (PatientNumber == temp.PatientNumber)
            equal = true;
        else
            equal = false;
        return equal;
    }
    public override int GetHashCode()
    {
        return PatientNumber;
    }
}
class InsuredPatient : Patient, IComparable
{
    public const string INSCO1 = "Wrightstown Mutual";
    public const string INSCO2 = "Red Umbrella";
    public const double PCT1 = 0.80;
    public const double PCT2 = 0.60;
    public const double PCT3 = 0.25;
    string insCompany;
    double pctPaid;
    double origAmountDue;
    public InsuredPatient(int num, string name, int age,
      int amt, string co, double pct) :
      base(num, name, age, amt)
    {
        InsCompany = co;
        OrigAmountDue = AmountDue;
        AmountDue = OrigAmountDue - (pctPaid * OrigAmountDue);
    }
    public InsuredPatient() : this(0, "", 0, 0, "", 0)
    {
    }
    public string InsCompany
    {
        get
        {
            return insCompany;
        }
        set
        {
            insCompany = value;
            if (insCompany == INSCO1)
                PctPaid = PCT1;
            else
               if (insCompany == INSCO2)
                PctPaid = PCT2;
            else
                PctPaid = PCT3;
        }
    }
    public double OrigAmountDue
    {
        get
        {
            return origAmountDue;
        }
        set
        {
            origAmountDue = value;
        }
    }
    public double PctPaid
    {
        get
        {
            return pctPaid;
        }
        set
        {
            pctPaid = value;
        }
    }
    public new double AmountDue
    {
        get
        {
            return base.AmountDue;
        }
        set
        {
            origAmountDue = value;
            base.AmountDue = origAmountDue - (pctPaid * origAmountDue);
        }
    }
    public override string ToString()
    {
        string temp = base.ToString();
        temp = temp + "\n   Original total is " + origAmountDue.ToString("C") +
        "\n   " + insCompany + " Insurance Company pays " +
           pctPaid.ToString("P") + "\n So patient owes......................... " +
           AmountDue.ToString("C");
        return temp;
    }
    int IComparable.CompareTo(Object o)
    {
        int returnVal;
        InsuredPatient temp = (InsuredPatient)o;
        if (this.PatientNumber > temp.PatientNumber)
            returnVal = 1;
        else
           if (this.PatientNumber < temp.PatientNumber)
            returnVal = -1;
        else
            returnVal = 0;
        return returnVal;
    }
}
Posted
Updated 4-Aug-18 19:23pm
v2
Comments
Patrice T 4-Aug-18 23:14pm    
Describe how it don't do what it should.
Eric Lynch 5-Aug-18 1:50am    
Come on! You've got the name "ppolymorphe" and didn't catch the "new" keyword on the InsuredPatient.AmountDue property and suggest changing it to a "virtual/override"? Such a missed opportunity :)
Patrice T 5-Aug-18 3:09am    
I did not even tried to read the code :)
Eric Lynch 5-Aug-18 1:36am    
I second ppolymorphe's point. While it seems you have some problem related to PctPaid, it is unclear exactly wheat that problem might be. For example, does it affect all patients or only one? What were your expected values? What were your actual values? If you want to receive help in a timely fashion, the more information you include the better your results will be. Unlike you, we cannot see your screen or step through the debugger to look at things. We can only work with the information you provide us.

That said, though it won't help you fix your problem, I would like to suggest a couple of improvements to your code.

You repeat the following a couple of times in your code:

goodNum = true;
for (y = 0; y < x; ++y)
if (patient[x].Equals(patient[y]))
goodNum = false;

You might consider moving this into a "IsPatientValid" method, which you can call in both places. Also, you ask for a patient and then loop to ask for a valid patient. If you refactor slightly, you can reduce this duplication to a single loop that gets a valid patient.

Also, it looks like you are familiar with auto-implemented properties. You might want to use them more consistently. For example, the following:

double pctPaid;
public double PctPaid
{
get
{
return pctPaid;
}
set
{
pctPaid = value;
}
}

Could be:

public double PctPaid { get; set; }

It really cleans up the code quite a bit. You have a few places that would benefit from it.

Also, about this:

public new double AmountDue

Just don't! There are almost no cases where hiding an inherited member with the "new" keyword is appropriate. This is not one of them.

Add a "virtual" keyword to AmountDue in Patient and an "override" keyword in AmountDue in InsuredPatient. This may seem like a minor difference. Trust me. It is not.

Eventually, in some future program, that "new" keyword will cause you some major headaches. Using the "new" keyword, if you ever assign an InsuredPatient value to a Patient variable, you won't get the AmountDue you expect. You'll get the one from Patient...not from InsuredPatient. The "new" keyword simply hides the value in the parent class, but its still there waiting to cause you problems.

The "virtual/override" keyword pair replaces it. Together they provide one of the many wonders that is polymorphism :)

1 solution

Read the instructions again:
Quote:
Override the parent class ToString() method to include the name of the insurance company, the percentage paid, and the amount due after the insurance has been applied to the bill.

Which class did you add this information to?
 
Share this answer
 
Comments
Eric Lynch 5-Aug-18 1:43am    
Nice catch! I was busy asking for more info, trying to dissuade the OP from using the "new" keyword on an inherited property, and suggesting some ways to reduce clutter :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900