.NET Fiddle

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using Motiv;

var now = DateTime.Parse("2022-01-01T12:00:00Z");
var product = new IPhone12(now);
var competitorsProducts = new CompetitorsProducts();
var orderContext = new OrderContext(product, new User("John", [product]), now);

// Define discounts policies
var isInStockFor90daysDiscountPolicy =
	Spec.Build((OrderContext ctx) => ctx.Product.DateInStock < ctx.Now.AddDays(-90))
		.WhenTrue(0.05m)
		.WhenFalse(0m)
		.Create("the product has been in stock for 90 days");

var isNightOwlTimeDiscountPolicy =
	Spec.Build((OrderContext ctx) => ctx.Now.Hour is >= 1 and < 5)
		.WhenTrue(0.1m)
		.WhenFalse(0m)
		.Create("it is night owl time");

var isPreviouslyAbandonedProductDiscountPolicy =
	Spec.Build((OrderContext ctx) => ctx.User.AbandonedCartProducts.Contains(ctx.Product))
		.WhenTrue(0.02m)
		.WhenFalse(0m)
		.Create("the product was previously in an abandoned cart");

var isLowStockDiscountPolicy =
	Spec.Build((OrderContext ctx) => ctx.Product.WarehouseStockLevel < 10)
		.WhenTrue(-0.2m)
		.WhenFalse(0m)
		.Create("there are ten or fewer items in stock");

var isPriceMatchPolicy =
	Spec.Build((OrderContext ctx) => competitorsProducts.TryGetValue(ctx.Product.Id, out var product) 
			   ? ctx.Product.Price > product.Price
			   : false)
		.WhenTrue(ctx => competitorsProducts[ctx.Product.Id].Price)
		.WhenFalse(ctx => ctx.Product.Price)
		.Create("is a competitor's price match");
		
var isRegularDiscountPolicy =
	Spec.Build(isInStockFor90daysDiscountPolicy
            | isNightOwlTimeDiscountPolicy
            | isPreviouslyAbandonedProductDiscountPolicy
            | isLowStockDiscountPolicy)
		.WhenTrue((ctx, result) => ctx.Product.Price * (1 - result.Values.Sum()))
		.WhenFalse(ctx => ctx.Product.Price)
		.Create("a discount is applied");

// create business rule
var isAdjustedPriceRule = isPriceMatchPolicy.OrElse(isRegularDiscountPolicy);

// evaluate rule
var adjustment = isAdjustedPriceRule.IsSatisfiedBy(orderContext);

// output results
Console.WriteLine($"Old price {orderContext.Product.Price:F2}, new price {adjustment.Value:F2}");
foreach (var assertion in adjustment.RootAssertions)
{	
	Console.WriteLine($"  -  {assertion}");
}


/****** Model Definitions ******/
public record OrderContext(InventoryPricedProduct Product, User User, DateTime Now);

public record PricedProduct(string Brand, string Model) : Product(Brand, Model)
{
    public decimal Price { get; init; }
}

public record ProductId(string Brand, string Model);public record Product(string Brand, string Model)
{
    public DateTime? ExpireDate { get; init; }

    public ProductId Id { get; } = new(Brand, Model);
}

public record User(string Name, HashSet<Product> AbandonedCartProducts);

public class CompetitorsProducts() : Dictionary<ProductId, PricedProduct>(new Dictionary<ProductId, PricedProduct>());
	
public record InventoryPricedProduct(string Brand, string Model)
    : PricedProduct(Brand, Model)
{
    public int WarehouseStockLevel { get; init; }

    public int InStoreStockLevel { get; init; }
    public DateTime DateInStock { get; init; }
}

public record IPhone12 : InventoryPricedProduct
{
	public IPhone12(DateTime now) : base("Apple", "iPhone 12")
	{
		Price = 899.99m;
		InStoreStockLevel = 5;
		WarehouseStockLevel = 10;
		DateInStock = now.AddDays(-100);
	}
}
;