价格匹配算法是否使用 LINQ
Usage of LINQ or not for the price match algorithm
我有一个 JSON 输入结构是这样的
"Levels": [
{
"Name": "3 - 50 Points",
"UnitPrice": 855,
"MinValue": 0,
"MaxValue": 51
},
{
"Name": "51 - 100 Points",
"UnitPrice": 800,
"MinValue": 51,
"MaxValue": 100
},
{
"Name": "100- 150 Points",
"UnitPrice": 544,
"MinValue": 100,
"MaxValue": 150
},
{
"Name": "150 Points",
"UnitPrice": 504,
"MinValue": 150,
"MaxValue": -1
}
]
我有一个 class: -
public class Level {
public string Name { get; set; }
public decimal UnitPrice { get; set; }
public int MinValue { get; set; }
public int MaxValue { get; set; }
}
现在我的objective是根据代码注释的方法中的示例解释的算法来计算发生的成本
public decimal GetCostOccured (int quantity, int minmaxflag, IEnumerable<Level> levels)
{
// if quantity = 51 AND minmaxflag is 1
//then choose the [UnitPrice] where MaxValue is
//less than or equal to 51 which is 855. so cost is (51 * 855)
//if quantity = 51 AND minmaxflag is 2 then
//choose the [UnitPrice] where MinValue is
//less than or equal to 51 which is 800. so cost is (51 * 800)
}
如何实现?
这应该提供您所描述的输出集:
private static Level GetLevel(int quantity, int minmaxflag, IEnumerable<Level> levels)
{
return minmaxflag == 1
? levels.Last(l => quantity > l.MinValue)
: levels.Last(l => quantity >= l.MinValue);
}
public static decimal GetCostOccured(int quantity, int minmaxflag,
IEnumerable<Level> levels)
{
return GetLevel(quantity, minmaxflag, levels).UnitPrice * quantity;
}
据我所知,minmaxflag 的工作方式是:
- 1 -> 选择范围
quantity
在 (MinValue, MaxValue]
- 2 -> 选择范围
quantity
在 [MinValue, MaxValue)
换句话说,当minmaxflag
为1时,价格一旦quantity
超过阈值就会下降,而当minmaxflag
为2时,一旦 quantity
达到 阈值,价格就会下降。这听起来正确吗?
我有一个 JSON 输入结构是这样的
"Levels": [
{
"Name": "3 - 50 Points",
"UnitPrice": 855,
"MinValue": 0,
"MaxValue": 51
},
{
"Name": "51 - 100 Points",
"UnitPrice": 800,
"MinValue": 51,
"MaxValue": 100
},
{
"Name": "100- 150 Points",
"UnitPrice": 544,
"MinValue": 100,
"MaxValue": 150
},
{
"Name": "150 Points",
"UnitPrice": 504,
"MinValue": 150,
"MaxValue": -1
}
]
我有一个 class: -
public class Level {
public string Name { get; set; }
public decimal UnitPrice { get; set; }
public int MinValue { get; set; }
public int MaxValue { get; set; }
}
现在我的objective是根据代码注释的方法中的示例解释的算法来计算发生的成本
public decimal GetCostOccured (int quantity, int minmaxflag, IEnumerable<Level> levels)
{
// if quantity = 51 AND minmaxflag is 1
//then choose the [UnitPrice] where MaxValue is
//less than or equal to 51 which is 855. so cost is (51 * 855)
//if quantity = 51 AND minmaxflag is 2 then
//choose the [UnitPrice] where MinValue is
//less than or equal to 51 which is 800. so cost is (51 * 800)
}
如何实现?
这应该提供您所描述的输出集:
private static Level GetLevel(int quantity, int minmaxflag, IEnumerable<Level> levels)
{
return minmaxflag == 1
? levels.Last(l => quantity > l.MinValue)
: levels.Last(l => quantity >= l.MinValue);
}
public static decimal GetCostOccured(int quantity, int minmaxflag,
IEnumerable<Level> levels)
{
return GetLevel(quantity, minmaxflag, levels).UnitPrice * quantity;
}
据我所知,minmaxflag 的工作方式是:
- 1 -> 选择范围
quantity
在 (MinValue, MaxValue] - 2 -> 选择范围
quantity
在 [MinValue, MaxValue)
换句话说,当minmaxflag
为1时,价格一旦quantity
超过阈值就会下降,而当minmaxflag
为2时,一旦 quantity
达到 阈值,价格就会下降。这听起来正确吗?