Pricing discount logic, 2 for $4, 6 for the price of 5

246 Views Asked by At

So i'm currently working on some pricing logic where the situations are:

  • 1 glove costs $2.50, 2 gloves costs $4, therefore applying a 20% discount.
  • If person buys 3 gloves then it should be the $4 + original price of $2.50 making total $6.50
  • If person buys 4 gloves then it should be $8.

  • 1 gum costs $0.65, you can buy 6 for the price of 5, so $3.25 for 6 instead of $3.90
  • If person buys 7, then it should be 6 for $3.25 + original price of $0.65 totalling $3.90
  • If person buys 8, then it should be 6 for $3.25 + (original price of $0.65 * 2) totalling $4.55

So it's very important at what index/stage that the discount needs to be applied by

I took my logic inspiration from this article: http://codekata.com/kata/kata01-supermarket-pricing/

I took my code inspiration from this article: https://github.com/raddanesh/Kata01

Specifically the VolumePricingStrategy as show in this diagram:enter image description here

Here is my code attempt given the logic i want to achieve:

//2 for $4
const gloves = {quantity: 3, price: 2.50 }

function volumePriceGloves(threshold = 2){
  let regularPrice = gloves.quantity * gloves.price;
  let volumeDiscount = 0;
    
  let volumePrice = regularPrice * 0.8;

  if(gloves.quantity >= threshold){
    volumeDiscount = threshold * gloves.price - volumePrice
  }
  return regularPrice - volumeDiscount;
}

// 5 for the price of 6
const gum = {quantity: 7, price: 0.65 }

function volumePriceGum(threshold = 6){
  let regularPrice = gum.quantity * gum.price;
  let volumeDiscount = 0;
  
  let volumePrice = regularPrice * 0.16666667;

  if(gum.quantity % threshold){
    volumeDiscount = threshold * gum.price - volumePrice;
  }
  
  return regularPrice - volumeDiscount; 
}

My code is obviously wrong because it outputs the incorrect values when calling the functions. Although I very much like the code written by github.com/raddanesh/Kata01 and can understand the concepts behind it. I'm really struggling to truly understand what volumePrice is representing. In my mind the volumePrice is what the discounted price of that product is given that they meet the threshold.

So for the gloves scenario it makes sense for volumePrice to be regularPrice * 0.8 which represents a 20% discount of the original price. However my intentions don't quite formulate in my code volumePriceGloves(3) returns 6 instead of 6.5 and all the scenarios don't match if you change the threshold parameter amount. Any ideas/help is much appreciated!

For the gum scenario, i'm not quite sure if the code example by github.com/raddanesh/Kata01 can be applied to this logic. As it seems quite different and i'm not sure what approach to really take. As you can see i've made volumePrice on this one regularPrice * 0.16666667, which reflects the discount amount per gum.

Another idea i thought of was potentially putting the prices into an array and popping the last one out given it meets the threshold however i'm not sure how feasible that is i still need that item in the array for further calculations. This one i think is the trickiest and i couldn't find many good examples online, hence this post! All ideas/suggestions are welcome!

2

There are 2 best solutions below

1
TKoL On BEST ANSWER

Both situations require pretty much the same logic -- there's an individual price, and a group price, and any item that doesn't fit in a group gets the individual price.

So my approach would be to just use that as the basis for the logic. Write a function that takes as an input the qty, the group-qty, the individual price and the group price, like so:

function getPrice(qty, groupQty, indivPrice, groupPrice) {
  const groupCount = Math.floor(qty / groupQty);
  const indivCount = qty % groupQty;
  return (groupCount * groupPrice) + (indivCount * indivPrice);
}

console.log(getPrice(5, 2, 1.5, 2.5)); // groups of 2 cost 2.5, but 1 costs 1.5 -- total should be 6.5

So from this point on, all you would need to do is figure out the 'groupPrice' depending on what type of deal it is

3
Chris Norman On

There is a lot of ways to do this. I tend to go the route of less code.

I would go this route.

if(quantity => threshold){

discounteditems = Math.floor(quantity/threhold);
remainder = quantity%threhold;
discountedtotalprice = discounteditems * discountedprice;
fullprice = remainder * fullprice;

}

This way you get all your discounted items, what is outside this threshold you charge the full price.

For me, this is a lot simpler method of getting the desire you are looking for.