Banking System
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 108 of 110.
Challenge
EasyLet's build a Banking System that models real-world financial operations using the OOP principles you've mastered throughout this course. You'll create accounts with different rules, track transactions, and manage customers - all organized across multiple files.
Your banking system will be organized across six files:
transaction.dart: Create aTransactionclass that represents an immutable record of a financial operation. Each transaction hasfinalfields fortype(String - "deposit", "withdrawal", or "transfer"),amount(double), andtimestamp(DateTime). OverridetoString()to return[type] $amount.account.dart: Create an abstractAccountclass with a private_balance(double), afinalaccount number (accountNumber- String), and aList<Transaction>to store transaction history. Include a getter for balance, a protected setterset balance(double value)so subclasses in other files can update the balance, an abstract methodbool canWithdraw(double amount)that subclasses implement, and these concrete methods:deposit(double amount)- adds to balance, records a transaction, printsDeposited $amount. Balance: $balancewithdraw(double amount)- ifcanWithdraw()returns true, subtracts from balance, records a transaction, printsWithdrew $amount. Balance: $balance. Otherwise printsWithdrawal denied.getTransactionCount()- returns the number of transactions
savings_account.dart: Create aSavingsAccountthat extendsAccount. Savings accounts have a withdrawal limit - they cannot withdraw more than $500 in a single transaction. ImplementcanWithdraw()to enforce this rule (also ensure sufficient balance). Add a methodapplyInterest(double rate)that increases the balance by the given percentage rate using thebalancegetter and setter, then printsInterest applied. Balance: $balance.checking_account.dart: Create aCheckingAccountthat extendsAccount. Checking accounts have overdraft protection up to $100 - they can go negative but not below -$100. ImplementcanWithdraw()to allow withdrawals as long as the resulting balance stays at or above -$100.customer.dart: Create aCustomerclass with anid(String),name(String), and aList<Account>for their accounts. Include methodsaddAccount(Account account)to add an account, andgetTotalBalance()that returns the sum of all account balances. OverridetoString()to returnCustomer: name (X accounts).main.dart: Demonstrate your banking system. Create a customer (C001,John Smith). Create aSavingsAccount(SAV001, starting balance $1000) and aCheckingAccount(CHK001, starting balance $500). Add both accounts to the customer and print the customer. Then perform these operations:- Deposit $200 to the savings account
- Withdraw $300 from the savings account (should succeed)
- Try to withdraw $600 from the savings account (should be denied - exceeds limit)
- Apply 5% interest to the savings account
- Withdraw $550 from the checking account (should succeed using overdraft)
- Try to withdraw $100 more from checking (should be denied - would exceed overdraft limit)
Savings transactions: XandTotal balance: $Y(formatted to 2 decimal places).
Expected output:
Customer: John Smith (2 accounts)
Deposited $200.0. Balance: $1200.0
Withdrew $300.0. Balance: $900.0
Withdrawal denied
Interest applied. Balance: $945.0
Withdrew $550.0. Balance: $-50.0
Withdrawal denied
Savings transactions: 3
Total balance: $895.0Try it yourself
import 'transaction.dart';
import 'account.dart';
import 'savings_account.dart';
import 'checking_account.dart';
import 'customer.dart';
void main() {
// TODO: Create a customer with id "C001" and name "John Smith"
// TODO: Create a SavingsAccount with accountNumber "SAV001" and starting balance $1000
// TODO: Create a CheckingAccount with accountNumber "CHK001" and starting balance $500
// TODO: Add both accounts to the customer
// TODO: Print the customer
// TODO: Perform the following operations:
// 1. Deposit $200 to the savings account
// 2. Withdraw $300 from the savings account (should succeed)
// 3. Try to withdraw $600 from the savings account (should be denied - exceeds limit)
// 4. Apply 5% interest to the savings account
// 5. Withdraw $550 from the checking account (should succeed using overdraft)
// 6. Try to withdraw $100 more from checking (should be denied - would exceed overdraft limit)
// TODO: Print "Savings transactions: X" where X is the transaction count
// TODO: Print "Total balance: \$Y" where Y is formatted to 2 decimal places
}
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