A flexible and extensible checkout system implemented in Elixir, designed to handle dynamic pricing rules while maintaining simplicity and reliability. This project was developed using Test-Driven Development (TDD) methodology.
- Evaluate Programming Style: Demonstrate clean, maintainable, and idiomatic Elixir code.
- Showcase Elixir Expertise: Build a robust and flexible solution.
- Test-Driven Development: Ensure code reliability with comprehensive unit tests.
- Add Items to Cart:
- Dynamically add products to a shopping cart.
- Calculate Total:
- Display the total price based on the items in the cart.
- Customizable Pricing Rules:
- Flexible rules to meet stakeholder requirements:
- Buy-One-Get-One-Free: Applies to "Green Tea (GR1)".
- Bulk Discount: For "Strawberries (SR1)", price drops to £4.50 each if buying 3 or more.
- Coffee Discount: For "Coffee (CF1)", price drops to two-thirds the original price if buying 3 or more.
- Flexible rules to meet stakeholder requirements:
| Basket | Expected Total Price |
|---|---|
GR1, SR1, GR1, GR1, CF1 |
£22.45 |
GR1, GR1 |
£3.11 |
SR1, SR1, GR1, SR1 |
£16.61 |
GR1, CF1, SR1, CF1, CF1 |
£30.57 |
- Language: Elixir
- Testing Framework: ExUnit
- Database: Not required (in-memory state for simplicity)
git clone https://github.com/imahmedismail/supermarket.git
cd supermarketmix test- Start an Elixir interactive session:
iex -S mix- Use the Checkout module to scan items and calculate totals:
alias Supermarket.Checkout
checkout = Checkout.new()
checkout = Checkout.scan(checkout, "GR1")
checkout = Checkout.scan(checkout, "SR1")
checkout = Checkout.scan(checkout, "GR1")
checkout = Checkout.scan(checkout, "GR1")
checkout = Checkout.scan(checkout, "CF1")
IO.puts("Total: £#{Checkout.total(checkout)}")A Postman collection is included to test the checkout system's functionality with the following scenarios:
-
Basket: GR1, SR1, GR1, GR1, CF1
Expected Total Price: £22.45
-
Basket: GR1, GR1
Expected Total Price: £3.11
-
Basket: SR1, SR1, GR1, SR1
Expected Total Price: £16.61
-
Basket: GR1, CF1, SR1, CF1, CF1
Expected Total Price: £30.57
To import and use the collection:
- Just import this file supermarket_checkout.postman_collection.json from project-directory to Postman
- Test the endpoints with the predefined requests.
Contributions are welcome! Feel free to submit issues or create pull requests to improve this project.