Java bank - Inheritance

Let us assume that one day our Java bank will need an account to withdraw even if the withdrawal amount is greater than the balance. Now some accounts can have negative balances. If inheritance is applied, the existing account class can be superclass without change. And you can make an account with a negative balance as a subclass. Let's name an account with a negative balance as a minus account. A minus account class only need to override the withdraw() of the superclass account class. The addAccount(String accountNo, String name, double balance) method of the bank class should determine whether an account to create is a regular account or a minus account. So add the parameter to the addAccount() as below.

public void addAccount(String accountNo, String name, double balance, String kind) {
  //..Omitted
}

Suppose that the kind parameter is assigned the dash character (-) when creating a negative account.

public void addAccount(String accountNo, String name, double balance, String kind) {
  Account account = this.getAccount(accountNo);
  if (account == null) {
    if (kind != null && kind.equals("-") {
      accounts.add(new MinusAccount(accountNo, name, balance));
    } else {
      accounts.add(new Account(accountNo, name, balance));
    } 
  }
}
Account.java
package net.java_school.bank;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Calendar;
import java.util.Date;

public class Account {
  private String accountNo;
  private String name;
  protected double balance;
  protected List<Transaction> transactions = new ArrayList<Transaction>();
	
  static final String KIND = "Normal";
  static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd");
  static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
  static final String DEPOSIT = "Deposit";
  static final String WITHDRAW = "Withdrawal";
  static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance();

  public Account() {}
	
  public Account(String accountNo, String name) {
    this.accountNo = accountNo;
    this.name = name;
  }
	
  public Account(String accountNo, String name, double balance) {
    this.accountNo = accountNo;
    this.name = name;
    this.balance = balance;
  }
	
  public void deposit(double amount) {
    balance = balance + amount;
    Transaction transaction = new Transaction();
    Calendar cal = Calendar.getInstance();
    Date date = cal.getTime();
    transaction.setTransactionDate(DATE_FORMAT.format(date));
    transaction.setTransactionTime(TIME_FORMAT.format(date));
    transaction.setAmount(amount);
    transaction.setBalance(balance);
    transaction.setKind(DEPOSIT);
    transactions.add(transaction);
  }
	
  public void withdraw(double amount) {
    if (amount > this.balance) {
      return;
    }
    balance = balance - amount;
    Transaction transaction = new Transaction();
    Calendar cal = Calendar.getInstance();
    Date date = cal.getTime();
    transaction.setTransactionDate(DATE_FORMAT.format(date));
    transaction.setTransactionTime(TIME_FORMAT.format(date));
    transaction.setAmount(amount);
    transaction.setBalance(balance);
    transaction.setKind(WITHDRAW);
    transactions.add(transaction);
  }
	
  public String getName() {
    return name;
  }
	
  public void setName(String name) {
    this.name = name;
  }
	
  public String getAccountNo() {
    return accountNo;
  }
	
  public void setAccountNo(String accountNo) {
    this.accountNo = accountNo;
  }
	
  public long getBalance() {
    return balance;
  }
	
  public void setBalance(double balance) {
    this.balance = balance;
  }
	
  public List<Transaction> getTransactions() {
    return transactions;
  }
	
  public void setTransactions(List<Transaction> transactions) {
    this.transactions = transactions;
  }
  
  public String getKind() {
    return KIND;
  }
  
  @Override
  public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append(accountNo);
    sb.append("|");		
    sb.append(name);
    sb.append("|");
    sb.append(NUMBER_FORMAT.format(balance));
    sb.append("|");
    sb.append(getKind());
			
    return sb.toString();
  }

}

Modify the toString() method of the Transaction class as follows:

Transaction.java
@Override
public String toString() {
  StringBuilder sb = new StringBuilder();
  sb.append(transactionDate);
  sb.append("|");
  sb.append(transactionTime);
  sb.append("|");
  sb.append(kind);
  sb.append("|");
  sb.append(Account.NUMBER_FORMAT.format(amount));
  sb.append("|");
  sb.append(Account.NUMBER_FORMAT.format(balance));
	
  return sb.toString();
}
MinusAccount.java
package net.java_school.bank;

import java.util.Calendar;
import java.util.Date;

public class MinusAccount extends Account {
  static final String KIND = "Minus";
	
  public MinusAccount() {}
	
  public MinusAccount(String accountNo, String name) {
    super(accountNo, name);
  }

  public MinusAccount(String accountNo, String name, double balance) {
    super(accountNo, name, balance);
  }
	
  @Override
  public String getKind() {
    return KIND;
  }

  @Override
  public void withdraw(double amount) {
    balance = balance - amount;
    Transaction transaction = new Transaction();
    Calendar cal = Calendar.getInstance();
    Date date = cal.getTime();
    transaction.setTransactionDate(Account.DATE_FORMAT.format(date));
    transaction.setTransactionTime(Account.TIME_FORMAT.format(date));
    transaction.setAmount(amount);
    transaction.setBalance(balance);
    transaction.setKind(Account.WITHDRAW);
    transactions.add(transaction);
  }
	
}
Bank.java
package net.java_school.bank;

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

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

  public void addAccount(String accountNo, String name) {
    Account account = this.getAccount(accountNo);
    if (account == null) {
      accounts.add(new Account(accountNo, name));
    }
  }
    
  public void addAccount(String accountNo, String name, double balance) {
    Account account = this.getAccount(accountNo);
    if (account == null) {
      accounts.add(new Account(accountNo, name, balance));
    }
  }

  public void addAccount(String accountNo, String name, String kind) {
    Account account = this.getAccount(accountNo);
    if (account == null) {
      if (kind != null && kind.equals("-")) {
        accounts.add(new MinusAccount(accountNo, name));
      } else {
        accounts.add(new Account(accountNo, name));
      }
    }
  }
    
  public void addAccount(String accountNo, String name, double balance, String kind) {
    Account account = this.getAccount(accountNo);
    if (account == null) {
      if (kind != null && kind.equals("-")) {
        accounts.add(new MinusAccount(accountNo, name, balance));
      } else {
        accounts.add(new Account(accountNo, name, balance));
      }
    }
  }
    
  public Account getAccount(String accountNo) {
    int size = accounts.size();
    for (int i = 0; i < size; i++) {
      if (accountNo.equals(accounts.get(i).getAccountNo())) {
        return accounts.get(i);
      }
    }
    return null;
  }
    
  public List<Account> findAccountByName(String name) {
    List<Account> matched = new ArrayList<Account>();
    int size = accounts.size();
    for (int i = 0; i < size; i++) {
      if (name.equals(accounts.get(i).getName())) {
        matched.add(accounts.get(i));
      }
    }
        
    return matched;
  }
    
  public List<Account> getAccounts() {
    return accounts;
  }
  
}
Test.java
package net.java_school.bank;

import java.util.List;

public class Test {
  static final String ACCOUNTS_HEADING = "AccountNo|Owner|Balance|N/M";
  static final String TRANSACTIONS_HEADING = "Date|Time|D/W|Amount|Balance";

  public static void main(String[] args) {
    
    //Create test accounts
    Bank bank = new Bank();
    bank.addAccount("101", "Alison");
    bank.addAccount("202", "Bill");
    bank.addAccount("303", "Carol");
    bank.addAccount("404", "Alison", "-");
        
    //..Omitted

    System.out.println();

    //6.Withdraw $ 5 from 404 account.
    System.out.println("6.Withdraw $ 5 from 404 account.");
    Account alison404 = bank.getAccount("404");
    alison404.withdraw(5);
    System.out.println(ACCOUNTS_HEADING);
    System.out.println(alison404);
  }

}
6.Withdraw $ 5 from 404 account.
AccountNo|Owner|Balance
404|Alison|-5|Minus

TODO

Create following custom exceptions and apply them to the example.

  • InsufficientBalanceException
  • DuplicateAccountException