Menu
Coddy logo textTech

Recap - Bank Account Manager

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

challenge icon

Challenge

Easy

Let'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 a BankAccount class that manages individual accounts and tracks overall bank statistics. Your class should include:
    • A final String accountNumber that cannot change after creation
    • A String holderName for the account holder's name
    • A private double _balance field starting at 0.0
    • A static int totalAccounts starting at 0 to track how many accounts exist
    • A static double totalDeposits starting at 0.0 to track all deposits made across the bank
    • A constructor that takes accountNumber and holderName, and increments totalAccounts
    • A getter balance that returns the current balance
    • A deposit(double amount) method that adds to the balance only if the amount is positive, and also adds to totalDeposits when 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
  • 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.0 into Alice's account
    • Deposit 300.0 into Bob's account
    • Withdraw 150.0 from Alice's account
    • Try to withdraw 1000.0 from Bob's account (should be ignored - insufficient funds)
    • Try to deposit -50.0 into 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()

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.0

Try 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