简体   繁体   中英

Having trouble with array of objects output

I am writing this program and cannot get two items to display. The taxowed variable shows 0 in each output and the taxpayer number does not show at all in the output either...can someone explain why this is happening?

class Rates
{
    private int limit;
    private double lowRate;
    private double highRate;

    public int Limit
    {
        get
        {
            return limit;
        }
    }
    public double LowRate
    {
        get
        {
            return lowRate;
        }
    }
    public double HighRate
    {
        get
        {
            return highRate;
        }
    }

    public void setRates()
    {
        limit = 30000;
        lowRate = 0.15;
        highRate = 0.28;
    }

    public void setRates(int iLimit, double lRate, double hRate)
    {
        Console.WriteLine("Enter dollar limit: ");
        iLimit = Convert.ToInt32(Console.ReadLine());
        limit = iLimit;
        Console.WriteLine("Enter the low rate: ");
        lRate = Convert.ToDouble(Console.ReadLine());
        lowRate = lRate;
        Console.WriteLine("Enter the high rate: ");
        hRate = Convert.ToDouble(Console.ReadLine());
        highRate = hRate;
    }

    public void CalculateTax(int userIncome)
    {
        if (userIncome < limit)
            Console.Write(userIncome * lowRate);
        else
            Console.Write(userIncome * highRate);
    }
}
class TaxPayer : IComparable
{
    private String ssn;
    private double grossIncome;
    private double taxOwed;

    public String SSN
    {
        get
        {
            return this.ssn;
        }
        set
        {
            this.ssn = value;
        }
    }
    public double GrossIncome 
    {
        get
        {
            return this.grossIncome;
        }
        set
        {
            this.grossIncome = value;
        }
    }
    public double TaxOwed
    {
        get
        {
            return taxOwed;
        }
    }

    public void CalcTax()
    {
        Rates firstRate = new Rates();
        if (GrossIncome < firstRate.Limit)
            taxOwed = GrossIncome * firstRate.LowRate;
        else
            taxOwed = GrossIncome * firstRate.HighRate;
    }

    int IComparable.CompareTo(object o)
    {
        int returnVal;
        TaxPayer temp = (TaxPayer)o;
        if (this.TaxOwed < temp.TaxOwed)
            returnVal = -1;
        else
            returnVal = 0;
        return returnVal;
    }

    public void getRates()
    {
        int income = 0;
        double lowRate = 0;
        double highRate = 0;
        int limit = 0;
        char userInput;
        char upper;
        Rates newRates = new Rates();


        Console.WriteLine("Do you want default values ('D') or enter your own ('O')? ");
        userInput = Convert.ToChar(Console.ReadLine());
        upper = char.ToUpper(userInput);

        if (upper == 'D')
            newRates.setRates();
        newRates.CalculateTax(income);
        if (upper == 'O')
            newRates.setRates(limit, lowRate, highRate);
    }
    static void Main()
    {
        TaxPayer[] tpArray = new TaxPayer[5];
        int x;
        for (x = 0; x < tpArray.Length; ++x)
            tpArray[x] = new TaxPayer();
        for (x = 0; x < tpArray.Length; ++x)
        {
            Console.WriteLine("Enter the social scurity number: ");
            tpArray[x].SSN = Convert.ToString(Console.ReadLine());
            Console.WriteLine("Enter the gross income for the taxpayer: ");
            tpArray[x].GrossIncome = Convert.ToDouble(Console.ReadLine());
            tpArray[x].getRates();
            tpArray[x].CalcTax();
        }
        for (x = 0; x < tpArray.Length; ++x)
        {
            Console.WriteLine("Taxpayer # {0} SSN: {1} income {2} and tax owed is {3}", tpArray[x], tpArray[x].SSN,
                tpArray[x].GrossIncome, tpArray[x].TaxOwed.ToString("C"));
        }

        Array.Sort(tpArray);
        for (x = 0; x < tpArray.Length; ++x)
        {
            Console.WriteLine("Taxpayer # {0} SSN: {1} income {2} and tax owed is {3}", tpArray[x], tpArray[x].SSN,
                tpArray[x].GrossIncome, tpArray[x].TaxOwed.ToString("C"));
        }
        Console.ReadLine();
    }
}

}

Using the debugger, step into this line: tpArray[x].CalcTax(); and you'll see the problem: you are not using the rates entered by user, but creating a new Rates object for the purpose of calculation.

Naturally, this new Rates object is not initialized, and the rates are all zeroes.

Another problem is, in order to output the taxpayer #, you should output x , not tpArray[x]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM