This issue covers initial thoughts on how to find the best combination of prices along different supermarkets given a Product
list.
Let us begin with a simple list containing 3 products.
Each product, for simplicity reasons on this example will have only:
name
prices
Our list of products shall contain the following:
[{
"name": "Natural Mineral Water",
"prices": [{
"store_id": 1,
"price": 1.20
}, {
"store_id": 2,
"price": 1.18
}, {
"store_id": 3,
"price": 0.95
}, {
"store_id": 4,
"price": 1.10
}]
},
{
"name": "Lacta Chocolate",
"prices": [{
"store_id": 1,
"price": 1.35
}, {
"store_id": 2,
"price": 1.50
}, {
"store_id": 3,
"price": 1.20
}, {
"store_id": 4,
"price": 1.49
}]
},
{
"name": "DELTA Fresh Milk 1.5LT",
"prices": [{
"store_id": 1,
"price": 0.88
}, {
"store_id": 2,
"price": 1.10
}, {
"store_id": 3,
"price": 0.95
}, {
"store_id": 4,
"price": 1.25
}]
}]
The algorithm starts by iterating through each product and finding the lowest price of it.
The store that had the lowest receives a bonus.
So for instance, on the first product, store with id 3 has the lowest price. So it receives a bonus.
For now we have the following statistics on the store bonuses:
Store 1 bonus = 0
Store 2 bonus = 0
Store 3 bonus = 1
Store 4 bonus = 0
Next product is iterated, and lowest price is found on store with id 3. That store gets a bonus.
Last, but not least, we have the following statistics on the store bonuses:
Store 1 bonus = 0
Store 2 bonus = 0
Store 3 bonus = 2
Store 4 bonus = 0
Last product on this example is iterated, lowest price is found on store with id 1. That store gets a bonus.
Finally, we have the following statistics on the store bonuses:
Store 1 bonus = 1
Store 2 bonus = 0
Store 3 bonus = 2
Store 4 bonus = 0
At the end of the iteration, find the store that has the received the biggest bonus.
TO-DO
Calculate the final price * quantity
of a product before finding the lowest price of that on each store.
Though, this can be done after calculating the lowest total of the whole basket.
What I mean by that?
First find which store offers the lowest overall prices of the basket products. Then, after having found that, calculate the quantity * price
of each product and return the results.