8000
Skip to content

prasadus92/checkout

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Supermarket Checkout Service

A high-performance, production-ready checkout service for supermarket pricing with support for special offers, bulk discounts, and flexible pricing strategies.

Features

  • Flexible Pricing Strategies: Supports standard pricing and special offers (e.g., "3 for $1.30")
  • RESTful API: Clean, well-documented REST API with OpenAPI/Swagger documentation
  • Production Ready: Health checks, metrics, structured logging, and comprehensive error handling
  • Containerized: Docker and Docker Compose support for easy deployment
  • Fully Tested: Comprehensive unit and integration tests with TestContainers
  • Modern Stack: Spring Boot 3.4, Java 21, MongoDB

Technology Stack

Component Technology
Language Java 21 (LTS)
Framework Spring Boot 3.4
Database MongoDB 7.0
API Docs OpenAPI 3.0 (SpringDoc)
Testing JUnit 5, TestContainers, Mockito
Build Maven 3.9+
Container Docker
CI/CD GitHub Actions

Architecture

src/main/java/com/supermarket/checkout/
├── api/                    # REST Controllers & DTOs
│   ├── dto/               # Request/Response objects
│   ├── CheckoutController # Checkout endpoint
│   └── ItemController     # Item catalog CRUD
├── config/                # Configuration classes
├── domain/                # Domain models
│   ├── entity/           # MongoDB entities
│   └── model/            # Value objects
├── exception/             # Exception handling
├── pricing/               # Pricing strategies
│   ├── PricingEngine     # Strategy selector
│   ├── PricingStrategy   # Strategy interface
│   ├── StandardPricing   # Default pricing
│   └── SpecialOffer      # Bulk discount pricing
├── repository/            # Data access layer
└── service/               # Business logic

Quick Start

Prerequisites

  • Java 21+
  • Maven 3.9+
  • Docker & Docker Compose (for containerized deployment)

Running Locally

  1. Clone the repository

    git clone <repository-url>
    cd checkout
  2. Run with Docker Compose (recommended)

    docker-compose up -d
  3. Or run with Maven (requires local MongoDB)

    ./mvnw spring-boot:run

The service will be available at http://localhost:8080

API Documentation

Once running, access the Swagger UI at:

API Endpoints

Checkout

Calculate the total price for a basket of items.

POST /api/v1/checkout
Content-Type: application/json

{
  "items": ["A", "A", "A", "B", "B", "C", "D"]
}

Response:

{
  "items": [
    {
      "sku": "A",
      "name": "Apple",
      "quantity": 3,
      "unitPrice": 0.50,
      "subtotal": 1.30,
      "specialOfferApplied": true
    }
  ],
  "total": 2.10,
  "totalItems": 7
}

Items Catalog

Method Endpoint Description
GET /api/v1/items List all items
GET /api/v1/items/{sku} Get item by SKU
POST /api/v1/items Create new item
PUT /api/v1/items/{sku} Update item
DELETE /api/v1/items/{sku} Delete item

Health & Metrics

Endpoint Description
/actuator/health Health check
/actuator/info Application info
/actuator/metrics Application metrics
/actuator/prometheus Prometheus metrics

Pricing Rules

Default pricing configuration:

SKU Name Unit Price Special Offer
A Apple $0.50 3 for $1.30
B Banana $0.30 2 for $0.45
C Cherry $0.20 -
D Date $0.15 -

Special Offer Calculation

For items with special offers:

total = (quantity / offerQty) * offerPrice + (quantity % offerQty) * unitPrice

Example: 5 Apples (3 for $1.30, normally $0.50 each)

= (5 / 3) * $1.30 + (5 % 3) * $0.50
= 1 * $1.30 + 2 * $0.50
= $2.30

Configuration

Environment Variables

Variable Description Default
SERVER_PORT Server port 8080
MONGODB_URI MongoDB connection string mongodb://localhost:27017/checkout
SPRING_PROFILES_ACTIVE Active profile default

Profiles

  • default - Development with local MongoDB
  • prod - Production configuration
  • test - Test configuration

Development

Building

# Compile
./mvnw clean compile

# Run tests
./mvnw test

# Run integration tests
./mvnw verify

# Package
./mvnw package

# Build Docker image
./mvnw spring-boot:build-image

Running Tests

# Unit tests only
./mvnw test

# Integration tests only
./mvnw verify -DskipUnitTests

# All tests with coverage
./mvnw verify

# View coverage report
open target/site/jacoco/index.html

Code Quality

The project includes:

  • JaCoCo for code coverage (80% line coverage target)
  • ArchUnit for architecture tests
  • OWASP Dependency Check for vulnerability scanning

Deployment

Docker

# Build image
docker build -t supermarket/checkout-service:latest .

# Run container
docker run -p 8080:8080 \
  -e MONGODB_URI=mongodb://host.docker.internal:27017/checkout \
  supermarket/checkout-service:latest

Docker Compose

# Start all services
docker-compose up -d

# Start with dev tools (includes Mongo Express)
docker-compose --profile dev up -d

# View logs
docker-compose logs -f app

# Stop all services
docker-compose down

Kubernetes

The application is container-ready with:

  • Health check endpoints (/actuator/health/liveness, /actuator/health/readiness)
  • Prometheus metrics endpoint (/actuator/prometheus)
  • Graceful shutdown support
  • Resource-efficient JVM settings

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A high-performance, production-ready checkout service for supermarket pricing with support for special offers, bulk discounts, and flexible pricing strategies.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

0