C# 按距离范围动态计算价格
我有一个案例来计算出租车距离的价格,所以我会解释这个提议C# 按距离范围动态计算价格,c#,.net,math,C#,.net,Math,我有一个案例来计算出租车距离的价格,所以我会解释这个提议 从0km到10km的价格为2$每公里 从10km到50km的价格为1.5$ 从50km到100km的价格为1.25$每公里 每公里超过100km1$ 所以我需要计算总价格 我有这个代码,但我需要更灵活的实现 decimal price = 0; //convert Distance from meters to Km decimal DistanceKM = 35; //========================== //
- 从
km到0
km的价格为10
每公里2$
- 从
km到10
km的价格为50
1.5$
- 从
km到50
km的价格为100
每公里1.25$
- 每公里超过
km100
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;
}
让我们提取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;
}
演示:
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 $
结果:
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
km成本10*1.25$+40*1.50$+10*2.00$==92.50$
,而不是问题中的86.25$
。如果您将范围的另一端添加到价格中:
//================================
//= 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 $
你想在一行代码中完成吗?还是没关系?如果代码在一行中,那就太好了。这个问题的主要原因是为了更具可读性和简短,并查看其他想法以获得相同的结果。在上一个示例中,为什么有
+(40*1.5$)
?它不应该是(10*2$)+(50*1.5$)+(60*1.25$)
。第三个例子看起来也是错误的,我需要实现这一点,但要通过动态值而不是静态值来实现database@Engr瑞斯兰:我明白了;让我们概括一下Price
方法