/* This program is an exercise for Inheritance and Polymorphism.

Label each method call as either:

1. Polymorphism,

2. Inheritance, or

3. Overloading.

*/

public class AccountTest

{ public static void main(String[] args)

{

SavingsAccount momsSavings

= new SavingsAccount(0.5);

TimeDepositAccount collegeFund

= new TimeDepositAccount(1, 3);

CheckingAccount harrysChecking

= new CheckingAccount(0.);

momsSavings.deposit( 10000. );

collegeFund.deposit( 10000. );

momsSavings.transfer( harrysChecking, 2000. );

collegeFund.transfer( harrysChecking, 980. );

harrysChecking.withdraw( 500. );

harrysChecking.withdraw( 80. );

harrysChecking.withdraw( 400. );

harrysChecking.withdraw( 10000. );

endOfMonth( momsSavings );

endOfMonth( collegeFund );

endOfMonth( harrysChecking );

printBalance( "mom's savings", momsSavings );

// $10000 - $2000 + 0.5% interest = $8040

printBalance( "the college fund", collegeFund );

// $10000 - $980 - $20 penalty + 1% interest

// = $9090

printBalance( "Harry's checking", harrysChecking );

// $2000 + $980 - $500 - $80 - $400 - $6 fees - 25 penalty

// = $1969

}

public static void endOfMonth( SavingsAccount savings )

{ savings.addInterest();

}

public static void endOfMonth( CheckingAccount checking )

{ checking.deductFees();

}

public static void printBalance( String name,BankAccount account )

{ System.out.println("The balance of " + name

+ " account is $" + account.getBalance());

}

}

public class BankAccount

{

private double balance_;

private long accountNumber_;

static double OVERDRAFT_PENALTY = 25.0;

static String bankName = "Wells Fargo Bank";

public BankAccount()

{ balance_ = 0.; } // default (no-arg) constructor.

public BankAccount( double initialBalance )

{ balance_ = initialBalance; }

public void deposit( double amount )

{ balance_ += amount; }

public void withdraw( double amount )

{

try

{

if ( amount > balance_ ) { // overdraft!

if ( OVERDRAFT_PENALTY > balance_ )

balance_ = 0.0;

else

balance_ -= OVERDRAFT_PENALTY;

throw new OverdraftException( "Withdrawal exceeds balance" );

} else {

balance_ -= amount;

}

} catch( OverdraftException e ) {

System.out.println( e );

}

} // end withdraw method

public double getBalance()

{ return balance_;

}

public void transfer( BankAccount other, double amount )

{ this.withdraw( amount ); // this is the implicit parameter

other.deposit( amount );

}

} // end BankAccount class

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// Customized User (Programmer) Designed Exception Class:

public class OverdraftException extends RuntimeException

{

public OverdraftException( String reason )

{ super ( reason ) ; }

}

public class CheckingAccount extends BankAccount

{ public CheckingAccount( double initialBalance )

{ // construct superclass

super( initialBalance );

// initialize transaction count

transactionCount_ = 0;

} // end the constructor

public void deposit( double amount )

{ ++transactionCount_;

// now add amount to balance

super.deposit( amount );

}

public void withdraw(double amount)

{ ++transactionCount_;

// now subtract amount from balance

super.withdraw( amount );

}

public void deductFees()

{ if (transactionCount_ > FREE_TRANSACTIONS)

{ double fees = TRANSACTION_FEE *

(transactionCount_ - FREE_TRANSACTIONS);

super.withdraw( fees );

}

transactionCount_ = 0;

} // end deductFees method

private int transactionCount_;

static final int FREE_TRANSACTIONS = 3;

static final double TRANSACTION_FEE = 2.0;

} // end CheckingAccount class

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

public class SavingsAccount extends BankAccount

{ public SavingsAccount( double rate )

{ super();

interestRate_ = rate;

} // end constructor

public void addInterest()

{ double interest = getBalance() * interestRate_ / 100.0 ;

deposit( interest );

}

private double interestRate_;

} // end the SavingsAccount class

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

public class TimeDepositAccount extends SavingsAccount

{

public TimeDepositAccount( double rate, int maturity )

{

super( rate );

periodsToMaturity_ = maturity;

}

public void addInterest( )

{

--periodsToMaturity_;

super.addInterest();

}

public void withdraw( double amount )

{

if ( periodsToMaturity_ > 0 )

super.withdraw( EARLY_WITHDRAWAL_PENALTY );

super.withdraw( amount );

}

private int periodsToMaturity_;

final static double EARLY_WITHDRAWAL_PENALTY = 20;

} // end TimeDepositAccount class

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

OverdraftException: withdrawal exceeds balance

The balance of mom's savings account is $8040.0

The balance of the college fund account is $9090.0

The balance of Harry's checking account is $1969.0