OOPs

7. Write a Java program to create a class called "Bank" with a collection of accounts and methods to add and remove accounts, and to deposit and withdraw money. Also define a class called "Account" to maintain account details of a particular customer.

            import java.util.ArrayList;
            import java.util.List;
            
            class Account {
                private String accountNumber;
                private String accountHolder;
                private double balance;
            
                // Constructor
                public Account(String accountNumber, String accountHolder) {
                    this.accountNumber = accountNumber;
                    this.accountHolder = accountHolder;
                    this.balance = 0.0; // Initial balance is 0
                }
            
                // Getters
                public String getAccountNumber() {
                    return accountNumber;
                }
            
                public String getAccountHolder() {
                    return accountHolder;
                }
            
                public double getBalance() {
                    return balance;
                }
            
                // Method to deposit money
                public void deposit(double amount) {
                    if (amount > 0) {
                        balance += amount;
                        System.out.println("Deposited: Rs" + amount + " | New Balance: Rs" + balance);
                    } else {
                        System.out.println("Deposit amount must be positive!");
                    }
                }
            
                // Method to withdraw money
                public void withdraw(double amount) {
                    if (amount > 0 && amount <= balance) {
                        balance -= amount;
                        System.out.println("Withdrawn: Rs" + amount + " | New Balance: Rs" + balance);
                    } else {
                        System.out.println("Invalid withdrawal amount!");
                    }
                }
            }
            
            class Bank {
                private List accounts;
            
                // Constructor
                public Bank() {
                    accounts = new ArrayList<>();
                }
            
                // Method to add an account
                public void addAccount(Account account) {
                    accounts.add(account);
                    System.out.println("Account added: " + account.getAccountHolder());
                }
            
                // Method to remove an account by account number
                public void removeAccount(String accountNumber) {
                    for (int i = 0; i < accounts.size(); i++) {
                        if (accounts.get(i).getAccountNumber().equals(accountNumber)) {
                            System.out.println("Account removed: " + accounts.get(i).getAccountHolder());
                            accounts.remove(i);
                            return;
                        }
                    }
                    System.out.println("Account with account number " + accountNumber + " not found.");
                }
            
                // Method to get account by account number
                public Account getAccount(String accountNumber) {
                    for (Account account : accounts) {
                        if (account.getAccountNumber().equals(accountNumber)) {
                            return account;
                        }
                    }
                    return null;
                }
            }
            
            public class BankManagementSystem {
                public static void main(String[] args) {
                    Bank bank = new Bank();
            
                    // Create some accounts
                    Account account1 = new Account("12345", "Misbah");
                    Account account2 = new Account("67890", "Sandeep");
            
                    // Add accounts to the bank
                    bank.addAccount(account1);
                    bank.addAccount(account2);
            
                    // Perform some transactions
                    account1.deposit(500);
                    account1.withdraw(200);
            
                    account2.deposit(1000);
                    account2.withdraw(1500); // This should show an invalid withdrawal message
            
                    // Remove an account
                    bank.removeAccount("12345");
                    
                    // Try to remove a non-existing account
                    bank.removeAccount("11111");
                }
            }
        

OUTPUT

            Account added: Misbah
            Account added: Sandeep
            Deposited: Rs. 5000.0 | New Balance: Rs. 5000.0
            Withdrawn: Rs. 200.0 | New Balance: Rs. 300.0
            Deposited: Rs. 1000.0 | New Balance: Rs. 1000.0
            Invalid withdrawal amount!
            Account removed: Misbah
            Account with account number 11111 not found.