Hi,
Continuation of a previous thread. This is different question, so new thread. I have an array of 5 taxpayers. User enters 5 SSNs and Incomes. An array is instantiated to fill this data. I need to display all five objects listing SSN, income and tax. I am having difficulty because five of the same taxpayer prints over and over. I can't figure out how to display data for each. Used to using parallel arrays for this. Here is part of what I have. I have colored my weak attempt to get SSN, but I know it is wrong.

Thank you.

//	Create a class named Taxpayer that has the following data members:
        public class Taxpayer
        {
            //Social Security number (use type string, no dashes between groups).  Use get and set accessors.
            string SSN
            { get; set; }

            int yearlyGrossIncome // Use get and set accessors.
            { get; set; }

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

//  Implement a for-loop that will prompt the user to enter the Social Security Number and gross income.
            for (x = 0; x < taxArray.Length; x++)
            {
                taxArray[x] = new Taxpayer();
                Console.Write("Please enter the Social Security Number for taxpayer {0}:  ", x);
                SSN = Convert.ToString(Console.ReadLine());
              
                //SSN = String.Format("{0:000-00-0000}");
                Console.Write("Please enter the gross income for taxpayer {0}:  ", x);
                income = 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 < 5; i++)
                {
                    
                    Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i, Taxpayer[i].SSN, income, tax);
                }

Instead of SSN = Convert.ToString(Console.ReadLine());
use
taxArray[x].SSN = Console.ReadLine(); // readline already returns a string

Thank you. Now I am getting an 'inaccessible due to protection level'. I don't get that. I am using public class Taxpayer.

change private to public in

public int taxOwed  //  Use read-only accessor.
            {
                get { return taxOwed; }
               [B] private[/B] set { }
            }

It is required to be read-only. If I remove private, then it will not be read-only, right? How else can I get this done?

You have defined all your properties without access specifier, so the default private is used. It is not sufficient that your class is public, you must also make the properties and methods of your class public if you want them to be so.

You generate getters and setters for your Taxpayer but you don't declare them public. That's why the compiler can't access them from other classes. Since one can't see the context of your methods besides your for-iterations, I guess that this is the problem.

Thank you everyone, this is the most massive C# thing I have done to date. Now I am having difficulty with income. I did it like SSN, taxArray[x].income. It is declared above, but says there is no definition for income. Arrggg. They are both there, why does it not see it?

I can't wait until I know this well enough that I can ANSWER questions instead of just asking. . . someday :)

static void Main(string[] args)
            {
                //  instantiate an array of five (5) Taxpayer objects.

                string SSN = "0";
                int income = 0;
                int tax = 0;
                int x = 1;
                Taxpayer[] taxArray = new Taxpayer[5];


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

                    //SSN = String.Format("{0:000-00-0000}");
                    Console.Write("Please enter the gross income for taxpayer {0}:  ", x);
                    taxArray[x].income = Convert.ToInt32(Console.ReadLine());

                    Taxpayer.GetRates();

                }  //end for

                //  Implement a for-loop that will display each object as formatted taxpayer SSN, income and calculated tax.
                for (int i = 0; i < 5; i++)
                {

                    Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i, SSN, income, tax);
                } // end for

Your TaxPayer class has no property called income.
Could you post what you have so far for the code of this class?

I hope it isn't inappropriate to put all of it up here, but here it is. Requirements are documented as comments. Any insight would be greatly appreciated. I am also having difficulty with the comparing issue. Well maybe not, I can't get rid of all these pesky little errors to check through it. I wanted to add some things to make sure of valid input, etc. but ran out of time. Also formatting for SSN. Not finding really good examples of these two things to fit what I need. Thanks for the help!

namespace Taxes
{
    class Rates
    {
        // Create a class named rates that has the following data members: 
        private int incomeLimit;
        private double lowTaxRate;
        private double highTaxRate;

        public int IncomeLimit  // use read-only accessor.
        { get { return incomeLimit; } }

        public double LowTaxRate // use read-only accessor.
        { get { return lowTaxRate; } }

        public double HighTaxRate // use read-only accessor.
        { get { return highTaxRate; } }

