Commit b7738f7
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
1 file changed
+1
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
0 commit comments