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.
| Command | Behavior | Output |
|---|---|---|
["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.
6 pay card 50 pay paypal 120 pay crypto 300 pay card 75 pay wire 200 pay paypal 40
card: charged 50 paypal: sent 120 crypto: transferred 300 card: charged 75 Unknown method: wire paypal: sent 40
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.