Home
Time Box
Calculator
Snake
Blogs
Hacks

Unit 5 Hacks • 5 min read


import java.util.ArrayList;
import java.util.List;

class Bank {
    private List<BankAccount> accounts;
    private double totalBankDeposits;

    public Bank() {
        accounts = new ArrayList<>();
        totalBankDeposits = 0.0;
    }

    public void createAccount(String accountHolder, double initialDeposit) {
        BankAccount newAccount = new BankAccount(accountHolder, initialDeposit);
        accounts.add(newAccount);
        totalBankDeposits += initialDeposit;
    }

    public void deposit(int accountNumber, double amount) {
        BankAccount account = findAccount(accountNumber);
        if (account != null) {
            account.deposit(amount);
            totalBankDeposits += amount;
        } else {
            System.out.println("Account not found.");
        }
    }

    public void withdraw(int accountNumber, double amount) {
        BankAccount account = findAccount(accountNumber);
        if (account != null) {
            if (account.getBalance() >= amount) {
                account.withdraw(amount);
                totalBankDeposits -= amount;
            } else {
                System.out.println("Insufficient funds.");
            }
        } else {
            System.out.println("Account not found.");
        }
    }

    public double getTotalBankDeposits() {
        return totalBankDeposits;
    }

    private BankAccount findAccount(int accountNumber) {
        for (BankAccount account : accounts) {
            if (account.getAccountNumber() == accountNumber) {
                return account;
            }
        }
        return null;
    }
}

class BankAccount {
    private static int accountCounter = 1000;
    private int accountNumber;
    private String accountHolder;
    private double balance;

    public BankAccount(String accountHolder, double initialDeposit) {
        this.accountNumber = accountCounter++;
        this.accountHolder = accountHolder;
        this.balance = initialDeposit;
    }

    public int getAccountNumber() {
        return accountNumber;
    }

    public double getBalance() {
        return balance;
    }

    public void deposit(double amount) {
        balance += amount;
    }

    public void withdraw(double amount) {
        balance -= amount;
    }
}

public class Main {
    public static void main(String[] args) {
        Bank bank = new Bank();

        bank.createAccount("John Doe", 1000.0);
        bank.createAccount("Jane Smith", 1500.0);

        bank.deposit(1000, 500.0);
        bank.withdraw(1001, 200.0);

        System.out.println("Total Bank Deposits: $" + bank.getTotalBankDeposits());
    }
}

Main.main(null);

This Java program demonstrates the concepts you mentioned: