Facebook Pixel

Exercise: Abstraction & Interfaces

This exercise puts the abstraction and interfaces lesson into code.

Scenario

A checkout service supports three payment methods: card, PayPal, and crypto. Each method is a concrete class that implements a common PaymentProcessor interface. A dispatcher holds a map from method name to processor instance and routes each incoming command to the correct implementation. The dispatcher returns the provider's confirmation string without knowing or caring which concrete class handled the call.

Commands

The program reads one command per line and prints one line of output per command.

CommandBehaviorOutput
["pay", "card", amount]Route to CardProcessor"card: charged <amount>"
["pay", "paypal", amount]Route to PayPalProcessor"paypal: sent <amount>"
["pay", "crypto", amount]Route to CryptoProcessor"crypto: transferred <amount>"
["pay", <unknown>, amount]No matching processor"Unknown method: <unknown>"

Amounts are integers.

Example
Input
6
pay card 50
pay paypal 120
pay crypto 300
pay card 75
pay wire 200
pay paypal 40
Output
card: charged 50
paypal: sent 120
crypto: transferred 300
card: charged 75
Unknown method: wire
paypal: sent 40
Explanation
The first four commands route to `CardProcessor`, `PayPalProcessor`, `CryptoProcessor`, and `CardProcessor` in sequence, each returning its confirmation string. The `pay wire` command has no matching processor in the map, so the dispatcher returns the unknown-method message. The final `pay paypal` routes normally.

Your task

Create the CardProcessor, PayPalProcessor, and CryptoProcessor classes — each implements PaymentProcessor and returns its own confirmation string from process(amount) — then run the tests. The dispatcher below already registers CardProcessor(), PayPalProcessor(), and CryptoProcessor() by name, so the code tells you exactly what to build.

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro