w3resource

Java: Bank Account Management

Java OOP: Exercise-7 with Solution

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.

Sample Solution:

Java Code:

//Account.java
public class Account {
  private String name;
  private String accountNumber;
  private double balance;

  public Account(String name, String accountNumber, double balance) {
    this.name = name;
    this.accountNumber = accountNumber;
    this.balance = balance;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAccountNumber() {
    return accountNumber;
  }

  public void setAccountNumber(String accountNumber) {
    this.accountNumber = accountNumber;
  }

  public double getBalance() {
    return balance;
  }

  public void setBalance(double balance) {
    this.balance = balance;
  }

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

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

  public String getAccountInfo() {
    return "Name: " + name + ", Account Number: " + accountNumber + ", Balance: " + balance;
  }
}

The above class has three private attributes: name, accountNumber and balance. There are several methods to deposit, withdraw, maintain balance in an individual account, print account details and more.

//Bank.java
import java.util.ArrayList;

public class Bank {
  private ArrayList < Account > accounts;

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

  public void addAccount(Account account) {
    accounts.add(account);
  }

  public void removeAccount(Account account) {
    accounts.remove(account);
  }

  public void depositMoney(Account account, double amount) {
    account.deposit(amount);
  }

  public void withdrawMoney(Account account, double amount) {
    account.withdraw(amount);
  }

  public ArrayList < Account > getAccounts() {
    return accounts;
  }
}

The above class has a private accounts attribute, a constructor that initializes this attribute as an empty array list. It also has methods to add and remove accounts from the collection, and to deposit and withdraw money from an account.

//Main.java
import java.util.ArrayList;
public class Main {
  public static void main(String[] args) {
    Bank bank = new Bank();

    Account account1 = new Account("Peter Irmgard", "C0011", 5000);
    Account account2 = new Account("Katja Ruedi", "C0121", 4500);
    Account account3 = new Account("Marcella Gebhard", "C0222", 20000);

    bank.addAccount(account1);
    bank.addAccount(account2);
    bank.addAccount(account3);

    ArrayList < Account > accounts = bank.getAccounts();

    for (Account account: accounts) {
      System.out.println(account.getAccountInfo());
    }

    System.out.println("\nAfter depositing 1000 into account1:");
    bank.depositMoney(account1, 1000);
    System.out.println(account1.getAccountInfo());
    System.out.println("No transaction in account2:");
    System.out.println(account2.getAccountInfo());
    System.out.println("After withdrawing 5000 from account3:");
    bank.withdrawMoney(account3, 5000);
    System.out.println(account3.getAccountInfo());
  }
}

In the above example code, we create an instance of the "Bank" class and three instances of the "Account" class, and add them to the collection through the ‘addAccount’ method. We then print the account information for each account in the collection using a for loop. We also deposit 1000 into account1 using the ‘depositMoney’ method, and withdraw 5000 from account3 using the ‘withdrawMoney’ method. We also print the updated account information. Account2 has no transaction.

Sample Output:

Name: Peter Irmgard, Account Number: C0011, Balance: 5000.0
Name: Katja Ruedi, Account Number: C0121, Balance: 4500.0
Name: Marcella Gebhard, Account Number: C0222, Balance: 20000.0

After depositing 1000 into account1:
Name: Peter Irmgard, Account Number: C0011, Balance: 6000.0
No transaction in account2:
Name: Katja Ruedi, Account Number: C0121, Balance: 4500.0
After withdrawing 5000 from account3:
Name: Marcella Gebhard, Account Number: C0222, Balance: 15000.0

Flowchart:

Flowchart: Java  OOP Exercises: Bank Account Management.
Flowchart: Java  OOP Exercises: Bank Account Management.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: Employee Management System.
Java OOP Next: Traffic Light Simulation.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.