AlgoMaster Logo
AlgoMasterImplement a Legacy Payment Adaptereasy

Implement a Legacy Payment Adapter

easy

A checkout application already depends on a simple payment contract: provide an order id and an amount in whole dollars. A legacy gateway is still reliable, but it accepts cents, puts the amount before the reference, and requires an explicit capture flag. The application should not learn those details.

Your task is deliberately focused: implement only LegacyPaymentAdapter. The PaymentProcessor contract, LegacyGateway, and CheckoutDesk test harness are already implemented in the starter code.

The adapter must behave as follows:

  • LegacyPaymentAdapter(gateway) stores the supplied LegacyGateway.
  • It implements PaymentProcessor, exposing pay(orderId, dollars).
  • pay converts dollars to cents and calls gateway.makeCharge(dollars * 100, orderId, true).
  • It returns the gateway's string unchanged.

The gateway returns "Legacy -> <reference>: <cents> cents (capture=<flag>)" and counts each call. The provided desk validates amounts, counts valid payments, and formats receipts. A negative amount returns "INVALID" before the adapter is called. Zero dollars is a valid payment.

Do not rewrite the contract, gateway, or desk. The examples call CheckoutDesk because it is the public test harness; your code changes belong only inside the adapter.

Example 1:

Input:

Output:

Explanation: The adapter converts 25 dollars to 2500 cents and supplies the order id and capture flag in the shape required by the gateway.

Example 2:

Input:

Output:

Explanation: receipt uses the same payment workflow exactly once, so both counters finish at 1.

Constraints

  • 1 <= orderId.length <= 30
  • Order ids contain letters, digits, and hyphens only.
  • -1 <= dollars <= 10000
  • At most 100 calls in total are made across all methods.

Starter Code

Only LegacyPaymentAdapter is unfinished. Define the complete adapter at its marked location. Every other type is already implemented; do not rewrite it.

How the design is graded

needs 7/10 to pass
  • Target contract and composition

    Full marks when `LegacyPaymentAdapter` implements `PaymentProcessor` and stores the exact `LegacyGateway` supplied to its constructor. Lose points heavily when it extends the gateway, creates a new gateway per payment, or changes the target method signature.

  • Correct request translation

    Full marks when `pay(orderId, dollars)` delegates as `makeCharge(dollars * 100, orderId, true)` and returns the gateway result unchanged. Lose points for passing dollars as cents, swapping fields incorrectly, using `false`, or constructing the result inside the adapter.

  • Focused adapter scope

    Full marks when validation, receipt formatting, and counts remain in the supplied classes while the adapter only translates and delegates. Lose points when the adapter rejects amounts, tracks payments, formats receipts, or prints 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 CheckoutDesk()null
pay("ORD-7", 25)"Legacy -> ORD-7: 2500 cents (capture=true)"
paymentCount()1
gatewayCallCount()1

The adapter converts 25 dollars to 2500 cents, reorders the arguments for the legacy API, and requests immediate capture.

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