Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: C# Problem help

  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C# Problem help

    Hi, I need help with this C# code, it's not properly displaying the totals for the income and whatnot. Can anyone help me with this?

    Also here is my assignment
    Create a class name Taxpayer. Data fields for taxpayer objects include the Social Security number (use a string for the type, but do not use dashes within the number), the yearly gross income, and the tax owed. Include a property with get and set accessors for the first two data fields, but make the tax owed a read-only property. The tax should be calculated whenever the income is set. Assume the tax is 15% of income for incomes under $30,000 and 28% for incomes that are $30,000 or higher. Write a program that declares an array of ten Taxpayer objects. Prompt the user for data for each object and display the ten objects.
    Modify the Taxpayer class so its objects are comparable to each other based on tax owed. Modify the TaxPayerDemo application so that after the ten objects are displayed, they are sorted in order by the amount of tax owed; then display the objects again.
    --------------------------------------------------
    And here's the code I have so far.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Exercise_6
    {
        class Rates
        {
            public readonly int incomeLimit;
    
            public readonly double lowTaxRate;
    
            public readonly double highTaxRate;
    
            public Rates()
            {
                incomeLimit = 30000;
                lowTaxRate = .15;
                highTaxRate = .28;
    
    
            }
    
            public Rates(int limit, double lowRate, double highRate)
            {
                incomeLimit = limit;
                lowTaxRate = lowRate;
                highTaxRate = highRate;
            }
            public void CalculateTax(double income)
            {
                double tax;
    
                if (income < incomeLimit)
                {
                    tax = income * lowTaxRate;
                }
                if (income > incomeLimit)
                {
                    tax = income * highTaxRate;
                }
                Taxpayer tt = new Taxpayer();
    
            }
        }
    
    
    
        class Taxpayer : IComparable
        {
            public string socialSecurityNumber { get; set; }
            public int yearlyGrossIncome { get; set; }
            public readonly double taxOwed;
    
            public void getRates()
            {
                int incomeLimit;
                double lowRate;
                double highRate;
    
    
                Console.WriteLine("Do you want default values (enter D) or enter your own (enter O)?");
                char values = Convert.ToChar(Console.ReadLine());
                if (values == 'd' || values == 'D')
                {
                    Rates deval = new Rates();
                    incomeLimit = deval.incomeLimit;
                    lowRate = deval.lowTaxRate;
                    highRate = deval.highTaxRate;
                    deval.CalculateTax(yearlyGrossIncome);
    
                }
                if (values == 'o' || values == 'O')
                {
                    Console.WriteLine("Enter the dollar limit:");
                    incomeLimit = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Enter the low rate:");
                    lowRate = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("Enter the high rate:");
                    highRate = Convert.ToDouble(Console.ReadLine());
    
                }
    
            }
    
            public int CompareTo(object obj)
            {
                throw new NotImplementedException();
            }
        }
        class Exercise_6
        {
            public static void Main(string[] args)
            {
                string[] socialSecurityNumber = new string[10];
                int[] grossIncome = new int[10];          
                double[] taxArray = new double[10];
                string inputstring;
    
                for (int x = 0; x < socialSecurityNumber.Length; ++x)
                {
                    Console.WriteLine("Enter Social Security Number for taxpayer: ");
                    inputstring = Console.ReadLine();
                    socialSecurityNumber[0] = inputstring;
                    Console.WriteLine("Enter gross income for taxpayer: ");
                    inputstring = Console.ReadLine();
                    grossIncome[0] = Convert.ToInt32(inputstring);
                    Taxpayer own = new Taxpayer();
                    own.yearlyGrossIncome = grossIncome[0];
                    Rates tp1 = new Rates();
                    tp1.CalculateTax(own.yearlyGrossIncome);
                    
                    own.getRates();
                    taxArray[0] = 0;
    
                }
                for (int x = 0; x < socialSecurityNumber.Length; ++x)
                {
                    Console.WriteLine("Taxpayer SSN: {0} income ${1} Tax is ${2}", socialSecurityNumber, grossIncome, taxArray);
    
                }
            }
        }
    }

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Well you need to implement the CompareTo function so it returns if the passed in object is less than, greater than or equal to the instance. The return codes for CompareTo can be found on the MSDN.
    OracleGuy

  • #3
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay I did that, and how do I properly display the income, tax, and social security number at the bottom of the code. When I debug it I get an error for each record.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •