Skip to content

Commit b7738f7

Browse files
committed
feat: Implement simple bank account system with static counter for unique account numbers
WHAT the code does: - BankAccountPerson class: - Private field accountNumber. - Static counter count shared across all objects to ensure unique account numbers. - generateAccountNumber(): returns a unique string "BANK-" followed by count, incrementing each time it’s called. - Constructor: assigns a generated account number automatically. - Getter getAccountNumber() exposes the account number. - CustomerBank class: - Fields: name and account (composition — customer has a BankAccountPerson). - Constructor: sets name and creates a new BankAccountPerson object. - Method showDetails(): prints customer name and associated account number. - BankAccountIMP main(): - Creates three customers (Alice, Bob, Charlie). - Calls showDetails() for each, showing unique account numbers generated sequentially. WHY this matters: - Demonstrates **encapsulation**: account number and its generation logic are private to BankAccountPerson. - Uses a **static variable**: count increments across all instances to guarantee unique IDs. - Highlights **composition**: each CustomerBank contains its own BankAccountPerson. - Models a real-world concept in a simple, interview-friendly example. HOW it works: 1. count starts at 1 (shared across all BankAccountPerson objects). 2. Each time a new BankAccountPerson is constructed: - generateAccountNumber() returns "BANK-" + count, then increments count. 3. Customers automatically get new accounts upon creation. 4. showDetails() prints customer name + unique account number. Tips and gotchas: - Account number format is simplistic ("BANK-1", "BANK-2"). In production, you’d likely include date, branch code, or randomization. - Static variables reset if program restarts. Persistence (files or databases) is needed for real systems. - Encapsulation is good here: generation logic is hidden, but toString() overrides could improve printing. - Class names should follow Java naming conventions: BankAccountPerson → BankAccount, CustomerBank → Customer. Use-cases / analogies: - Banking: each customer automatically gets a unique account number. - Membership systems: auto-generated IDs for new members. - Ticketing/reservation systems: unique booking IDs issued sequentially. Short key: java-oop encapsulation static-counter unique-id composition customer-bank. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 7c2413c commit b7738f7

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Section16StaticFinal/src/BankAccountIMP.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ public static void main(String[] args) {
4646
Encapsulation: BankAccount hides the account number generation logic.
4747
Static Variable Usage: Ensures unique account numbers.
4848
Simple Object-Oriented Approach: Each Customer has a BankAccount.
49-
*/
49+
*/

0 commit comments

Comments
 (0)