One interface, three payments
After this: Write the PaymentMethod interface and three classes that keep its promise, and read them as a class diagram.
On this page · 6 sections · 3 checks
In lesson 1 the checkout asked each payment "what are you?" and then did the work itself. Turn that around. Write down what every kind of payment can do, and let each kind do it in its own way.
What every payment can do
Look at card, UPI and wallet together. Each one can answer two questions:
- What is your fee on this amount?
- Please pay this amount: what happened?
Those two questions are all the checkout needs. A list of methods that several classes promise to have, with no code inside, is an . Ours is called PaymentMethod, and it lists two methods: fee(amount) and pay(amount).
The two languages write the promise differently:
- In Java,
interface PaymentMethodlists the two methods, and each class saysimplements PaymentMethod. - Python has no
interfaceword. The usual way to write one is an (ABC for short): a class built onABC, with each promised method marked@abstractmethodand a body of..., which means "no code here". Lesson 4 shows what these lines buy you.
Three classes, one promise
Each kind of payment is now its own class, with its own rule inside:
| Class | fee(amount) | pay(amount) says |
|---|---|---|
Card | 2% of the amount | card charged and the amount plus the fee |
Upi | 0 | UPI paid and the amount |
Wallet(balance) | 1% of the amount | paid, with what is left; or refused when the total is more than the balance |
Here is the same design as a . Types are written the Java way (int, String), as everywhere in this learning path.
- Card implements PaymentMethod
- Upi implements PaymentMethod
- Wallet implements PaymentMethod
The interface box says «interface» above its name. Each class has a dashed line to it with a hollow triangle at the interface's end. Read that line as "can do": a card can do everything a PaymentMethod can do.
The code
from abc import ABC, abstractmethod
class PaymentMethod(ABC): @abstractmethod def fee(self, amount): ...
@abstractmethod def pay(self, amount): ...
class Card(PaymentMethod): def fee(self, amount): return amount * 2 // 100
def pay(self, amount): return f"card charged {amount + self.fee(amount)}"
class Upi(PaymentMethod): def fee(self, amount): return 0
def pay(self, amount): return f"UPI paid {amount}"
class Wallet(PaymentMethod): def __init__(self, balance): self.balance = balance
def fee(self, amount): return amount * 1 // 100
def pay(self, amount): total = amount + self.fee(amount) if total > self.balance: return f"wallet refused: needs {total}, has {self.balance}" self.balance -= total return f"wallet paid {total}, left {self.balance}"
u = Upi()print(u.fee(1250), u.pay(1250))w = Wallet(2000)print(w.fee(1250))print(w.pay(1250))0 UPI paid 1250 12 wallet paid 1262, left 738
interface PaymentMethod { int fee(int amount); String pay(int amount);}
class Card implements PaymentMethod { public int fee(int amount) { return amount * 2 / 100; }
public String pay(int amount) { return "card charged " + (amount + fee(amount)); }}
class Upi implements PaymentMethod { public int fee(int amount) { return 0; }
public String pay(int amount) { return "UPI paid " + amount; }}
class Wallet implements PaymentMethod { private int balance;
Wallet(int balance) { this.balance = balance; }
public int fee(int amount) { return amount * 1 / 100; }
public String pay(int amount) { int total = amount + fee(amount); if (total > balance) { return "wallet refused: needs " + total + ", has " + balance; } balance -= total; return "wallet paid " + total + ", left " + balance; }}
public class Main { public static void main(String[] args) { Upi u = new Upi(); System.out.println(u.fee(1250) + " " + u.pay(1250)); Wallet w = new Wallet(2000); System.out.println(w.fee(1250)); System.out.println(w.pay(1250)); }}0 UPI paid 1250 12 wallet paid 1262, left 738
The interface comes first, then one class per kind. Below the classes, the program uses two of them:
u.fee(1250)is 0 andu.pay(1250)saysUPI paid 1250. Both are printed on one line.- A wallet with 2000 is asked for its fee on 1250. 1% of 1250 is 12.5, and the division drops the .5, so it returns
12. w.pay(1250)works out the total, 1250 plus 12 is 1262. 1262 is not more than 2000, so the wallet takes it off its balance and sayswallet paid 1262, left 738.
Look at Wallet's pay. It calls its own fee to work out the total, then checks the balance before it changes anything. When the money is not there, it returns the refusal and the balance stays as it was.
Run it
Change the wallet's 2000 to 1000 and run it again. The total, 1262, is now more than the balance, and the last line becomes a refusal.
Check yourself
What does the PaymentMethod interface promise about every class that implements it?
A card pays for an order of 1250. What does this print?
from abc import ABC, abstractmethod
class PaymentMethod(ABC): @abstractmethod def fee(self, amount): ...
@abstractmethod def pay(self, amount): ...
class Card(PaymentMethod): def fee(self, amount): return amount * 2 // 100
def pay(self, amount): return f"card charged {amount + self.fee(amount)}"
class Upi(PaymentMethod): def fee(self, amount): return 0
def pay(self, amount): return f"UPI paid {amount}"
class Wallet(PaymentMethod): def __init__(self, balance): self.balance = balance
def fee(self, amount): return amount * 1 // 100
def pay(self, amount): total = amount + self.fee(amount) if total > self.balance: return f"wallet refused: needs {total}, has {self.balance}" self.balance -= total return f"wallet paid {total}, left {self.balance}"
c = Card()print(c.pay(1250))interface PaymentMethod { int fee(int amount); String pay(int amount);}
class Card implements PaymentMethod { public int fee(int amount) { return amount * 2 / 100; }
public String pay(int amount) { return "card charged " + (amount + fee(amount)); }}
class Upi implements PaymentMethod { public int fee(int amount) { return 0; }
public String pay(int amount) { return "UPI paid " + amount; }}
class Wallet implements PaymentMethod { private int balance;
Wallet(int balance) { this.balance = balance; }
public int fee(int amount) { return amount * 1 / 100; }
public String pay(int amount) { int total = amount + fee(amount); if (total > balance) { return "wallet refused: needs " + total + ", has " + balance; } balance -= total; return "wallet paid " + total + ", left " + balance; }}
public class Main { public static void main(String[] args) { Card c = new Card(); System.out.println(c.pay(1250)); }}A wallet works out its fee on an order of 1250 with amount * 1 // 100amount * 1 / 100. What does fee(1250)fee(1250) return?
Honesty notes
- Say what an interface is: a list of methods with no code, which several classes promise to have.
- Write PaymentMethod with fee and pay, in Python as an abstract base class and in Java with interface.
- Write Card, Upi and Wallet, each keeping the promise with its own fee and its own pay.
- Read the dashed line with a hollow triangle in a class diagram as "implements", or "can do".