Skip to content

About

A god-class order processor refactored into tested, separated components (Strategy pattern, BigDecimal money, no silent tax guessing). Git history shows the before/after.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

java-legacy-refactor

A small order-pricing module, refactored from a single untested "god class" into separated, tested components. The git history tells the story directly: the first commit is the legacy baseline, the second is the full refactor. git diff between them shows the transformation.

The legacy version (first commit)

One OrderProcessor class did everything: computed the subtotal (twice, redundantly), applied discount rules through nested if/else on string-typed customer types, looked up tax rates through another if/else chain, computed shipping, and printed a receipt to System.out -- all inside a single method, with no tests.

It also had a real, easy-to-miss bug: it used double for money. Running it with Widget x2 @ $19.99, Gadget x1 @ $45.00 produced:

Subtotal: $84.97999999999999

That's floating-point representation error, not a rounding choice -- the kind of bug that looks fine in a demo and causes penny-off reconciliation issues in production.

// legacy OrderProcessor.processOrder(...) -- see git history for the full method
double subtotal = 0;
for (int i = 0; i < items.size(); i++) {
    subtotal = subtotal + (prices.get(i) * quantities.get(i));
}
double discount = 0;
if (customerType.equals("VIP")) {
    if (isMember) {
        if (subtotal > 100) { discount = subtotal * 0.20; }
        else { discount = subtotal * 0.15; }
    } else { /* ... more nesting ... */ }
} else if (customerType.equals("REGULAR")) { /* ... */ }

What changed, and why

Problem Fix
double for money loses precision BigDecimal everywhere; tests assert on it with compareTo, not equals(), since BigDecimal.equals() treats 150.0 and 150.00 as different values -- a gotcha worth knowing
One class doing pricing and printing Split into OrderProcessor (pure calculation, returns an OrderTotal) and ReceiptFormatter (formatting only) -- calculation is now usable and testable without capturing stdout
Nested if/else on string customer types DiscountPolicy interface (Strategy pattern) with one implementation per CustomerType, wired through DiscountPolicyFactory
if/else chain of hardcoded state tax rates TaxCalculator backed by a Map<String, BigDecimal>
Unknown state silently taxed at a guessed 5% Throws IllegalArgumentException instead -- a silently-wrong tax rate is worse than a loud failure
Parallel lists (items, prices, quantities) LineItem and Order domain records, each validating its own invariants (no blank names, no negative prices, no empty orders)
Subtotal computed twice in two different loops Single Order.subtotal(), used everywhere
Zero tests 23 tests across every component: domain validation, each discount tier, tax lookup + the unknown-state failure path, shipping thresholds, and a full end-to-end pricing scenario

Running it

mvn test    # 23 tests
mvn compile && java -cp target/classes com.echaro.orders.Main   # demo receipt

Project structure

src/main/java/com/echaro/orders/
  CustomerType.java
  LineItem.java, Order.java              # domain model
  OrderProcessor.java                    # orchestration only, no I/O
  ReceiptFormatter.java                  # I/O concern, separated out
  pricing/
    DiscountPolicy.java (+ Vip/Regular/Guest impls, Factory)
    TaxCalculator.java
    ShippingCalculator.java
    OrderTotal.java                      # result object instead of a side-effect
src/test/java/...                        # mirrors main, 23 tests total

About

A god-class order processor refactored into tested, separated components (Strategy pattern, BigDecimal money, no silent tax guessing). Git history shows the before/after.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages