在 Taxpayer 类上实现 Icomparable 以根据 TaxOwed 进行排序
我无法实现 Icomparable CompareTo 来根据所欠税款来比较 Taxpayer 对象..有人可以帮助我实现 Taxpayer 类的 icomparable 实现吗? 我想像这里一样实现 icomparable - 接口对我来说是新主题..请帮助 http://www.dotnetperls。 com/icomparable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 yearlyGrossIncome // 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].yearlyGrossIncome = 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
。
i am not being able to implemenet Icomparable CompareTo to compare Taxpayer objects based on tax owed ..can someone help me with the icomparable implementatioin of Taxpayer class??
I want to implement the icomparable like here-Interfaces is new topic to me..please help http://www.dotnetperls.com/icomparable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 yearlyGrossIncome // 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].yearlyGrossIncome = 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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在公共类上实现
IComparable
并消除私有taxpayer
类。这是 Taxpayer 类第一部分的重写:
You need to implement
IComparable<Taxpayer>
on the public class and eliminate the privatetaxpayer
class.Here is a rewrite of the first part of the Taxpayer class:
您已经实现了 IComparable 接口。我建议您使用
IComparable
。这是在
taxpayer
类上实现IComparable
的方法:您不需要在
CompareTo
方法内添加if
语句。如果this
的值较小且other
的值较大,则返回负数。如果相反,则返回正值。如果相等则返回零。这就是将它们彼此相减的情况this.taxOwed - other.taxOwed
如果您像这样使用它:
输出将是:
Update 我对以下内容感到困惑代码,我没有注意到有两个类
taxpayer
和Taxpayer
。为什么你同时拥有它们?您只需要一个,就可以像上面一样实现IComparable
。You've already implemented the IComparable interface. I suggest that you use
IComparable<taxpayer>
.This is how you could implement
IComparable<taxpayer>
on thetaxpayer
class:You don't need to add
if
statements inside theCompareTo
method. Return negative if the value ofthis
is smaller andother
is bigger. Return positive if the opposite is true. Return zero if they are equal. That is the case of just subtracting them from each otherthis.taxOwed - other.taxOwed
If you use it like this:
The output would be:
Update I was confused by the code and I didn't pay attention that there was two classes
taxpayer
andTaxpayer
. Why do you have them both? You only need one and you can implementIComparable<Taxpayer>
like above.