ASP.NET MVC calculating Shipping total

前端 未结 2 2048
日久生厌
日久生厌 2021-01-29 09:15

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          


        
相关标签:
2条回答
  • 2021-01-29 09:50

    You can add a simple condition and addition to your loop:

    @{
        double itemTotal = 0;
        double subTotal = 0;
        int totalQty = 0;
        double discount = 0.8;
        double shippingBase = 3.99;
        double shippingItem = 0.99;
        double totalShipping = 0; //used to calculate the cumulative shipping total
    }
    
    @foreach (var item in Model)
    {
    
        double price = (double)item.price / 100 * discount;
        itemTotal = @item.qty * price;
        subTotal += itemTotal;
        totalQty += @item.qty;
        totalShipping += ((totalShipping >= shippingBase) ? shippingItem : shippingBase);
    }
    
    0 讨论(0)
  • 2021-01-29 09:55

    Using this statement

    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; }
    }
    

    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);
    }
    

    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.

    0 讨论(0)
提交回复
热议问题