Saturday, 12 October 2013

Program to demonstrate Abstract class and methods

//Program to demonstrate Abstract class and methods
//Program by Ahlam Ansari

abstract class BankAccount
{

    public abstract void deposit(float anAmount);

    public abstract float getBalance();
   
    public abstract void updateBalance(float aBalance);

    public abstract void withdraw(float anAmount);

}

class SavingAccount extends BankAccount
{
    private String ownersName;
    private int accountNumber;
    private float balance;
    private float saveLimit;

    public SavingAccount(int anAccountNumber, String aName, float aLimit, float aBalance)
    {
        accountNumber = anAccountNumber;
        ownersName = aName;
        balance = aBalance;
        saveLimit = aLimit;
    }

    public void deposit(float anAmount)
    {
        if (anAmount>0.0)
            balance = balance + anAmount;
    }

    public float getBalance()
    {
        return balance;
    }
   
    public void updateBalance(float aBalance)
    {
        balance= aBalance;
    }


    public void withdraw(float anAmount)
    {
        float aBalance;
        if ((anAmount>0.0) && (getBalance()>anAmount)&&(getBalance()-anAmount>=saveLimit))
        {
            aBalance = getBalance() - anAmount;
            updateBalance(aBalance);
        }
        else
            System.out.println("Cannot withdraw as balance is insufficient");
    }

}

public class MyAccountAbstract
{
    public static void main(String ahlam[])
    {
         SavingAccount anAccount = new SavingAccount(8801, "Ahlam Ansari", 10000.0f , 50000.0f);
         anAccount.deposit(57890.0f);
         System.out.println("Your Current Balance is Rs." +anAccount.getBalance()+"/-");
         anAccount.withdraw(11890.0f);
         System.out.println("Your Current Balance is Rs." +anAccount.getBalance()+"/-");
    }
}

/*
 C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac MyAccountAbstra
ct.java

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MyAccountAbstrac
t
Your Current Balance is Rs.107890.0/-
Your Current Balance is Rs.96000.0/-

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
 */

No comments:

Post a Comment