C# - 代码重构以扩展功能

发布于 2025-02-04 14:15:55 字数 647 浏览 1 评论 0原文

来计算一个区域的税率。

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 SalesDockets which contain an Amount and
a RegionATaxRate, and returns a dictionary containing the totals against the individual RegionATaxRates.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一桥轻雨一伞开 2025-02-11 14:15:56

您可以将regionAtaxrate更改为regiontaxrate,并在指示相关区域的region> reamialtaxrate中包含一个属性。

例如:


public class RegionTaxRate {
    public string Region {get;set;}
    public decimal TaxRate {get;set;}
}

You could change your RegionATaxRate to RegionTaxRate and include a property in the RegionTaxRate that indicates the pertinent region.

For example:


public class RegionTaxRate {
    public string Region {get;set;}
    public decimal TaxRate {get;set;}
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文