通过距离范围动态计算价格

时间:2018-09-17 13:01:41

标签: c# .net math

我有一个案例来计算出租车的价格,所以我将解释建议

  • 0公里到10公里的价格为2$每公里。
  • 10公里到50公里的价格为1.5$每公里。
  • 50公里到100公里的价格为1.25$每公里。
  • 每公里100公里1$公里。

所以我需要计算总价格

我有这段代码,但是我需要更灵活的实现。

decimal price = 0;
//convert Distance from meters to Km
decimal DistanceKM = 35;

//==========================
//  ID   RangeKM   Pricing  
//   1      0         2$   
//   2     10         1.5$  
//   3     50         1.25$
//   4    100         1$
//==========================

List<Price> Prices = Prices
  .Where(x => x.RangeKM < DistanceKM)
  .OrderBy(x => x.RangeKM)
  .ToList();

for (int i = 0; i < Prices.Count; i++)
{
    decimal ss = Prices[i].Pricing * ((i + 1 < Prices.Count)
      ? Math.Min(DistanceKM, Prices[i + 1].RangeKM - Prices[i].RangeKM) 
      : DistanceKM);

    price += ss;

    DistanceKM -= (i + 1 < Prices.Count)
      ? Math.Min(DistanceKM, Prices[i + 1].RangeKM - Prices[i].RangeKM) 
      : DistanceKM;
} 

示例:

if DistanceKM   8km then the price =  (8*2$) = 16$  
if DistanceKM  35km then the price = ((10*2$) + (25*1.5$)) = 57.5$  
if DistanceKM  55km then the price = ((10*2$) + (40*1.5$) + (5*1.25$)) = 86.25$ 
if DistanceKM 120km then the price = ((10*2$) + (40*1.5$) + (50*1.25) + (20*1$)) = 162.5$  

2 个答案:

答案 0 :(得分:2)

让我们的提取 Price方法:

// Prices: Key - distance (in km); Value - price
private static Dictionary<decimal, decimal> defaultPrices = 
  new Dictionary<decimal, decimal>() {
    { 100m, 1.00m},
    {  50m, 1.25m},
    {  10m, 1.50m},
    {   0m, 2.00m},
};

private static decimal Price(decimal distance, 
  IDictionary<decimal, decimal> policy = null) {

  // if no price policy provided, use default one
  if (null == policy)
    policy = defaultPrices;

  decimal result = 0.00m;

  while (distance > policy.Keys.Min()) {
    var pair = policy
      .Where(item => distance > item.Key)
      .OrderByDescending(item => item.Key)
      .First();

    result += (distance - pair.Key) * pair.Value;
    distance = pair.Key;
  }

  return result;
}

然后我们可以轻松使用它,例如让我们计算tolal的总和:

  List<decimal> distances = ...

  // Alas, we can't Sum() decimals, let's Aggregate them
  decimal total = distances.Aggregate(0.00m, (s, d) => s + Price(d));  

演示:

  decimal[] tests = new decimal[] {
    8, 35, 60, 120
  };

  string report = string.Join(Environment.NewLine, tests
    .Select(d => $"{d,3} km costs {Price(d),6} $"));

  Console.WriteLine(report);

  string reportTotal = $"Total: {tests.Aggregate(0.00m, (s, d) => s + Price(d))} $";

  Console.WriteLine();
  Console.WriteLine(reportTotal);

结果:

  8 km costs  16.00 $
 35 km costs  57.50 $
 60 km costs  92.50 $
120 km costs 162.50 $

Total: 328.50 $

请注意,60公里的费用为10 * 1.25$ + 40 * 1.50$ + 10 * 2.00$ == 92.50$,而不是问题中的86.25$

答案 1 :(得分:0)

如果您将价格范围的另一端添加到价格:

    //================================
    //= ID   StartKM  EndKM  Pricing  
    //=  1      0      10        2$   
    //=  2     10      50      1.5$  
    //=  3     50     100     1.25$
    //=  4    100     MAX        1$
    //===============================

然后您可以像这样计算总价:

        price = prices.Where(x => d > x.StartKM)
                .Sum(x => (Math.Min(d, x.EndKM) - x.StartKM) * x.Pricing);


  8 km costs   16,0 $
 35 km costs   57,5 $
 60 km costs  92,50 $
120 km costs 162,50 $

Total: 328,50 $