ASP.NET MVC计算运输总计 [英] ASP.NET MVC calculating Shipping total

查看:57
本文介绍了ASP.NET MVC计算运输总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用razor html计算运输总量.运费为第一件商品$ 3.99,其他每件商品$ 0.99.

How do I calculate the shipping total in razor html. Shipping charges are $3.99 for the first item and $.99 for each additional item.

 @{
     double itemTotal = 0;
     double subTotal = 0;
     int totalQty = 0;
     double discount = 0.8;
     double shippingBase = 3.99;
     double shippingItem = 0.99;

 }

    @foreach (var item in Model)
    {

        double price = (double)item.price / 100 * discount;
        itemTotal = @item.qty * price;
        subTotal += itemTotal;
        totalQty += @item.qty;

推荐答案

使用此语句

第一件商品的运费为3.99美元,每件商品的运费为0.99美元 其他项目.

Shipping charges are $3.99 for the first item and $.99 for each additional item.

提取以下数据以创建运费模型.

the following data was extracted to create a shipping charge model.

public class ShippingCharge {
    public decimal basePrice { get; set; }
    public int baseCount { get; set; }
    public decimal unitPrice { get; set; }
}

使用OP中的示例填充为

Which using the example from the OP would be populated as

//Shipping charges are 
shippingCharge = new ShippingCharge() {
    // $3.99 
    basePrice = 3.99M,
    //for the first item 
    baseCount = 1,
    // $.99 for each additional item. 
    unitPrice = 0.99M
};

完成此操作后,将使用以下算法来计算给定项目数的运费.

With that done the following algorithm was used to calculate the shipping charge given an item count.

decimal? CalculateShippingTotal(int itemCount, ShippingCharge charge) {
    decimal? total = null;
    if (charge != null) {
        var basePrice = charge.basePrice;
        var baseCount = charge.baseCount;

        if (itemCount > baseCount) {
            var qtyDifference = itemCount - baseCount;
            var additionalCost = qtyDifference * charge.unitPrice;

            total = basePrice + additionalCost;
        } else {
            total = itemCount * basePrice;
        }
    }
    return total;
}

以下单元测试验证了该算法在计算总运费中的正确性.

The following unit tests verify the correctness of the algorithm in calculating total shipping charges.

[TestMethod]
public void _TotalShipping_For_One_Item() {
    //Arrange
    var totalQty = 1;
    var expected = 3.99M;

    //Act
    var actual = CalculateShippingTotal(totalQty, shippingCharge);

    //Assert
    actual.ShouldBeEquivalentTo(expected);
}

[TestMethod]
public void _TotalShipping_For_Two_Items() {
    //Arrange
    var totalQty = 2;
    var expected = 4.98M;

    //Act
    var actual = CalculateShippingTotal(totalQty, shippingCharge);

    //Assert
    actual.ShouldBeEquivalentTo(expected);
}

[TestMethod]
public void _TotalShipping_For_Three_Items() {
    //Arrange
    var totalQty = 3;
    var expected = 5.97M;

    //Act
    var actual = CalculateShippingTotal(totalQty, shippingCharge);

    //Assert
    actual.ShouldBeEquivalentTo(expected);
}

此答案的目标是具体如何根据OP而不是带折扣的小计来计算运输成本.这应该足够简单,您可以通过计算项目,数量和价格来进行计算.完成后,使用项目计数和费用来计算运输成本.

This answer target specifically how to calculate the shipping cost based on OP not the subtotal with discount. That should be simple enough for you to calculate by tallying the items, quantities and prices. Once done use item count and charges to calculate the shipping costs.

这篇关于ASP.NET MVC计算运输总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