AlgoMaster Logo
AlgoMasterRefactor a Price Bookeasy

Refactor a Price Book

easy

PriceBook is the preimplemented driver for tax, shipping, totals, and receipts. It delegates all pricing work to PriceRules, which is the class you will refactor.

The starter PriceRules is a working legacy implementation of the original rules. It deliberately repeats the tax formula in taxOn, totalFor, and receipt. A new requirement has now arrived:

Tax must be rounded to the nearest cent before any total or receipt uses it. For nonnegative amounts, a third decimal of 5 or more rounds up.

Refactor PriceRules so this rule has one authoritative implementation. Do not modify PriceBook or any public method signature. Preserve all existing behavior except where the new rounding requirement changes a result.

  • PriceBook() creates a price book with a tax rate of 10 percent.
  • boolean setTaxPercent(int percent) sets the rate and returns true. A percent below 0 or above 100 changes nothing and returns false.
  • int taxPercent() returns the current rate.
  • double taxOn(double amount) returns the tax, rounded to the nearest cent.
  • double totalFor(double amount) returns the amount plus its rounded tax plus a flat 5.00 shipping fee.
  • String receipt(double amount) returns "net <amount>, tax <tax>, ship <shipping>, total <total>", with every figure formatted to two decimals and no thousands separators.

The goal is not to patch the same rounding expression into all three copies. After the refactor, changing the tax rule again should require editing only one place.

Example 1:

Input:

Output:

Explanation: The raw tax is 1.999. The new rule rounds it to 2.00 before totalFor or receipt uses it.

Example 2:

Input:

Output:

Explanation: The current rate and the rounding rule flow through the same tax calculation, so every result stays consistent.

Constraints

  • 0 <= amount <= 10^6
  • -1000 <= percent <= 1000
  • At most 100 calls will be made across all methods.

Starter Code

The starter is intentionally repetitive. Refactor PriceRules; the provided PriceBook driver must remain unchanged.

How the design is graded

needs 7/10 to pass
  • One authoritative tax rule

    Full marks when multiplication by the rate and cent rounding appear in exactly one method, and totalFor and receipt delegate to that behavior. Lose points heavily when the tax formula or rounding rule remains duplicated.

  • Behavior-preserving refactor

    Full marks when the provided PriceBook driver, public API, rate validation, shipping fee and receipt format remain unchanged apart from the new rounded-tax requirement. Lose points when refactoring changes unrelated behavior or stores derived totals.

  • One source for related values

    Full marks when the shipping fee is a single named constant and receipt reuses the total calculation. Lose points for repeated shipping literals, repeated total arithmetic, locale-dependent formatting or printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new PriceBook()null
taxPercent()10
taxOn(19.99)2
totalFor(19.99)26.99
receipt(19.99)"net 19.99, tax 2.00, ship 5.00, total 26.99"

The raw tax is 1.999, but the new rule rounds it to 2.00 before the total and receipt use it.

Run checks these cases. Submit also runs a larger hidden set.