Menu
Coddy logo textTech

Recap - Payment Processor

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 62 of 110.

challenge icon

Challenge

Easy

Let's build a payment processing system that demonstrates the power of polymorphism! You'll create an abstract payment method class and several concrete implementations, then process a collection of different payment types through a unified interface.

You'll organize your code into two files:

  • payments.dart: Define your payment hierarchy here:
    • An abstract class PaymentMethod with a String accountHolder property, a constructor, and an abstract method processPayment(double amount)
    • A CreditCard class that extends PaymentMethod with an additional String cardNumber property (last 4 digits only). Its processPayment should print Processing $[amount] via Credit Card ****[cardNumber] for [accountHolder]
    • A PayPal class that extends PaymentMethod with an additional String email property. Its processPayment should print Processing $[amount] via PayPal ([email]) for [accountHolder]
    • A BankTransfer class that extends PaymentMethod with an additional String bankName property. Its processPayment should print Processing $[amount] via Bank Transfer from [bankName] for [accountHolder]
    Also create a function processAllPayments(List<PaymentMethod> methods, double amount) that:
    • Prints Processing [count] payments of $[amount] each: where count is the number of payment methods
    • Iterates through each payment method and calls processPayment(amount)
    • After processing all payments, counts how many are CreditCard types using the is operator and prints Credit card payments: [count]
  • main.dart: Import your payments file and demonstrate the payment processor:
    • Create a List<PaymentMethod> containing:
      • A CreditCard for 'Alice Smith' with card number '1234'
      • A PayPal for 'Bob Jones' with email 'bob@email.com'
      • A BankTransfer for 'Carol White' with bank 'National Bank'
      • A CreditCard for 'David Brown' with card number '5678'
    • Call processAllPayments with this list and amount 99.99

Notice how the processAllPayments function works with the abstract PaymentMethod type, yet each payment processes according to its specific implementation. The type checking at the end demonstrates how you can still identify specific types when needed.

Expected output:

Processing 4 payments of $99.99 each:
Processing $99.99 via Credit Card ****1234 for Alice Smith
Processing $99.99 via PayPal (bob@email.com) for Bob Jones
Processing $99.99 via Bank Transfer from National Bank for Carol White
Processing $99.99 via Credit Card ****5678 for David Brown
Credit card payments: 2

Try it yourself

import 'payments.dart';

void main() {
  // TODO: Create a List<PaymentMethod> containing:
  // - CreditCard for 'Alice Smith' with card number '1234'
  // - PayPal for 'Bob Jones' with email 'bob@email.com'
  // - BankTransfer for 'Carol White' with bank 'National Bank'
  // - CreditCard for 'David Brown' with card number '5678'
  
  // TODO: Call processAllPayments with the list and amount 99.99
}

All lessons in Object Oriented Programming