programnoob 0 Newbie Poster

I am running into and problem where I am trying to pass an element in an array to a method to calculate the tax that is owed but all I get as a result is 0, which is not right.

Here is the writeline:

Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, taxRates.CalculateTax(taxArray[i].grossIncome)); 

and the zero is coming from the last one {3:c}

Also, the sort is not sorting by the income level for some reason

Here is the entire namespace and output:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment5_2
{
    public class Rates
    {
        // Create a class named rates that has the following data members: 
        int incLimit;
        double lowTaxRate;
        double highTaxRate;

        // use read-only accessor
        public int IncomeLimit
        { get { return incLimit; } }
        public double LowTaxRate
        { get { return lowTaxRate; } }
        public double HighTaxRate
        { get { return highTaxRate; } }

        //A class constructor that assigns default values 
        public void assignRates()
        {
            int limit = 30000;
            double lowRate = .15;
            double highRate = .28;
            incLimit = limit;
            lowTaxRate = lowRate;
            highTaxRate = highRate;
        }
        //A class constructor that takes three parameters to assign input values for limit, low rate and high rate.
        public void assignRates(int lim, double low, double high)
        {
            incLimit = lim;
            lowTaxRate = low;
            highTaxRate = high;
        }
        //  A CalculateTax method that takes an income parameter and computes the tax as follows:
        public int CalculateTax(int income)
        {

            int taxOwed;
            //  If income is less than the limit then return the tax as income times low rate.
            if (income < incLimit){
                taxOwed = Convert.ToInt32(income * lowTaxRate); }
            //  If income is greater than or equal to the limit then return the tax as income times high rate.
            else if(income >= incLimit) {
                taxOwed = Convert.ToInt32(income * highTaxRate);}
            else taxOwed = 0;
            return taxOwed;
        }


    }  //end class Rates

    // Create a class named Taxpayer that has the following data members:
    public class Taxpayer : IComparable
    {
        //Use get and set accessors.
        string SSN
        { get; set; }
        int grossIncome
        { get; set; }

        //  Use read-only accessor.
        public int taxOwed
        {
            get { return taxOwed; }
        }

        int IComparable.CompareTo(Object o)
        {
            int returnVal;
            Taxpayer temp = (Taxpayer)o;
            if (this.taxOwed > temp.taxOwed)
                returnVal = 1;
            else if (this.taxOwed < temp.taxOwed)
                returnVal = -1;
            else returnVal = 0;

            return returnVal;

        }  // End IComparable.CompareTo

        public static void GetRates()
        {
            //  Local method data members for income limit, low rate and high rate.
            int incLimit;
            double lowRate;
            double highRate;
            string userInput;
            //  Prompt the user to enter a selection for either default settings or user input of settings.
            Console.Write("Would you like the default values (D) or would you like to enter the values (E)?:  ");
            /*   If the user selects default the default values you will instantiate a rates object using the default constructor
            * and set the Taxpayer class data member for tax equal to the value returned from calling the rates object CalculateTax method.*/
            userInput = (Console.ReadLine());
            if (userInput == "D" || userInput == "d")
            {
                Rates rates = new Rates();
                rates.assignRates();
            } // end if
            /*  If the user selects to enter the rates data then prompt the user to enter values for income limit, low rate and high rate, 
             * instantiate a rates object using the three-argument constructor passing those three entries as the constructor arguments and 
             * set the Taxpayer class data member for tax equal to the valuereturned from calling the rates object CalculateTax method. */
            else if (userInput == "E" || userInput == "e")
            {
                Console.Write("Please enter the income limit: ");
                incLimit = Convert.ToInt32(Console.ReadLine());
                Console.Write("Please enter the low rate: ");
                lowRate = Convert.ToDouble(Console.ReadLine());
                Console.Write("Please enter the high rate: ");
                highRate = Convert.ToDouble(Console.ReadLine());
                Rates rates = new Rates();
                rates.assignRates(incLimit, lowRate, highRate);
            }
        }

        static void Main(string[] args)
        {

            Taxpayer[] taxArray = new Taxpayer[5];
            Rates taxRates = new Rates();
            //  Implement a for-loop that will prompt the user to enter the Social Security Number and gross income.
            for (int x = 0; x < taxArray.Length; ++x)
            {
                taxArray[x] = new Taxpayer();
                Console.Write("Please enter the Social Security Number for taxpayer {0}:  ", x + 1);
                taxArray[x].SSN = Console.ReadLine();

                Console.Write("Please enter the gross income for taxpayer {0}:  ", x + 1);
                taxArray[x].grossIncome = Convert.ToInt32(Console.ReadLine());

            }

            Taxpayer.GetRates();

            //  Implement a for-loop that will display each object as formatted taxpayer SSN, income and calculated tax.
            for (int i = 0; i < taxArray.Length; ++i)
            {
                Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, taxRates.CalculateTax(50000));//taxRates.CalculateTax(taxArray[i].grossIncome));

            } // end for 
            //  Implement a for-loop that will sort the five objects in order by the amount of tax owed 
            Array.Sort(taxArray);
            Console.WriteLine("Sorted by tax owed");
            for (int i = 0; i < taxArray.Length; ++i)
            {
                Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, taxRates.CalculateTax(taxArray[i].grossIncome));

            }
        }  
    } 

}  

output:

Please enter the Social Security Number for taxpayer 1: 444
Please enter the gross income for taxpayer 1: 16113
Please enter the Social Security Number for taxpayer 2: 222
Please enter the gross income for taxpayer 2: 1111111
Please enter the Social Security Number for taxpayer 3: 333
Please enter the gross income for taxpayer 3: 5555
Please enter the Social Security Number for taxpayer 4: 111
Please enter the gross income for taxpayer 4: 25000
Please enter the Social Security Number for taxpayer 5: 555
Please enter the gross income for taxpayer 5: 65000
Would you like the default values (D) or would you like to enter the values (E)?
: d
Taxpayer # 1 SSN: 444, Income is $16,113.00, Tax is $0.00
Taxpayer # 2 SSN: 222, Income is $1,111,111.00, Tax is $0.00
Taxpayer # 3 SSN: 333, Income is $5,555.00, Tax is $0.00
Taxpayer # 4 SSN: 111, Income is $25,000.00, Tax is $0.00
Taxpayer # 5 SSN: 555, Income is $65,000.00, Tax is $0.00
Sorted by tax owed
Taxpayer # 1 SSN: 111, Income is $25,000.00, Tax is $0.00
Taxpayer # 2 SSN: 555, Income is $65,000.00, Tax is $0.00
Taxpayer # 3 SSN: 333, Income is $5,555.00, Tax is $0.00
Taxpayer # 4 SSN: 444, Income is $16,113.00, Tax is $0.00
Taxpayer # 5 SSN: 222, Income is $1,111,111.00, Tax is $0.00
Press any key to continue . . .

Thank you for your help, this one has got me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.