Java bank - Interface

The interface defines the functionality of the component. Modify the existing Bank class into an interface that defines the functionality of the bank component. I will create a class called MyBank as an implementation that implements the Bank interface. Open Bank.java and select File - Save As ... menu to save it as MyBank.java and fix the compilation error. Reopen Bank.java and modify it as shown below.

Bank.java
package net.java_school.bank;

import java.util.List;

public interface Bank {
	
    //Create an account.
    public void addAccount(String accountNo, String name, String kind);

    //Create an account.
    public void addAccount(String accountNo, String name, long balance, String kind);

    //Find the account by account number.
    public Account getAccount(String accountNo);

    //Find the account by owner name.
    public List<Account> findAccountByName(String name);

    //Return all accounts.
    public List<Account> getAccounts();
  
}

Modify the MyBank.java file by referring to the highlighted section.

MyBank.java
package net.java_school.bank;

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

public class MyBank implements Bank {
	private List<Account> accounts = new ArrayList<Account>();

	@Override	
	public void addAccount(String accountNo, String name, String kind) {
		Account account = getAccount(accountNo);
		
		if (account != null) throw new DuplicateAccountException("Duplicated account.");
		
		if (Account.NORMAL.equals(kind)) {
			accounts.add(new NormalAccount(accountNo, name, kind));
		} else if (Account.MINUS.equals(kind)) {
			accounts.add(new MinusAccount(accountNo, name, kind));
		}
	}
	
	@Override
	public void addAccount(String accountNo, String name, long balance, String kind) {
		Account account = getAccount(accountNo);
		
		if (account != null) throw new DuplicateAccountException("Duplicated account.");
		
		if (Account.NORMAL.equals(kind)) {
			accounts.add(new NormalAccount(accountNo, name, balance, kind));
		} else if (Account.MINUS.equals(kind)) {
			accounts.add(new MinusAccount(accountNo, name, balance, kind));
		}
			
	}

	@Override
	public Account getAccount(String accountNo) {
		int totalAccount = accounts.size();
		
		for (int i = 0; i < totalAccount; i++) {
			if (accountNo.equals(accounts.get(i).getAccountNo())) {
				return accounts.get(i);
			}
		}

		return null;
	}

	@Override
	public List<Account> findAccountByName(String name) {
		List<Account> matched = new ArrayList<Account>();
		int totalAccount = accounts.size();
		
		for (int i = 0; i < totalAccount; i++) {
			if (name.equals(accounts.get(i).getName())) {
				matched.add(accounts.get(i));
			}
		}
		
		return matched;
	}

	@Override
	public List<Account> getAccounts() {
		return accounts;
	}

}

Modify the following in the main method of the test class:

Test.java
Bank bank = new MyBank();