デバッガーを使って近づいているような気がしますが、それでもこれを理解することはできません。
私は次のコードを歩いています
namespace Taxes
{
public class Rates
{
//A class constructor that assigns default values
public Rates()
{
incLimit = 30000;
lowTaxRate = .15;
highTaxRate = .28;
}
//A class constructor that takes three parameters to assign input values for limit, low rate and high rate.
public Rates(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)
{
//determine if the income is above or below the limit and calculate the tax owed based on the correct rate
int taxOwed;
if (income < incLimit)
taxOwed = Convert.ToInt32(income * lowTaxRate);
else
taxOwed = Convert.ToInt32(income * highTaxRate);
return taxOwed;
}
}
// The Taxpayer class is a comparable class
public class Taxpayer : IComparable
{
//Use get and set accessors.
private int taxOwed;
string SSN
{ set; get; }
int grossIncome
{ set; get; }
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;
}
public static Rates GetRates()
{
// Local method data members for income limit, low rate and high rate.
int incLimit;
double lowRate;
double highRate;
string userInput;
//Rates myRates = new Rates(incLimit, lowRate, highRate);
//Rates rates = new Rates();
// 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 they want the default values or enter their own
userInput = (Console.ReadLine());
if (userInput == "D" || userInput == "d")
{
Rates myRates = new Rates();
return myRates;
//Rates.Rates();
//rates.CalculateTax(incLimit);
}
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 myRates = new Rates(incLimit, lowRate, highRate);
return myRates;
//rates.CalculateTax(incLimit);
}
else return null;
}
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());
//taxArray[x].taxOwed = taxRates.CalculateTax(taxArray[x].grossIncome);
}
Rates myRate = Taxpayer.GetRates();
//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, myRate.CalculateTax(taxArray[i].grossIncome));//taxArray[i].taxOwed);
}
// 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)
{
//double taxes = myTax.CalculateTax(taxArray[i].grossIncome);
Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, myRate.CalculateTax(taxArray[i].grossIncome));
}
}
}
}
なんらかの理由で未払額でソートされていないことを除けば、すべて解決しました。