Recap - Bank Account Manager
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 22 of 110.
Challenge
EasyLet's build a bank account management system that brings together all the class property concepts you've learned in this chapter. You'll create a system that tracks individual account data while also maintaining bank-wide statistics.
You'll organize your code into two files:
bank_account.dart: Define aBankAccountclass that manages individual accounts and tracks overall bank statistics. Your class should include:- A
final String accountNumberthat cannot change after creation - A
String holderNamefor the account holder's name - A private
double _balancefield starting at0.0 - A
static int totalAccountsstarting at0to track how many accounts exist - A
static double totalDepositsstarting at0.0to track all deposits made across the bank - A constructor that takes
accountNumberandholderName, and incrementstotalAccounts - A getter
balancethat returns the current balance - A
deposit(double amount)method that adds to the balance only if the amount is positive, and also adds tototalDepositswhen successful - A
withdraw(double amount)method that subtracts from the balance only if the amount is positive and doesn't exceed the current balance - A
displayInfo()method that prints the account details - A static method
getBankStats()that returns a string with the bank-wide statistics
- A
main.dart: Import your bank account class and demonstrate the system:- Create an account with number
'ACC001'for'Alice' - Create an account with number
'ACC002'for'Bob' - Deposit
500.0into Alice's account - Deposit
300.0into Bob's account - Withdraw
150.0from Alice's account - Try to withdraw
1000.0from Bob's account (should be ignored - insufficient funds) - Try to deposit
-50.0into Alice's account (should be ignored - negative amount) - Call
displayInfo()on Alice's account, then Bob's account - Print the bank stats using
BankAccount.getBankStats()
- Create an account with number
The displayInfo() method should print in this format:
Account [accountNumber] | Holder: [holderName] | Balance: $[balance]The getBankStats() method should return:
Bank Stats - Total Accounts: [totalAccounts] | Total Deposits: $[totalDeposits]Expected output:
Account ACC001 | Holder: Alice | Balance: $350.0
Account ACC002 | Holder: Bob | Balance: $300.0
Bank Stats - Total Accounts: 2 | Total Deposits: $800.0Try it yourself
import 'bank_account.dart';
void main() {
// TODO: Create an account with number 'ACC001' for 'Alice'
// TODO: Create an account with number 'ACC002' for 'Bob'
// TODO: Deposit 500.0 into Alice's account
// TODO: Deposit 300.0 into Bob's account
// TODO: Withdraw 150.0 from Alice's account
// TODO: Try to withdraw 1000.0 from Bob's account (should be ignored)
// TODO: Try to deposit -50.0 into Alice's account (should be ignored)
// TODO: Call displayInfo() on Alice's account
// TODO: Call displayInfo() on Bob's account
// TODO: Print the bank stats using BankAccount.getBankStats()
}
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