C# - 代码重构以扩展功能
来计算一个区域的税率。
public class TaxCaculator
{
public Dictionary<RegionATaxRate, decimal> CalculateTax(List<SalesDocket> sales)
{
var result = new Dictionary<TaxRate, decimal>();
// pseudo-code logic to calculate Region A tax rate
return result;
}
}
我有一些代码 代码>和 regionAtaxrate
,并返回包含针对单个regionAtaxrate
s的总数。
在某些情况下,税收计算器
类将使用如下:
var calculator = new TaxCalculator();
var result = calculator.CalculateTax(sales);
return result;
我要扩展此代码,以便将其用于多个区域-B区域B,区域C,区域D等地区的税率将不同。如何修改代码以允许?
I have some code which calculates the tax rates for one region, Region A.
public class TaxCaculator
{
public Dictionary<RegionATaxRate, decimal> CalculateTax(List<SalesDocket> sales)
{
var result = new Dictionary<TaxRate, decimal>();
// pseudo-code logic to calculate Region A tax rate
return result;
}
}
The CalculateTax()
method processes a list of SalesDocket
s which contain an Amount
and
a RegionATaxRate
, and returns a dictionary containing the totals against the individual RegionATaxRate
s.
For some context, the TaxCalculator
class will be used as follows:
var calculator = new TaxCalculator();
var result = calculator.CalculateTax(sales);
return result;
I want to extend this code so that it can be used for multiple regions - Region B, Region C, Region D, etc. However, each region will have a different tax rate. How can I modify my code to allow for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
regionAtaxrate
更改为regiontaxrate
,并在指示相关区域的region> reamialtaxrate
中包含一个属性。例如:
You could change your
RegionATaxRate
toRegionTaxRate
and include a property in theRegionTaxRate
that indicates the pertinent region.For example: