Implement a checkout system that consuming X input should return the subtotal having into consideration special prices.
Item Code | Unit Price | Special Price |
---|---|---|
A | 50 | 3 for 140 |
B | 35 | 2 for 60 |
C | 25 | |
D | 12 |
input:
[
{
code: "A",
quantity: 3
},
{
code: "B",
quantity: 3
},
{
code: "C",
quantity: 1
},
{
code: "D",
quantity: 2
}
]
expected result: 284
checkout() - findProduct() - calculateProductTotal() - calculateShoppingListSubtotal()
checkout(shoppingList)
- Calculate the subtotal of a shopping list having into consideration special prices.
- shoppingList: array of objects.
[
{
code: 'A',
quantity: 3,
}
]
- Subtotal of shopping list.
findProduct(code, index)
- Looks for the product details in pricing list database.
[
{
productCode: 'A',
unitPrice: 50,
specialPrice: {
minQuantity: 3,
bundlePrice: 140,
},
}
]
- code: product code from shopping list.
- index: shopping list product index.
- Product details as an array of objects
calculateProductTotal({ unitPrice, specialPrice }, quantity, index)
- Multiplies product price by quantity after checking if the product has a special price.
- unitPrice: normal price.
- specialPrice: contains minQuantity and bundlePrice proprety.
- quantity: product total units.
- index: shopping list product index.
- Each product total as an integer.
calculateShoppingListSubtotal(shoppingListDetails)
- Add the total of each product in the list.
- shoppingListDetails: contains each product total.
- Each product total added together as an integer.
- Make sure to install dev dependecies -
npm i
- Run test using command -
npm run test
- index.test.js
- Jest docs
- We would have liked the application to consume actual data sources (e.g. json files) like the one provided, rather than being hardcoded with the data-set.
- The ability to create sub-totals would be good to provide, as they would probably be required if we wanted a production checkout system (i.e. Scan half the data, provide a subtotal and then add more to the basket before working out the total).
- The provided solution didn't account for 2 different entries for the same product type, i.e. {code: A, quantity: 2}, {code: A, quantity: 1} results in 150, not 140 as it should.