ApexSensei

Drills Classes & objects

Reject an invalid discount

A custom exception gives a business rule its own type.

Your task

Make a discount policy throw a custom exception for percentages outside 0 to 50, and print the error message when it does.

class DiscountPolicy {
    public Decimal apply(Decimal amount, Decimal percent) {
        return null;
    }
}

Requirements

  1. throw new DiscountRangeException should be present
  2. catch (DiscountRangeException should be present
  3. The error message should be built from the percent value
  4. Valid: 150.00 should be printed
  5. Exactly 50: 100.00 should be printed
  6. Over limit: 51% exceeds 50% should be printed
  7. Below zero: -5% is below 0% should be printed

Sign in to open this drill