Java bank - Interface
The interface defines the functionality of the component.
Modify the current bank class into an interface that defines the functionality of the bank component and create a class called MyBank as an implementation that implements the Bank interface.
Open Bank.java and select the "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);
  //Create an account.
  public void addAccount(String accountNo, String name, double balance);
  //Create an account.
  public void addAccount(String accountNo, String name, String kind);
  //Create an account.
  public void addAccount(String accountNo, String name, double 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 class.
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) {
    Account account = getAccount(accountNo);
    if (account != null) throw new DuplicateAccountException("Duplicated account.");
    accounts.add(new NormalAccount(accountNo, name));
  }
  @Override    
  public void addAccount(String accountNo, String name, double balance) {
    Account account = getAccount(accountNo);
    if (account != null) throw new DuplicateAccountException("Duplicated account.");
    accounts.add(new NormalAccount(accountNo, name, balance));
  }
  @Override    
  public void addAccount(String accountNo, String name, String kind) {
    Account account = getAccount(accountNo);
    if (account != null) throw new DuplicateAccountException("Duplicated account.");
    if (kind != null && kind.equals("-")) {
      accounts.add(new MinusAccount(accountNo, name));
    } else {
      accounts.add(new NormalAccount(accountNo, name));
    }
  }
    
  @Override    
  public void addAccount(String accountNo, String name, double balance, String kind) {
    Account account = getAccount(accountNo);
    if (account != null) throw new DuplicateAccountException("Duplicated account.");
    if (kind != null && kind.equals("-")) {
      accounts.add(new MinusAccount(accountNo, name, balance));
    } else {
      accounts.add(new NormalAccount(accountNo, name, balance));
    }
            
  }
  @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 main() of the test class.
Test.java
Bank bank = new MyBank();
