我正在尝试根据可用的供应量创建价格。例如,如果我们的商品库存为4000万美元,基价为20美元,那么当供应量每增加0.125%时,价格就会下降0.06%,每0.125%的变化增加0.1%。供应量下降。
到目前为止,我能够提出这个......
public void AddDailyCrudeOil() {
CrudeOilSupplyRussia += DailyCrudeOilAmountRussia;
/*
* Every 0.125% increase in supply, has a 0.06% decrease in the price.
* Conversely, any 0.125% decrease in supply, has a 0.1% increase in price.
* At our base supply, 40M, 0.125% will be 50K, and the condition will be true as our daily supply delivery is 100k
* */
if (DailyCrudeOilAmountRussia > CrudeOilSupplyRussia * 0.00125f) {
CrudeOilPriceRussia *= -0.0006f;
}
}
但是,当然,这只会工作一次而不是递增。
我一直坚持这个问题的时间超过了我想承认的时间,并且非常欢迎任何指导。
答案 0 :(得分:0)
这样的东西?写作NUnit测试并使用"应该"对于断言,这传递了。注意我使用小数来避免任何奇怪的舍入问题。
[TestFixture]
public class tmp
{
[Test]
public void test()
{
var baseAmount = 40000000M;
var basePrice = 100;
var percentIncreaseTrigger = 0.00125M;
var percentDecreaseInPrice = 0.0006M;
GetNewPrice(baseAmount, basePrice, percentIncreaseTrigger,
percentDecreaseInPrice, 40050000)
.ShouldBe(99.94M);
GetNewPrice(baseAmount, basePrice, percentIncreaseTrigger,
percentDecreaseInPrice, 40100000)
.ShouldBe(99.88M);
GetNewPrice(baseAmount, basePrice, percentIncreaseTrigger,
percentDecreaseInPrice, 40400000)
.ShouldBe(99.52M);
}
private decimal GetNewPrice(decimal baseAmount, decimal basePrice,
decimal trigger, decimal decreaseBy, decimal newAmount)
{
//work out what the trigger % is as an absolute amount
var triggerAmount = baseAmount * trigger;
//work out the price decrease as an absolute amount of the base price
var priceDecreaseAmount = basePrice * decreaseBy;
//work out how much over the baseAmount we are
var amountOver = newAmount - baseAmount;
//work out how many times we can fit the 0.125% into this -
//ignore fractional parts
var numberOfTriggers = Math.Floor(amountOver / triggerAmount);
//multiply the priceDecrease amount up by this
var totalToTakeOffPrice = priceDecreaseAmount * numberOfTriggers;
var result = basePrice - totalToTakeOffPrice;
return result;
}
}
答案 1 :(得分:0)
我会做一个线性函数,对于任何供应,返回正确的价格。这为您提供了连续的价格功能,每增加0.125%的供应量就没有“步骤”。如果您需要这些步骤,请忽略此答案。
根据您的要求,您可能需要有2个公式。一个供应超过4000万供应,一个供下面供应,因为看起来它们有不同的斜率。但是你真的希望你的价格曲线中有一个“扭结”的供应量达到4000万吗?
static void Main()
{
var price = GetPrice(40050000.0);
Console.WriteLine(price);
}
private static double GetPrice(double supply)
{
if (supply > 40000000.0)
{
double slope = -0.012 / 50000.0; // Decrease price by 0.012 for each 50000 increase in supply
double origin = 20 - (slope * 40000000.0); // Calculate the price at 0 supply given the price at 40 million supply
return origin + supply * slope; // Price at the given supply
}
else
{
double slope = -0.02 / 50000.0; // Decrease price by 0.02 for each 50000 increase in supply
double origin = 20 - (slope * 40000000.0); // Calculate the price at 0 supply given the price at 40 million supply
return origin + supply * slope; // Price at the given supply
}
}
答案 2 :(得分:0)
这个应该做的伎俩
public double practicalCase(double actualStock, double baseStock, double percentChange, double basePrice,
double priceDecrease, double priceIncrease)
{
double changeAmount = baseStock * percentChange;
double priceChanges = Math.Floor(Math.Abs(baseStock - actualStock) / changeAmount);
if (actualStock > baseStock)
return (basePrice - (priceDecrease * priceChanges));
else
return (basePrice + (priceIncrease * priceChanges));
}
尝试以下案例
[Fact]
public void TestPracticalCase()
{
Assert.Equal(100d, _propertyManager.practicalCase(actualStock: 40000000d, baseStock: 40000000d, percentChange: 0.00125d,
basePrice: 100d, priceDecrease: 0.06d, priceIncrease: 0.1d));
Assert.Equal(99.94d, _propertyManager.practicalCase(actualStock: 40050000d, baseStock: 40000000d, percentChange: 0.00125d,
basePrice: 100d, priceDecrease: 0.06d, priceIncrease: 0.1d));
Assert.Equal(101d, _propertyManager.practicalCase(actualStock: 39500000d, baseStock: 40000000d, percentChange: 0.00125d,
basePrice: 100d, priceDecrease: 0.06d, priceIncrease: 0.1d));
}