使用两个“平面”计算运费;利率和一个可变利率

本文关键字:利率 一个 两个 平面 计算 | 更新日期: 2023-09-27 18:13:59

我是编程和学习c#的新手,有点卡住了。我正在尝试设置两个固定利率和一个可变利率参考以下内容:

运费为5磅或以下3.50美元,20磅或以下10美元,[示例:10磅重:运费是10美元。]4磅重量:3.5美元]20磅以上,每磅9.5美元加10美分。(两个固定利率和一个浮动利率)

这是我目前的文件(它很乱):

//Declorations
int artDbl, carrDbl, beetDbl;
double resultInt, result1Dbl, result2Dbl,totalresultDbl; 
double artRate = 2.67, carrRate = 1.49, beetRate = .67;
//double discountDbl;
//double discountRate = 0.05;
decimal totalweightDbl;
//double shiprateDbl = 3.50, shiprate1Dbl = 10.00, shiprate2Dbl = 9.50, shiprate3Dbl = 0.10;
decimal totalshipDbl;
//var shipcostVar;
String outputString;    
//Inputs
artDbl = int.Parse(artBox.Text);
carrDbl = int.Parse(carrBox.Text);
beetDbl = int.Parse(beetBox.Text);
    //Results for weight multiplied by rate per pound
resultInt = artDbl * artRate;
result1Dbl = carrDbl * carrRate;
result2Dbl = beetDbl * beetRate;
totalweightDbl = artDbl + carrDbl + beetDbl;
//shipcostVar = totalweightDbl * .10;
// Results for total weight of all three items
totalresultDbl = resultInt + result1Dbl + result2Dbl;   
//Outputs
outputString = totalresultDbl.ToString("C");
textBox4.Text = totalresultDbl.ToString("C");

if (totalweightDbl <= 5)
{ totalshipDbl = 3.50m; }
else if (totalweightDbl <= 20)
{ totalshipDbl = 10.00m; }

//textBox5.Text = totalshipDbl.ToString("C");

使用两个“平面”计算运费;利率和一个可变利率

必须为航运公司做同样的事情。以下是如何满足需求的。

用于存储速率的模型。

public class Rate {
    public int weight { get; set; }
    public Nullable<decimal> basePrice { get; set; }
    public Nullable<int> baseWeight { get; set; }
    public Nullable<decimal> unitPrice { get; set; }
    public Nullable<int> unitWeight { get; set; }
}

从那里的集合可以用来保存从持久化存储加载的速率规则。

rates = new List<Rate>();
rates.Add(new Rate { weight = 5, basePrice = 3.50M });
rates.Add(new Rate { weight = 20, basePrice = 10M });
rates.Add(new Rate { weight = int.MaxValue, basePrice = 9.50M, baseWeight = 20, unitPrice = 0.10M, unitWeight = 1 });

以上为原例中费率的要求。weightbasePrice是不言自明的。正如你所看到的,体重不超过5磅和20磅有两种统一的费率。baseWeightunitPriceunitWeight用于计算变权成本。

基于总权重和可用费率计算费率的算法

public decimal? CalculateRate(int totalweight, IList<Rate> lookup) {
    decimal? result = null;
    var availableRates = lookup.OrderBy(r => r.weight);
    var rate = availableRates.FirstOrDefault(r => totalweight <= r.weight) ?? availableRates.LastOrDefault();
    if (rate != null) {
        if (rate.baseWeight != null) {
            var baseRate = rate.basePrice;
            var weighDiff = totalweight - rate.baseWeight.GetValueOrDefault();
            var weightDiffUnits = (int)Math.Ceiling((double)weighDiff / (double)rate.unitWeight.GetValueOrDefault());
            var pricePerDiff = rate.unitPrice.GetValueOrDefault();
            var weightDiffRate = weightDiffUnits * pricePerDiff;
            result = baseRate + weightDiffRate;
        } else {
            result = rate.basePrice;
        }
    }
    return result;
}

执行了以下单元测试以验证算法

[TestMethod]
public void When_4_Pounds() {
    //Arrange
    var totalWeight = 4;
    var expected = 3.5M;
    //Act
    var actual = CalculateRate(totalWeight, rates);
    //Assert
    Assert.AreEqual(expected, actual);
}
[TestMethod]
public void When_Ten_Pounds() {
    //Arrange
    var totalWeight = 10;
    var expected = 10M;
    //Act
    var actual = CalculateRate(totalWeight, rates);
    //Assert
    Assert.AreEqual(expected, actual);
}
[TestMethod]
public void When_Thirty_Pounds() {
    //Arrange
    var totalWeight = 30;
    var expected = 10.5M;
    //Act
    var actual = CalculateRate(totalWeight, rates);
    //Assert
    Assert.AreEqual(expected, actual);
}
相关文章: