Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有办法缩短这个长if/else if返回方法?_C# - Fatal编程技术网

C# 有没有办法缩短这个长if/else if返回方法?

C# 有没有办法缩短这个长if/else if返回方法?,c#,C#,我有一个很长的方法: public decimal decDiscount(QuoteData quoteData) { if (TotalChapter7(quoteData) >= 7499) return 5300; else if (TotalChapter7(quoteData) >= 7449) return 5300; else if (TotalChapter7(q

我有一个很长的方法:

public decimal decDiscount(QuoteData quoteData)
    {
        if (TotalChapter7(quoteData) >= 7499)
            return 5300;
        else if (TotalChapter7(quoteData) >= 7449)
            return 5300;
        else if (TotalChapter7(quoteData) >= 7399)
            return 5250;
        else if (TotalChapter7(quoteData) >= 7349)
            return 5200;
        else if (TotalChapter7(quoteData) >= 7299)
            return 5200;
        else if (TotalChapter7(quoteData) >= 7249)
            return 5150;
        else if (TotalChapter7(quoteData) >= 7199)
            return 5100;
        else if (TotalChapter7(quoteData) >= 7149)
            return 5100;
        else if (TotalChapter7(quoteData) >= 7099)
            return 5050;
            //...
        else if (TotalChapter7(quoteData) >= 1199)
            return 1100;
        else if (TotalChapter7(quoteData) >= 1149)
            return 1100;
        else if (TotalChapter7(quoteData) >= 1099)
            return 1050;
        else if (TotalChapter7(quoteData) >= 1049)
            return 1000;
        else
            return 0;
    }
具有循环模式,可通过以下Excel表格进行说明:

......>>>>

从最高的“费用”(7499)开始,每张账单的费用将下降50英镑。然而,每100次的回报(或“折扣价格”)保持不变(两次50%的费用下降),然后一次回报(一次50%的费用下降)下降50%,然后重复

正如你所看到的,我的方法相当长(我在7049和1249之间遗漏了)。我能做些什么来缩短这个吗


谢谢。

试着使用字典,试着做一个例子,但我不经常使用C,所以纠正代码中不正确的地方,试着理解一下:

