我是编程和学习C#的新手,我有点卡住了。我试图设置两个固定费率和一个可变费率参考以下内容:
5磅或以下的运费为3.50美元,20磅或以下的运费为10美元, [例如:10磅重量:运费是10美元。 4磅重量:3.5 $] 重量超过20磅,每磅9.5加$ 10c。 (两种统一费率和一种可变利率)
这就是我现在所拥有的(它一团糟):
//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");
答案 0 :(得分:1)
必须为航运公司做同样的事情。以下是满足要求的方式。
用于存储费率的模型。
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 });
以上显示了原始示例中费率的要求。 weight
和basePrice
应该是自我解释的。正如你所看到的那样,有两种平衡率,重量可达5磅和20磅。如果需要,baseWeight
,unitPrice
和unitWeight
用于计算可变权重的成本。
用于根据总重量和可用费率计算费率的算法
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);
}
如果费率更改,则使用新值更新集合。