Recap - Payment Processor
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 62 of 110.
Challenge
EasyLet'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
PaymentMethodwith aString accountHolderproperty, a constructor, and an abstract methodprocessPayment(double amount) - A
CreditCardclass that extendsPaymentMethodwith an additionalString cardNumberproperty (last 4 digits only). ItsprocessPaymentshould printProcessing $[amount] via Credit Card ****[cardNumber] for [accountHolder] - A
PayPalclass that extendsPaymentMethodwith an additionalString emailproperty. ItsprocessPaymentshould printProcessing $[amount] via PayPal ([email]) for [accountHolder] - A
BankTransferclass that extendsPaymentMethodwith an additionalString bankNameproperty. ItsprocessPaymentshould printProcessing $[amount] via Bank Transfer from [bankName] for [accountHolder]
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
CreditCardtypes using theisoperator and printsCredit card payments: [count]
- An abstract class
main.dart: Import your payments file and demonstrate the payment processor:- Create a
List<PaymentMethod>containing:- A
CreditCardfor'Alice Smith'with card number'1234' - A
PayPalfor'Bob Jones'with email'bob@email.com' - A
BankTransferfor'Carol White'with bank'National Bank' - A
CreditCardfor'David Brown'with card number'5678'
- A
- Call
processAllPaymentswith this list and amount99.99
- Create a
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: 2Try 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
1Fundamentals of OOP
External FilesLibraries & ImportsIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsInstance VariablesConstructor BasicsRecap - Simple Calculator4Null Safety
Intro to Null SafetyNullable vs Non-NullableThe ? and ! OperatorsLate Keyword & Null SafetyNull-Aware OperatorsNull Safety in ClassesRecap - User Profile System7Abstract Classes & Interfaces
Abstract ClassesAbstract MethodsInterfaces in DartImplicit InterfacesImplementing vs ExtendingMultiple InterfacesRecap - Shape Calculator10Collections & Generics
List, Set, Map OverviewType-Safe CollectionsGeneric ClassesGeneric MethodsGeneric ConstraintsIterable & IteratorRecap - Generic Storage13Advanced OOP Concepts
Composition vs InheritanceExtension MethodsCallable ClassesSealed Classes (Dart 3)Records (Dart 3)Patterns & Matching (3.0)Enums with Methods16Project: Library Management
Project OverviewBook and User Classes2Constructors in Dart
Default ConstructorNamed ConstructorsInitializer ListsConstant ConstructorsFactory ConstructorsRedirecting ConstructorsRecap - Shape Builder5Encapsulation
Public vs Private MembersThe _ Prefix ConventionLibrary-Level PrivacyGetters & Setters DepthInformation HidingRecap - Student Records8Mixins
Introduction to MixinsCreating MixinsUsing Multiple Mixinson Keyword in MixinsMixin vs InheritanceMixin vs InterfaceRecap - Animal System11Special Methods
toString() OverridehashCode & == OverrideComparable Interfacecall() MethodnoSuchMethod OverrideRecap - Custom Collection14Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern3Class Properties
Instance vs Static MembersFinal & Const FieldsLate VariablesStatic Methods & FieldsGetters and SettersRecap - Bank Account Manager6Inheritance
Basic InheritanceThe super KeywordMethod OverridingThe @override AnnotationThe final Class KeywordConstructors & InheritanceRecap - Employee Hierarchy9Polymorphism
Polymorphism BasicsPolymorphism via InterfacesRuntime Type CheckingThe is & as OperatorsCovariant KeywordRecap - Payment Processor