Menu
Coddy logo textTech

Banking System

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

challenge icon

Challenge

Easy

Let'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 a Transaction class that represents an immutable record of a financial operation. Each transaction has final fields for type (String - "deposit", "withdrawal", or "transfer"), amount (double), and timestamp (DateTime). Override toString() to return [type] $amount.
  • account.dart: Create an abstract Account class with a private _balance (double), a final account number (accountNumber - String), and a List<Transaction> to store transaction history. Include a getter for balance, a protected setter set balance(double value) so subclasses in other files can update the balance, an abstract method bool canWithdraw(double amount) that subclasses implement, and these concrete methods:
    • deposit(double amount) - adds to balance, records a transaction, prints Deposited $amount. Balance: $balance
    • withdraw(double amount) - if canWithdraw() returns true, subtracts from balance, records a transaction, prints Withdrew $amount. Balance: $balance. Otherwise prints Withdrawal denied.
    • getTransactionCount() - returns the number of transactions
  • savings_account.dart: Create a SavingsAccount that extends Account. Savings accounts have a withdrawal limit - they cannot withdraw more than $500 in a single transaction. Implement canWithdraw() to enforce this rule (also ensure sufficient balance). Add a method applyInterest(double rate) that increases the balance by the given percentage rate using the balance getter and setter, then prints Interest applied. Balance: $balance.
  • checking_account.dart: Create a CheckingAccount that extends Account. Checking accounts have overdraft protection up to $100 - they can go negative but not below -$100. Implement canWithdraw() to allow withdrawals as long as the resulting balance stays at or above -$100.
  • customer.dart: Create a Customer class with an id (String), name (String), and a List<Account> for their accounts. Include methods addAccount(Account account) to add an account, and getTotalBalance() that returns the sum of all account balances. Override toString() to return Customer: name (X accounts).
  • main.dart: Demonstrate your banking system. Create a customer (C001, John Smith). Create a SavingsAccount (SAV001, starting balance $1000) and a CheckingAccount (CHK001, starting balance $500). Add both accounts to the customer and print the customer. Then perform these 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)
    Finally, print Savings transactions: X and Total 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.0

Try 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