Menu
Coddy logo textTech

Recap - Bank Account Manager

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 20 of 91.

challenge icon

Challenge

Easy

Let's build a complete bank account management system that brings together all the class property concepts you've learned in this chapter. You'll create a BankAccount class that properly encapsulates account data while tracking bank-wide statistics.

You'll organize your code across two files:

  • BankAccount.php — Define a BankAccount class that manages individual accounts and tracks overall bank statistics. The class should include:
    • A constant MINIMUM_BALANCE set to 100
    • A public readonly property string $accountId that cannot change after creation
    • A public property $holderName for the account holder's name
    • A private property $balance to protect the account balance
    • A private static property $totalAccounts starting at 0
    • A private static property $totalDeposits starting at 0
    The constructor should accept an account ID, holder name, and initial balance. It should set all instance properties and increment the total accounts count. Include these methods:
    • getBalance() — returns the current balance
    • deposit($amount) — adds to the balance and updates the total deposits tracker, returns the new balance
    • withdraw($amount) — subtracts from the balance only if the remaining balance stays at or above the minimum balance constant, returns the new balance (or the unchanged balance if withdrawal isn't allowed)
    • A static method getTotalAccounts() — returns the total number of accounts created
    • A static method getTotalDeposits() — returns the sum of all deposits made across all accounts
  • main.php — Include the BankAccount class file. You'll receive four inputs: the first account ID, first holder name, second account ID, and second holder name. Create two accounts, both with an initial balance of 500. For the first account: deposit 200, then withdraw 100. Print the account ID, holder name, and balance on one line in the format: "[accountId]: [holderName] - $[balance]" For the second account: deposit 300, then attempt to withdraw 800 (this should fail due to minimum balance). Print the same format for this account. Finally, print the total accounts and total deposits on separate lines:
    • "Total Accounts: [count]"
    • "Total Deposits: $[amount]"

This challenge combines readonly properties for immutable identifiers, private properties with getter methods for protected data, constants for fixed rules, and static properties with static methods for bank-wide statistics.

Try it yourself

<?php

require_once 'BankAccount.php';

// Read inputs
$firstAccountId = trim(fgets(STDIN));
$firstHolderName = trim(fgets(STDIN));
$secondAccountId = trim(fgets(STDIN));
$secondHolderName = trim(fgets(STDIN));

// TODO: Create first account with initial balance of 500

// TODO: Create second account with initial balance of 500

// TODO: First account: deposit 200, then withdraw 100

// TODO: Print first account info: "[accountId]: [holderName] - $[balance]"

// TODO: Second account: deposit 300, then attempt to withdraw 800

// TODO: Print second account info: "[accountId]: [holderName] - $[balance]"

// TODO: Print "Total Accounts: [count]"

// TODO: Print "Total Deposits: $[amount]"

?>

All lessons in Object Oriented Programming