Skip to the content.
package com.bookshop.behavioral.chainofresponsibility;

import com.bookshop.domain.Book;
import java.math.BigDecimal;

/** Flags suspiciously large first orders from brand-new customers. */
public class FraudCheck extends OrderCheck {

    private static final BigDecimal NEW_CUSTOMER_LIMIT = new BigDecimal("500.00");

    @Override
    protected ValidationResult check(OrderRequest request) {
        BigDecimal total = request.books().stream()
                .map(Book::price)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        if (request.customer().loyaltyYears() == 0 && total.compareTo(NEW_CUSTOMER_LIMIT) > 0) {
            return ValidationResult.rejected("fraud", "large first order needs manual review");
        }
        return ValidationResult.ok();
    }
}

Raw file