|
|
|
public class BankAccount
{
private int accNum; // Private access
private String name; // -- limit to ONLY inside
private double balance; // the class "BankAccount"
/* ====================================================
Constructor 1: initialize all 3 instance variables
==================================================== */
public BankAccount(int a, String n, double amount)
{
accNum = a;
name = n;
balance = amount;
}
/* ====================================================
Default Constructor
==================================================== */
public BankAccount( )
{
accNum = -1;
name = "Nobody";
balance = 0.0;
}
/* ==============================================
deposit(amount): Add "amount" to balance
============================================== */
public void deposit( double amount )
{
balance += amount;
}
/* ======================================================
withdraw(amount): Subtract "amount" from balance
====================================================== */
public void withdraw( double amount )
{
if ( balance >= amount )
balance -= amount; // Subtract "amount" from balance
}
/* ==============================================
toString(): return a String containing
information of BankAccount
============================================== */
public String toString( )
{
return( "Account number: " + accNum
+ ", Name: " + name
+ ", Balance: " + balance);
}
}
|
|
public class CheckingAccount extends BankAccount
{
// Inherits:
// Variables: accNum, name, balance
// Methods: deposit(amount), withdraw(amount)
/* ---------------------------------------------
Add the transfer method to complete
the functionality requirements
---------------------------------------------- */
public void transfer( BankAccount toAccount, double amount )
{
deduct amount from this account;
add amount to "toAccount";
}
}
|
public class CheckingAccount extends BankAccount
{
// Inherits:
// Variables: accNum, name, balance
// Methods: deposit(amount), withdraw(amount)
/* -------------------------------------
transfer(toAccount, amount)
------------------------------------- */
public void transfer( BankAccount toAccount, double amount )
{
this.withdraw(amount); // deduct amount from this account;
toAccount.deposit(amount); // add amount to "toAccount";
}
}
|
public class TestChecking
{
public static void main( String[] args )
{
CheckingAccount acc1 = new CheckingAccount();
CheckingAccount acc2 = new CheckingAccount();
acc1.deposit( 1000.0 ); // uses an inherited method
acc2.deposit( 400.0 );
System.out.println( "acc1: " + acc1 ); // Uses toString() inherited from BankAccount
System.out.println( "acc2: " + acc2 );
acc1.transfer( acc2, 100.0 ); // Uses added method in CheckingAccount !
System.out.println( "After acc1.transfer( acc2, 100.0 );");
System.out.println( "acc1: " + acc1 );
System.out.println( "acc2: " + acc2 );
}
}
|
Output:
acc1: Account number: -1, Name: Nobody, Balance: 1000.0 acc2: Account number: -1, Name: Nobody, Balance: 400.0 After acc1.transfer( acc2, 100.0 ); acc1: Account number: -1, Name: Nobody, Balance: 900.0 acc2: Account number: -1, Name: Nobody, Balance: 500.0 |
How to run the program:
|
|
to the derived class to taylor the program to our needs.
Note:
|
|