Skip to content

Commit 7c2413c

Browse files
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

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Section16StaticFinal/src/BankAccountAutomaticStatic.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class BankAccount {
44
private String accountNumber;
55
private double balance;
6-
private static int count = 1; // Static counter for unique account numbers
6+
private static int count = 1; // Static counter for unique account numbers.
77

88
private String generateAccountNumber() {
99
Date d = new Date();
@@ -12,7 +12,7 @@ private String generateAccountNumber() {
1212

1313
public BankAccount() {
1414
this.accountNumber = generateAccountNumber();
15-
this.balance = 0.0; // Default balance
15+
this.balance = 0.0; // Default balance.
1616
}
1717

1818
public String getAccountNumber() {
@@ -76,9 +76,10 @@ public static void main(String[] args) {
7676
c2.getAccount().withdraw(700); // Should show insufficient funds
7777
}
7878
}
79+
7980
/*
8081
Features:
8182
Customer Class: Holds details like customer name and associated account.
8283
BankAccount Class: Generates unique account numbers using a static counter.
8384
Automatic Account Number Assignment: Uses a static counter with a timestamp-based prefix.
84-
*/
85+
*/

0 commit comments

Comments
 (0)