Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Implement Polymorphic Processing for Different Subclass Types

Design a class hierarchy for payment methods and implement polymorphic fee calculation across subclasses.

Python practice25 minInheritance & PolymorphismAdvancedLast updated April 8, 2026

Problem statement

You are building a small payment-fee calculation module. Implement a base PaymentMethod class that provides a general fee-calculation mechanism and several subclasses that override or extend that behavior: - CreditCard: percentage fee of 2.0%. - PayPal: percentage fee of 2.9% plus a fixed 30-cent surcharge. - BankTransfer: no fee. - LoyaltyCard: behaves like a CreditCard but provides a 50-cent discount on the calculated fee (fee can't go below zero). All fee calculations are in integer cents. The base class should encapsulate the percent-based calculation so subclasses can reuse it (use super() where appropriate). Finally, implement compute_total_fee(transactions) which accepts a list of (payment_method_instance, amount_in_cents) and returns the total fee (int) across all transactions. This exercise focuses on correct use of inheritance, overriding, super(), and writing polymorphic code that calls the same method on different subclass instances.

Task

Use inheritance and polymorphism to implement different fee-calculation behaviors in subclasses, use super() to reuse base logic, and write a function that processes a list of transactions polymorphically.

Examples

Mixed transactions

Input

compute_total_fee([(CreditCard(), 10000), (PayPal(), 2000), (BankTransfer(), 5000)])

Output

288

CreditCard: 2% of 10000 = 200. PayPal: 2.9% of 2000 = 58 + 30 = 88. BankTransfer: 0. Total = 200 + 88 + 0 = 288 cents.

Input format

A list of tuples: (payment_method_instance, amount_in_cents).

Output format

An integer representing the total fee in cents.

Constraints

All amounts and fees are non-negative integers. Use integer arithmetic for fee calculations. Do not print; return the integer value.

Samples

Sample 1

Input

compute_total_fee([(LoyaltyCard(), 10000), (CreditCard(), 5000)])

Output

250

LoyaltyCard: CreditCard fee 200 - 50 = 150. CreditCard for 5000 = 100. Total = 250.