This project implements a flexible supermarket checkout system that applies various pricing rules. It includes a service layer for processing checkout operations and unit tests to ensure correctness.
Currently, we have only three products in the database:
Product Code | Name | Price |
---|---|---|
FR1 | Fruit Tea | £3.11 |
SR1 | Strawberries | £5.00 |
CF1 | Coffee | £11.23 |
- Buy One Get One Free: Applies to specific products. (Currently configured for product code 'FR1' as you will see in the config below)
- Bulk Discount: Applies discounts when a minimum quantity of a product is purchased. (Currently configured for product code 'SR1'- when you buy 3 you get one free- as you will see in the config below)
- Flexible Pricing Rules: Configurable via a PHP configuration file.
-
Clone the repository:
cd supermarket-checkout
-
Install dependencies:
composer install
-
Setup environment:
Copy the example environment file adjust the configuration as needed ie setup the db creds:
cp .env.example .env
-
Run database migrations and seeders:
php artisan migrate --seed
The pricing rules are configured in config/pricing_rules.php
:
return [
'rules' => [
[
'class' => \App\Services\PricingRules\BuyOneGetOneFreeRule::class,
'params' => [
['FR1'] // array of product_codes on which discount is to be applied on
],
],
[
'class' => \App\Services\PricingRules\BulkDiscountRule::class,
'params' => [
['SR1'], // array of product_codes on which discount is to be applied on
3, // units
4.50], //discount
],
],
];
curl -X POST http://yourdomain.com/api/checkout \
-H "Content-Type: application/json" \
-d '{"products":[{"code":"FR1"},{"code":"SR1"}]}'
{
"total": 10.50
}
{
"error": "Failed to process product: FR1"
}
After setting up the PHPUnit.xml file run the following command
php artisan test --filter Feature or --filter Unit
Currently, the coverage is 100%!
This project is open-sourced software licensed under the MIT license.
Osmar Rodrigues (https://github.com/Reaper1994)