Solution 2:
Save the file as Account.java
import java.util.Scanner;
public class Account
{
/**
* data members
*/
private double balance;
private Scanner keyboardInput;
/**
* one argument constructor
* @param balance
*/
public Account(double balance)
{
this.balance = balance;
keyboardInput=new Scanner(System.in);
}
/**
* member function to display initial balance
*/
public void displayInitialBalance()
{
System.out.printf("Initial Balance= %.2f\n",balance);
}
/**
* member function to withdraw amount
*/
public void withdrawAmount()
{
double withdrawalAmount;
String strWithdrawalAmount;
System.out.println("ENTER THE WITHDRAWAL AMOUNT: ");
strWithdrawalAmount=keyboardInput.nextLine();
try
{
withdrawalAmount=Double.parseDouble(strWithdrawalAmount);
if(withdrawalAmount>0 && (balance-withdrawalAmount)>=0)
balance-=withdrawalAmount;
else
{
System.out.println("INVALID AMOUNT OR INSUFFICIENT BALANCE");
System.out.println("TRY AGAIN");
withdrawAmount();
}
}
catch(NumberFormatException nME)
{
System.out.println("INPUT NOT A NUMBER");
System.out.println("TRY AGAIN");
withdrawAmount();
}
}
/**
* member function to deposit amount
*/
public void depositAmount()
{
double depositedAmount;
String strDepositedAmount;
System.out.println("ENTER THE DEPOSITED AMOUNT: ");
strDepositedAmount=keyboardInput.nextLine();
try
{
depositedAmount=Double.parseDouble(strDepositedAmount);
if(depositedAmount>0)
balance+=depositedAmount;
else
{
System.out.println("INVALID AMOUNT");
System.out.println("TRY AGAIN");
withdrawAmount();
}
}
catch(NumberFormatException nME)
{
System.out.println("INPUT NOT A NUMBER");
System.out.println("TRY AGAIN");
depositAmount();
}
}
/**
* accessor function for balance
* @return
*/
public double getBalance()
{
return balance;
}
/**
* @param balance the balance to set
*/
public void setBalance(double balance)
{
this.balance = balance;
}
@Override
public String toString()
{
return String.format("Balance= %.2f",balance);
}
}
----------------------------------------------END------------------------------------
Save the file as SavingsAccount.java
public class SavingsAccount extends Account
{
private static final double INTEREST_RATE_PERCENT=3.5;
double interest;
/**
* one argument constructor
* @param balance
*/
public SavingsAccount(double balance)
{
super(balance);
}
/**
* member function to calculate interest
*/
public void calculateInterest()
{
//interest calculation for 1 year period
interest=getBalance()*INTEREST_RATE_PERCENT/100;
credit();
}
/**
* member function to add interest to balance
*/
public void credit()
{
setBalance(getBalance()+interest);
}
@Override
public String toString() {
return String.format("%s\nSavingsAccount interest= %.2f",super.toString(),interest);
}
}
----------------------------------------------------------END------------------------
Save the file as CheckingAccount.java
public class CheckingAccount extends Account
{
/**
* one argument constructor
* @param balance
*/
public CheckingAccount(double balance)
{
super(balance);
}
}
-----------------------------------------------END-----------------------------------
Save the file as AccountTest.java
public class AccountTest
{
public static void main(String[] arg)
{
//savings account objects
SavingsAccount sA1=new SavingsAccount(500.00);
SavingsAccount sA2=new SavingsAccount(200.00);
SavingsAccount sA3=new SavingsAccount(100.00);
//checking account objects
CheckingAccount cA1=new CheckingAccount(400.00);
CheckingAccount cA2=new CheckingAccount(450.00);
CheckingAccount cA3=new CheckingAccount(600.00);
//initialize the array with Accounts
Account accounts[]=new Account[6];
accounts[0]=sA1;
accounts[1]=sA2;
accounts[2]=sA3;
accounts[3]=cA1;
accounts[4]=cA2;
accounts[5]=cA3;
System.out.println("Accounts processed polymorphically:\n" );
int counter=1;
// generically process each element in array accounts
for(Account currentAccount : accounts)
{
System.out.println("Account #"+counter++);
currentAccount.displayInitialBalance();
currentAccount.withdrawAmount();
currentAccount.depositAmount();
if(currentAccount instanceof SavingsAccount)
{
((SavingsAccount) currentAccount).calculateInterest();
}
System.out.printf("Updated Balance: %.2f\n\n",currentAccount.getBalance());
}
System.out.println("BALANCE IN EACH ACCOUNT");
for(int i=0;i<accounts.length;i++)
{
System.out.println("Account is a "+accounts[i].getClass().getName());
System.out.printf("%s\n\n",accounts[i]);
}
}
}
------------------------------------------END---------------------------------------- |