Simple shopping cart that lets you add items and display the state of the cart
Project description
Instructions for the assignment
- Clone this repository on your machine.
- Use your IDE of choice to complete the assignment.
- When you are finished with the solution and have pushed it to the repo, you can submit the assignment here.
- There is no time limit for this task
Tips on what we’re looking for
We value simplicity as an architectural virtue and as a development practice. Solutions should reflect the difficulty of the assigned task, and shouldn’t be overly complex. We prefer simple, well tested solutions over clever solutions.
DO
- ✅ Include unit tests
- ✅ Test both any client and logic
- ✅ Update the README.md with any relevant information and/or tradeoffs you would like to highlight
DO NOT
- ❌ Submit a web, desktop, command line or any other kind of app
- ❌ Add unnecessary layers of abstraction
- ❌ Add unnecessary patterns/ architectural features that aren’t called for
Begin the task
Create a shopping cart package that facilitates 2 basic capabilities.
-
Add a product to the cart
- Specifying the product name and quantity
- Use the product name to discover the price from the Product API specified below
- Cart state (totals, etc.) must be available
-
Calculate the state:
- Cart subtotal (sum of price for all items)
- Tax payable (charged at 12.5% on the subtotal)
- Total payable (subtotal + tax)
- Totals should be rounded up where required
Product API
Base URL: https://equalexperts.github.io/
View Product: GET /backend-take-home-test-data/{product}.json
Available products
cheerios
cornflakes
frosties
shreddies
weetabix
Example
The below is a sample with the correct values you can use to confirm your calculations
Inputs
- Add 1 × cornflakes @ 2.52 each
- Add another 1 x cornflakes @2.52 each
- Add 1 × weetabix @ 9.98 each
Results
- Cart contains 2 x cornflakes
- Cart contains 1 x weetabix
- Subtotal = 15.02
- Tax = 1.88
- Total = 16.90
Code style
This project makes use of black with default settings to format the code and flake8 as a linter.
Usage
from shopping_cart.shopping_cart import ShoppingCart
cart: ShoppingCart = ShoppingCart()
print(cart.add_to_cart("cornflakes", 1))
print(cart.add_to_cart("cornflakes", 1))
print(cart.add_to_cart("weetabix", 1))
print(cart.calculate_state())
API
add_to_cart(product_name, quantity)
Will add the product details to the cart.
Args:
product_name (str): the name of the product
quantity (int): how many of the product
Raises: Exception: Exception, when there is an error adding a product to the cart
Returns: dict: object containing the status_code and message
calculate_state()
Will display the current state of the shopping cart
Returns: str:
A string representing the state of the shopping cart.
This includes:
product_name: the name of the product
quantity: how many of the product
sub: sum of all the prices of the products
tax: the tax payable on the sub
total: the sum of the sub and tax
Requirements
- python >= 3.10
- poetry
- coverage
Testing
The project uses pytest to run its tests To run the tests, follow the steps below.
Open up a terminal in the project
- run
poetry install
this will install the dependencies for the project - run
poetry shell
to enter the virtual environment - run
poetry run pytest -vv
to run the tests
Coverage
Open up a terminal in the project
- run
coverage run -m pytest
run the test suite and get data - run
coverage report -m
to get a report of the results
Other
The .gitignore file was generated using gitignore.io https://www.toptal.com/developers/gitignore/#
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file shopping_cart_387dd67aa803-0.1.0.tar.gz
.
File metadata
- Download URL: shopping_cart_387dd67aa803-0.1.0.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.5 Darwin/21.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac48256abfea3ac620266d5a103602da3cb9a888535b41208371c12e16f598e2 |
|
MD5 | 17aa70c5a1b0f56729320523b61b644b |
|
BLAKE2b-256 | e665eeb84b5d08474aea05351daa99c6a88e5d8517d4364bd9da9385ba6fd603 |
File details
Details for the file shopping_cart_387dd67aa803-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: shopping_cart_387dd67aa803-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.5 Darwin/21.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc1f0ad2e184e3cdfe91a6c777841d181a280a8259a5d308174fca2d3b7ef5d6 |
|
MD5 | 956ced897e05c9a3b0bef5b2a7246ad3 |
|
BLAKE2b-256 | af09cd14f6d46fddbf5a0f8574cebc7ec9a389fb0fba6cf71295a04033531e3c |