Skip to main content

Refactor the Validator

Challenge on lesson: Checkpoint: Professional Code

static String validateOrder(String id, int cents, String coupon) returns one of "OK", "BAD_ID", "BAD_AMOUNT", "BAD_COUPON" — first failure wins. Rules: - id: non-null, 3..20 chars → else BAD_ID - cents: 1..1_000_000 → else BAD_AMOUNT - coupon: null/empty OK; otherwise must match [A-Z0-9]{4,10} → else BAD_COUPON Implement it CLEANLY: extract static boolean isValidId(String id), static boolean isValidAmount(int cents), and static boolean isValidCoupon(String coupon) — validateOrder calls the three helpers, no duplicated rule logic, guard-style.

Difficulty: intermediate

Back to lesson: Checkpoint: Professional Code