2017-02-23 81 views
-1

如何計算剃刀html中的裝運總量。第一件產品的運費爲3.99美元,每增加一件產品運費爲0.99美元。ASP.NET MVC計算裝運總計

@{ 
    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; 
+2

僅供參考,您可能需要考慮將其從視圖移到視圖模型中。這是一條業務規則,所以將它捆綁到您的視圖中意味着您只能使用該視圖。您可以使用視圖助手,但由於這是一個業務規則,它可能屬於模型中的方法或單獨的業務邏輯類/等。然後,控制器可以通過提供所需的值來構建視圖模型,而視圖模型只需顯示那裏的內容,而無需執行任何計算。只是一個建議。 – Dave

回答

0

您可以添加一個簡單的條件,除了你的循環:

@{ 
    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

我對這個說法感到困惑:totalShipping + = totalShipping> shippingBase? shippingItem:shippingBase; –

+0

這是一個「第三級操作員」:https://msdn.microsoft.com/en-us/library/ty67wk28.aspx。它基本上就像是一個「if」語句,但更簡潔。在這個例子中,通過循環(每個項目)的每次迭代,「東西」被添加到'totalShipping'。如果'totalShipping'小於'3.99'(表示這是通過循環的第一次),則'3.99'被添加到'totalShipping'。如果'totalShipping'大於'3.99'(第一項後面的每個附加項),則'shippingItem'('0.99')被添加到'totalShipping'。 –

+0

好吧,我試過了,但它沒有增加0.99當它的第二項 –

1

使用此語句

運費是第一項$ 3.99及每個 0.99 $附加項目。

提取以下數據以創建運費模型。

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

使用來自OP的例子會被填充爲

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

做完這些下面的算法來計算給出一個項目計數運費。

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

以下單元測試驗證算法在計算總運費中的正確性。

[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的運輸成本而不是折扣的小計。這應該很簡單,可以通過計算物品,數量和價格來進行計算。完成後,使用物品數量和費用來計算運費。