        //A class constructor that assigns default values of limit=30000, low rate = .15 and high rate = .28.
        public Rates()
        {
            int limit = 30000;
            double lowRate = .15;
            double highRate = .28;

            incomeLimit = limit;
            lowTaxRate = lowRate;
            highTaxRate = highRate;
        }

        //A class constructor that takes three parameters to assign input values for limit, low rate and high rate.
        public Rates(int limit, double lowRate, double highRate)
        {

        }

        //  A CalculateTax method that takes an income parameter and computes the tax as follows:
        public int CalculateTax(int income)
        {
            int limit = 0;
            double lowRate = 0;
            double highRate = 0;
            int taxOwed = 0;

            //  If income is less than the limit then return the tax as income times low rate.
            if (income < limit)
                taxOwed = Convert.ToInt32(income * lowRate);
            //  If income is greater than or equal to the limit then return the tax as income times high rate.
            if (income >= limit)
                taxOwed = Convert.ToInt32(income * highRate);
            return taxOwed;
        }
    }  //end class Rates

        //	Create a class named Taxpayer that has the following data members:
        public class Taxpayer
        {
            //Social Security number (use type string, no dashes between groups).  Use get and set accessors.
            string SSN
            { get; set; }

            int yearlyGrossncome // Use get and set accessors.
            { get; set; }

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

            // **  The Taxpayer class should be set up so that its objects are comparable to each other based on tax owed.
            class taxpayer : IComparable
            {
                public int taxOwed { get; set; }
                public int income { get; set; }

                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
            } //end taxpayer  IComparable class

                //  **The tax should be calculated whenever the income is set.
                //  The Taxpayer class should have a getRates class method that has the following.
                public static void GetRates()
                {
                    //  Local method data members for income limit, low rate and high rate.
                    int incomeLimit = 0;
                    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 to enter your own values? (enter 0) or would you like to use the default values? (enter 1):  ");

                    /*   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 = Convert.ToString(Console.ReadLine());

                    if (userInput == "1")
                    {
                        Rates rates = new Rates();
                        rates.CalculateTax(incomeLimit);
                    } // 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. */

                    if (userInput == "0")
                    {
                        Console.Write("Please enter the income limit: ");
                        incomeLimit = 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(incomeLimit, lowRate, highRate);
                        rates.CalculateTax(incomeLimit);

                    }  // end if
                }  //end GetRates class
            


            static void Main(string[] args)
            {
                //  instantiate an array of five (5) Taxpayer objects.

                string SSN = "0";
                int income = 0;
                int tax = 0;
                int x = 1;
                Taxpayer[] taxArray = new Taxpayer[5];


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

                    //SSN = String.Format("{0:000-00-0000}");
                    Console.Write("Please enter the gross income for taxpayer {0}:  ", x);
                    taxArray[x].income = Convert.ToInt32(Console.ReadLine());

                    Taxpayer.GetRates();

                }  //end for

                //  Implement a for-loop that will display each object as formatted taxpayer SSN, income and calculated tax.
                for (int i = 0; i < 5; i++)
                {

                    Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i, SSN, income, tax);
                } // end for



                //  Implement a for-loop that will sort the five objects in order by the amount of tax owed and then display 
                //each object as formatted taxpayer SSN, income and calculated tax.

                Array.Sort(taxArray);
                Console.WriteLine("Sorted by tax owed");
                for (int i = 0; x < taxArray.Length; ++i)
                {
                    Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i, SSN, income, tax);

                    
                }


            }  //end main
        } //  end Taxpayer class
    

}  //end namespace

//	Internal documentation.

you haven't written get/set property for income in Taxpayer class

Edited 5 Years Ago by abelLazm: n/a

On line 38 of this last code you have method CalculateTax which has an income parameter. So in that case income is just an integer variable which you pass to a method. Is this what you want to do?

I think so. Don't I just want the parameter to hold the income from the taxpayer? How else would I use it? And to AbelLazm, doesn't the yearlyGrossIncome on line 62 do this? I tried changing that name to just income and it still gave me the problem.

No if you are using yearlyGrossIncome at line 62 then at line 157 use yearlyGrossIncome instead of income because you can only access those properties which are defined in the class and there is no property named income in class Taxpayer

This question has already been answered. Start a new discussion instead.