public decimal decDiscount(QuoteData quoteData)
{
    int result = 0; //if it doesn't match to any value on the dictionary it will return 0
    Dictionary<int, int> quotes = new Dictionary<int, int();
    quotes.add(7499, 5300); // not sure if that's how you add values to a dictionary
    ...
    quotes.add(1049, 1000);

    for(Entry<int, int> element in quotes) //not sure about the enhanced for too hehehe, not using C# for a while
    {
        if(TotalChapter7(quoteData) >= element.key() && element.value > result)
        {
            result = element.value(); //don't break cause you have to test the entire list for acurracy
        }
    }

    return result;
}
公共十进制折扣(QuoteData QuoteData)
{
int result=0;//如果它与字典上的任何值都不匹配,它将返回0
Dictionary quotes=新字典伪代码如下:

Dictionary<Int, Int> getDiscounts(int startFee, int startDiscount, int endFee)
{
    Dictionary <Int, Int> quoteDictionary = new Dictionary<Int, Int> ();
    for(int i = 0; i++; startFee >= endFee)
    {
         startFee -= 50;
         if(i != 0)
         {
             startDiscount -= 50;
         }
         if(i == 2)
         {
             i = -1;
         }
         quoteDictionary[startFee] = startDiscount;
    }
    return quoteDictionary;
}
Dictionary获取折扣(int startFee、int StartDiscovery、int endFee)
{
Dictionary quoteDictionary=新字典();
对于(int i=0;i++;startFee>=endFee)
{
startFee-=50;
如果(i!=0)
{
StartDiscovery-=50;
}
如果(i==2)
{
i=-1;
}
quoteDictionary[startFee]=StartDiscovery;
}
返回引用指令;
}
你可以这样称呼它:

Dictionary <Int, Int> prices = getDiscounts(7499, 5300, 1049);
int quote = TotalChapter7(quoteData);
int roundedQuote = quote - ((quote % 50) + 1);
int discountedFee = prices[roundedQuote];
字典价格=获得折扣(749953001049);
int quote=第7章总计(quoteData);
int roundedQuote=quote-((quote%50)+1);
int折扣价=价格[四舍五入报价];

也许这会有帮助:

        List<Tuple<int, int>> _FeeToPrice = new List<Tuple<int, int>> { 
                new Tuple<int,int>(7499,5300),
                new Tuple<int,int>(7399,5250),
                            ...
                new Tuple<int,int>(1049,1000)
            };

        public decimal decDiscount(QuoteData quoteData)
        {
            var processedQuoteData = TotalChapter7(quoteData);
            var tuple = _FeeToPrice.FirstOrDefault(x => processedQuoteData >= x.Item1);
            if (tuple != null)
                return tuple.Item2;

            return 0;                           
        }
List\u FeeToPrice=新列表{
新元组(74995300),
新元组(73995250),
...
新元组(10491000)
};
公共十进制折扣(QuoteData QuoteData)
{
var processedQuoteData=TotalChapter7(quoteData);
var tuple=_FeeToPrice.FirstOrDefault(x=>processedQuoteData>=x.Item1);
if(元组!=null)
返回tuple.Item2;
返回0;
}
编辑:
\u FeeToPrice
结构可以从文件、数据库或其他来源加载,这样可以更容易地更正返回值

这些价格和折扣都是数据!数据不应编译成代码

我不会在代码中构建降价,我会构建底层的定价结构(他们可能会很好地处理降价,但不太可能改变整体定价结构)

我希望价格和相关折扣在一个容易改变的地方(例如数据库、xml文件)反映这种结构

public class Pricing
{
     private List<Tuple<decimal, decimal>> pricePoints= new List<Tuple<int, decimal>> discountRanges();

    public Pricing()
    {
        // These hard coded values would be replaced by logic to load from file.
        pricePoints.Add(Tuple.Create(7499, 5300));
        pricePoints.Add(Tuple.Create(7399, 5250));
        pricePoints.Add(Tuple.Create(7349, 5200));
        pricePoints.Add(Tuple.Create(7249, 5150));
        . . .
        pricePoints.Add(Tuple.Create(1049, 1000));
    }

    public decimal GetDiscount(QuoteData quoteData)
    {
        var price = TotalChapter7(quoteData);
        foreach(var point in pricePoints)
        {
            if(price >= point.Item1)
                return point.Item2;
        }
        // If we're here it implies there were no matching points
        return 0;
    }
}
公共类定价
{
private List pricePoints=新建列表折扣更改();
公开定价()
{
//这些硬编码值将被从文件加载的逻辑替换。
Add(Tuple.Create(74995300));
Add(Tuple.Create(73995250));
Add(Tuple.Create(73495200));
Add(Tuple.Create(72495150));
. . .
Add(Tuple.Create(10491000));
}
公共折扣(QuoteData QuoteData)
{
var价格=第7章总计(报价数据);
foreach(价格点中的var点)
{
如果(价格>=点项目1)
返回点1.2项;
}
//如果我们在这里,这意味着没有匹配点
返回0;
}
}
如果您在代码中构建删除,并且它们更改了删除,那么您必须更改代码。
将数据放入一个文件中,在运行时加载该文件一次,它们可以更改价格,而您只需更改该文件

反驳“这显然是一条商业规则”评论

不包括销售点的折扣(2:1优惠,此商品10%折扣等),基本上有三种方法可以计算总体收费或费用的折扣(其中任何一种都可以与首选客户折扣相结合)

  • 固定百分比(例如,始终为10%)
  • 不同价格点的不同百分比
  • 不同价位的不同单位价格(这是我们看到的 (问题中)
客户决定使用哪一个(或多个)是业务规则,是的,规则需要在代码中表示


但是,无论使用哪种规则,实际值都是数据,并且该数据永远不应出现在代码中。

分析计算的工作公式如下:

int calcDiscount(int p)
{
  int s = (7500/50) - (p+1) / 50;
  int k = s / 3;
  int j = s % 3;

  return 5300 - 100*k - (j == 2 ? 50 : 0) 
}
工作测试用例(Java):


说明:首先,您可以找到距离最大值(7499)的下降步数如果是当前价格,那么你知道你必须每三步将折扣值降低100,但是如果你是当前三元组的最后一步,你必须将折扣值再降低50。

你们这些家伙真的太复杂了。任何试图用整数算法解决这个问题的方法都是一个坏主意。看看这对你来说有多困难一群非常聪明的人(我们都非常聪明,不是吗?)甚至在一开始就把事情做好。这真的很难发现,很难理解,很难做对,而且很难维持

你需要一种易于理解、易于维护的方法。看看你原来的帖子,你对规则有一个英文描述

然而,每100次的回报(或“折扣价格”)保持不变(两次50%的费用下降),然后一次回报(一次50%的费用下降)下降50%,然后重复

代码实际上是这样写的:

public int GetFeeFromQuoteData(QuoteData quoteData) {
    int fee = 5300;
    int difference = 7449 - TotalChapter7(quoteData);
    bool isTwoStep = true;

    while (difference > 0) {
        if (isTwoStep) {
            difference -= 50;
        }
        else {
            difference -= 100;
        }
        fee -= 50;
        isTwoStep = !isTwoStep;
    }
    return fee;
}

我不知道这是否适用于您,但您可以使用字典,按照上面的示例中的降序对键进行排序
public int GetFeeFromQuoteData(QuoteData quoteData) {
    int fee = 5300;
    int difference = 7449 - TotalChapter7(quoteData);
    bool isTwoStep = true;

    while (difference > 0) {
        if (isTwoStep) {
            difference -= 50;
        }
        else {
            difference -= 100;
        }
        fee -= 50;
        isTwoStep = !isTwoStep;
    }
    return fee;
}
public int GetNumber(int value)
{
    //initialize a dictionary to hold pairs of numbers
    var ranges = new SortedDictionary<int, int>
    {
        { 25, 250 },
        { 50, 500 },
        { 75, 750 }
    };

    //sort the dictionary in descending order and return the first value
    //that satisfies the condition
    return ranges.OrderByDescending(p => p.Key)
        .FirstOrDefault(p => value >= p.Key).Value;
}