Recap - Bank Account Manager
Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 20 of 91.
Challenge
EasyLet'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 aBankAccountclass that manages individual accounts and tracks overall bank statistics. The class should include:- A constant
MINIMUM_BALANCEset to100 - A public readonly property
string $accountIdthat cannot change after creation - A public property
$holderNamefor the account holder's name - A private property
$balanceto protect the account balance - A private static property
$totalAccountsstarting at0 - A private static property
$totalDepositsstarting at0
getBalance()— returns the current balancedeposit($amount)— adds to the balance and updates the total deposits tracker, returns the new balancewithdraw($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
- A constant
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 of500. For the first account: deposit200, then withdraw100. Print the account ID, holder name, and balance on one line in the format:"[accountId]: [holderName] - $[balance]"For the second account: deposit300, then attempt to withdraw800(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
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe $this KeywordMethodsPropertiesConstructor (__construct)Destructor (__destruct)Recap - Simple Calculator4Inheritance
Basic InheritanceThe parent:: KeywordMethod OverridingThe final KeywordAbstract ClassesRecap - Employee Hierarchy7Encapsulation
Public, Protected, PrivateAccess Modifiers in DepthGetters and SettersInformation HidingConstructor Promotion (8.0)Recap - Student Records System2Namespaces & Autoloading
Introduction to NamespacesThe use KeywordPSR-4 Autoloading StandardComposer AutoloaderRecap - Organized Project5Interfaces & Contracts
Introduction to InterfacesImplementing InterfacesMultiple Interface ImplementInterface vs Abstract ClassType Hinting with InterfacesRecap - Shape Calculator8Magic Methods
Magic Methods Introduction__toString & __debugInfo__get, __set, __isset, __unset__call & __callStatic__clone & Object Cloning__serialize & __unserializeRecap - Custom Collection11Type System & Error Handling
Type DeclarationsNullable TypesUnion & Intersection TypesException ClassesCustom Exception HierarchyTry, Catch, FinallyRecap - Form Validator14Project: Library Management
Project OverviewBook and User Classes3Class Properties
Instance vs Static PropertiesConstants in ClassesStatic Methods & PropertiesPrivate & Protected PropertiesReadonly Properties (PHP 8.1)Recap - Bank Account Manager6Polymorphism
Method Overriding RevisitedPolymorphism via InterfacesType Hinting & Union TypesLate Static BindingRecap - Payment Processor9Traits
Introduction to TraitsUsing Multiple TraitsTrait Conflict ResolutionAbstract Methods in TraitsTraits vs Inheritance12Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern