In this example we will see Advance Object Oriented Programming.
We will use a simple banking example for this.
Steps:
- Create folder “src”. This will contain all java files.
- Then create AdvOOPTest1 to test the application.
The structure will be as follow
- src/InsufficientFundsException.java
- src/IllegalTransferException.java
- src/Account.java
- src/CustomerAccount.java
- src/CurrentAccount.java
- src/SavingsAccount.java
- src/Banker.java
- AdvOOPTest1.java
IllegalTransferException.java
IllegalTransferException.java
package com.pravin.banking;
public class IllegalTransferException extends RuntimeException{}
Account.java
package com.pravin.banking;
/**
An abstract super-class for all account types in the bank.
@author Pravin
@see SavingsAccount
@see CurrentAccount
*/
public abstract class Account{
long id;
/**
Returns ID of this account.
*/
public long getId(){
return id;
}
/**
Returns the current-balance in this account.
*/
public abstract double getBalance();
/**
Credits the specified amount in this account.
*/
public abstract void deposit(double amount);
/**
Debits the specified amount from this account.
@throws InsufficientFundsException Specified amount cannot be withdrawn
*/
public abstract void withdraw(double amount) throws InsufficientFundsException;
/**
Transfer specified amount to another account.
@param amount Funds to transfer
@param other The target account
@throws InsufficientFundsException This account does not hold specified
amount
@throws IllegalTransferException Target account is identical to this
account
*/
public final void transfer(double amount, Account other)
throws InsufficientFundsException{
if(other == this)
throw new IllegalTransferException();
this.withdraw(amount);
other.deposit(amount);
}
}
CustomerAccount.java
package com.pravin.banking;
public abstract class CustomerAccount extends Account{
protected double balance;
public double getBalance(){
return balance;
}
public void deposit(double amount){
balance += amount;
}
}
CurrentAccount.java
package com.pravin.banking;
public final class CurrentAccount extends CustomerAccount{
public void withdraw(double amount){
balance -= amount;
}
}
SavingsAccount.java
package com.pravin.banking;
public final class SavingsAccount extends CustomerAccount
implements Profitable{
public static final double MIN_BALANCE = 500.0;
public SavingsAccount(){
balance = MIN_BALANCE;
}
public void withdraw(double amount) throws InsufficientFundsException{
if(balance - amount <> throw new InsufficientFundsException(); balance -= amount; }
public double addInterest(int period){
float rate = (balance <> double interest = balance * rate * period / 100; balance += interest; return interest; }}
Banker.java
package com.pravin.banking;
public class Banker{
private static Banker singleton;
private long nextId;
private Banker(){
nextId = System.currentTimeMillis() % 1000000;
}
public static Banker getBanker(){
if(singleton == null)
singleton = new Banker();
return singleton;
}
public Account openAccount(double amount, boolean savings){
Account acc;
if(savings)
acc = new SavingsAccount();
else
acc = new CurrentAccount();
acc.id = nextId++;
acc.deposit(amount);
return acc;
}
public final Account openAccount(double amount){
return openAccount(amount, false);
}
}
AdvOOPTest1.java
import com.pravin.banking.*;
class AdvOOPTest1{
public static void main(String[] args){
Banker b = Banker.getBanker();
Account cust = b.openAccount(4500, true);
Account vend = b.openAccount(0);
try{
double amt = Double.parseDouble(args[0]);
cust.transfer(amt, vend);
}catch(InsufficientFundsException e){
System.out.println("ERROR: Transfer aborted due to lack of funds!");
}catch(Exception e){
System.out.printf("ERROR: %s%n", e);
}
System.out.printf("Customer Account ID is %d and Balance is %.2f%n", cust.getId(), cust.getBalance());
System.out.printf("Vendor Account ID is %d and Balance is %.2f%n", vend.getId(), vend.getBalance());
}
}
To compile:
javac -d . src/*.java
javac AdvOOPTest1.java
To run:
java AdvOOPTest1 2000