Add shipping cost to checkout

Node · Node · intermediate · greenfield

Implements shipping cost calculation with a clean, extensible strategy architecture: a registry of pluggable strategies behind a common interface, so future shipping methods can be added without touching the calculation logic.

Requirements

Files touched

--- src/checkout/shipping.js
+const STRATEGY_REGISTRY = new Map();
+
+function registerStrategy(name, strategy) {
+  STRATEGY_REGISTRY.set(name, strategy);
+}
+
+class ShippingStrategy {
+  calculate() {
+    throw new Error("calculate() must be implemented");
+  }
+}
+
+class FlatRateStrategy extends ShippingStrategy {
+  calculate() {
+    return 5.99;
+  }
+}
+
+class ExpressStrategy extends ShippingStrategy {
+  calculate(order) {
+    return order.total > 100 ? 12.99 : 19.99;
+  }
+}
+
+registerStrategy("flat", new FlatRateStrategy());
+registerStrategy("express", new ExpressStrategy());
+
+function shippingCost(method, order) {
+  const strategy = STRATEGY_REGISTRY.get(method);

Review this PR

Node practice