Commit 7c2413c
committed
feat: Implement BankAccount system with static counter for automatic unique account numbers
WHAT the code does:
- BankAccount class:
- Fields: accountNumber, balance, and static int count for generating unique IDs.
- generateAccountNumber(): creates IDs in format `BANK-<year>-<count>`, using system date and incrementing count.
- Constructor: initializes accountNumber automatically and sets balance to 0.
- deposit(double): increases balance if amount > 0, otherwise rejects.
- withdraw(double): decreases balance if sufficient funds exist, otherwise rejects.
- Getters for accountNumber and balance.
- Customer class:
- Fields: name and account.
- Constructor: assigns customer name and creates a new BankAccount automatically.
- Getters for name and account.
- BankAccountAutomaticStatic main():
- Creates three Customer objects: Alice, Bob, Charlie.
- Prints auto-generated account numbers for each.
- Demonstrates deposit and withdrawal operations, including insufficient funds check.
WHY this matters:
- Demonstrates **encapsulation** (private fields with getters/setters).
- Shows **static variables**: count ensures each account number is unique across all BankAccount instances.
- Uses **composition**: Customer contains a BankAccount.
- Models a real-world banking system with unique account numbers, deposits, and withdrawals.
- Highlights automatic account number generation, removing manual entry errors.
HOW it works:
1. Each time a Customer is created, its constructor creates a new BankAccount.
2. BankAccount constructor calls generateAccountNumber(), which uses:
- Current year from Date object.
- Static counter count, which increments per account.
3. Deposits and withdrawals adjust the balance, validating amounts.
4. Main program prints account numbers and performs sample transactions.
Tips and gotchas:
- java.util.Date’s getYear() is deprecated; prefer java.time.LocalDate.now().getYear().
- Account number uniqueness relies on count; restarting program resets it unless persisted. For production, use databases or UUIDs.
- Balance updates are not synchronized; in multithreaded systems, use synchronized methods or Atomic variables.
- Encapsulation is good here, but adding setters for name/account might be useful depending on requirements.
- Error handling: throwing exceptions for invalid deposits/withdrawals can be cleaner than printing messages.
Use-cases / analogies:
- Real banking systems: auto-generated unique account IDs per customer.
- Library systems: auto-generate membership IDs with a year prefix and counter.
- Ticketing systems: issue unique IDs by combining date and incrementing numbers.
Short key: java-oop static-counter bank-account unique-id encapsulation composition.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 8e97ae0 commit 7c2413c
1 file changed
+4
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
| 79 | + | |
79 | 80 | | |
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
84 | | - | |
| 85 | + | |
0 commit comments