Skip to content

Repository files navigation

Shopping Cart Implementation Full Solution

The solution implements:

  • Adding products by product name and quantity
  • Retrieving product prices from the provided Price API
  • Aggregating quantities for the same product
  • Calculating subtotal, tax, and total payable
  • Exposing cart state through a clean API

Design Decisions

  • ShoppingCart handles business logic
  • PriceStrategy abstracts price retrieval
  • ApiPriceStrategy integrates with external API
  • TaxStrategy encapsulates tax calculations

Patterns Used

  • Strategy pattern used only where behavior may vary (price retrieval, tax calculation)

External API Handling

  • Uses Java HttpClient

  • Includes:

    • 5-second request timeout
    • HTTP status code validation
    • handling for invalid/missing price data
  • Non-200 responses and invalid payloads throw PriceRetrievalException.

  • Unit Test Case Business logic tested using mocked PriceStrategy

Covers:

  • Cart calculations
  • Product aggregation
  • Input validation (null/blank product name, invalid quantity)
  • Edge cases (empty cart, unmodifiable map)
  • Tax calculation
  • Integration ApiPriceStrategyTest uses real API calls for validation

Monetary data

  • Monetary data use BigDecimal and rounded using RoundingMode.HALF_UP

Assumptions and Tradeoffs

  • Product names must match those supported by the Price API
  • Valid API response expected to contain price field.
  • assumes single-threaded usage
  • No logs or Sysouts.

Tech Stack

Java 21 Gradle JUnit 5 Gson Java HttpClient

AI Tool Usage

  • Reviewing implementation against assignment requirements
  • Identifying missing edge cases and improving test coverage
  • Refining exception handling for the Price API client
  • Improving README clarity and structure

Percentage of Code Generated by AI

Approximately 30–40%.

How AI-Generated Code Was Reviewed

  • Manually reviewed
  • Refactored for readability and maintainability

Test Run

./gradlew test

ORIGINAL REQUIREMENT

⚠️ Please read these instructions carefully and entirely first

  • Clone this repository to your local machine.
  • Use your IDE of choice to complete the assignment.
  • When you have completed the assignment, you need to push your code to this repository and mark the assignment as completed by clicking here.
  • Once you mark it as completed, your access to this repository will be revoked. Please make sure that you have completed the assignment and pushed all code from your local machine to this repository before you click the link.
  • There is no time limit for this task - however, for guidance, it is expected to typically take around 1-2 hours.

Begin the task

Write some code that provides the following basic shopping cart capabilities:

  1. Add a product to the cart

    1. Specifying the product name and quantity
    2. Retrieve the product price by issuing a request to the Price API specified below
    3. Cart state (totals, etc.) must be available
  2. Calculate the state:

    1. Cart subtotal (sum of price for all items)
    2. Tax payable (charged at 12.5% on the subtotal)
    3. Total payable (subtotal + tax)
    4. Totals should be rounded up where required (to two decimal places)

Price API

The price API is an existing API that returns the price details for a product, identified by it's name. The shopping cart should integrate with the price API to retrieve product prices.

Price API Service Details

Base URL: https://equalexperts.github.io/

View Product: GET /backend-take-home-test-data/{product}.json

List of 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

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, assumptions, and/or tradeoffs you would like to highlight.

DO NOT

  • ❌ Submit any form of app, such as web APIs, browser, desktop, or command-line applications.
  • ❌ Add unnecessary layers of abstraction.
  • ❌ Add unnecessary patterns/ architectural features that aren’t called for e.g. persistent storage.

AI Tool Usage

While we encourage the use of AI tools as part of the learning process but to ensure transparency, please provide the following information regarding the use of AI tools in this submission:

  1. Specific Use Cases: Describe for what purposes tool was used (e.g., code generation, debugging assistance, query generation etc.).
  2. Percentage of Code Generated by AI: Provide an estimate of the percentage of the submitted code that was generated by AI.
  3. How AI-Generated Code Was Reviewed: Explain how you reviewed and verified the AI-generated code to ensure its correctness and quality.

Please note, during the technical interview, which will build upon this exercise, we'll focus on your coding abilities and problem-solving skills without the use of AI tools. This will allow us to see your direct approach and thought process.

About

Shopping Cart Design LLD

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages