문제

I have a table where I store the tiered pricing information for the products in the following schema

| ProductId   | MinimumQty  | MaximumQty    | Price |       
|-------------|------------ |------------   |-------|   
| 1           | 1           | 19            | 5.49  |       
| 1           | 20          | 99            | 4.79  |       
| 1           | 100         | 499           | 4.19  |       
| 1           | 500         | 9999          | 3.49  |        

If a customer adds this product like say x times to the cart then i need to let him know that by adding x more he can save y dollars.

That is if he adds 18 (18*5.49 = $98.82) of these to the cart then I should let the customer know that by adding 2 more (20*4.79=$95.8) to the cart he can save (98.82 - 95.8) = $2.88.

Below is my class

public class Product{
  public int ProductID { get; set; }
  public List<ProductPrice> ProductPrice{ get; set; }
}

public class ProductPrice{
  public int ProductID { get; set; }
  public int MinQty { get; set; }
  public int MaxQty { get; set; }
  public double Price { get; set; }
}

public class ProductSaving{
    public int QtyToAdd{ get; set; }
    public double Savings { get; set; }
}

public ProductSaving CalculateProductSavings(Product product,int qtyInCart){
 var productSaving = new ProductSaving();

 ****Help Needed!!****

  return productSaving;
 }

Thanks for the help.

도움이 되었습니까?

해결책

// find the current tier pricing of the product
var tierProductPrice = product.ProductPrice.FirstOrDefault(p => p.MinQty >= qtyInCart && p.MaxQty <= qtyInCart);

if (tierProductPrice != null)
{
 var maxTierQty = tierProductPrice.MaxQty;    
 var tierPrice = tierProductPrice.Price;

 // current price of the Cart
 var cartPrice = tierPrice * qtyInCart;

 // find next min price Tier
 var nextTierProductPrice = product.ProductPrice.FirstOrDefault(p => p.MinQty >= maxTierQty && p.MaxQty <= maxTierQty);

 if (nextTierProductPrice  != null)
 {
  var itemsToAdd = nextTierProductPrice.MinQty - qtyInCart;

  // calculate new price of the cart
  var newPrice = nextTierProductPrice.MinQty * nextTierProductPrice.Price;

  if (newPrice < cartPrice)
  {
   productSaving = new ProductSaving
   {
    QtyToAdd = itemsToAdd,
    Savings = cartPrice - newPrice
   };
  }
 } 
}

note that the above logic assumes that the immediate next tier is the only tier that is going to be profitable for the user for adding enough items to reach that tier. from the data it looks like it.

i.e. if i have 18 items currently in my cart, only the 20-99 tier may possibly result in a better deal. if 20-99 could not yield a better deal, then the tiers above 99 are never going to yield a better deal.

let us know if that assumption is not right. the code logic to determine nextTierProductPrice will go into a loop till we hit a better deal OR deplete all tiers.

i haven't put the logic unnecessarily until needed.

다른 팁

I assume you are wondering about the logic, not the code itself:

  • Calculate the price with the current quantity (select price where quantity is more than minimum quantity and less than maximum quantity)
  • Get the next price category (next category where the minimum quantity is more than current quantity) and calculate the total price based on minimum quantity and the price
  • Reduce the first from the second (of course it makes sense to show the savings value only if it's positive; if the customer is actually saving something)

Having a maximum quantity in the table is actually redundant since it's always one less the next minimum quantity, but it might make the calculations easier.

This code should do what you need. It will return a zero'ed out ProductSaving object if either there is no better deal or the quantity in cart value does not fit into any of the ProductPrice ranges (this logic can be easily changed to suit your needs):

public ProductSaving CalculateProductSavings(Product product, int qtyInCart)
{
    var productSaving = new ProductSaving();

    var rangeProduct = product.ProductPrice.FirstOrDefault(z => qtyInCart >= z.MinQty && qtyInCart <= z.MaxQty);

    // Return empty saving if quantity is not within range
    if (rangeProduct == null)
        return productSaving;

    var nextProduct = product.ProductPrice.FirstOrDefault(z => z.MinQty >= (rangeProduct.MaxQty + 1));

    // Return empty saving if there are no better savings
    if (nextProduct == null)
        return productSaving;

    productSaving.QtyToAdd = nextProduct.MinQty - qtyInCart;
    productSaving.Savings = (rangeProduct.Price * qtyInCart) - (nextProduct.Price * nextProduct.MinQty);
    return productSaving;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top