w3resource

Java Abstract Classes - Abstract Bank Account Class with Savings and Current Accounts

Java Abstract Class: Exercise-3 with Solution

Write a Java program to create an abstract class BankAccount with abstract methods deposit() and withdraw(). Create subclasses: SavingsAccount and CurrentAccount that extend the BankAccount class and implement the respective methods to handle deposits and withdrawals for each account type.

In the following code, the BankAccount class is an abstract class with an abstract deposit() method and an abstract withdraw() method. It also has a private instance variable balance of type double and corresponding getter and setter methods. The SavingsAccount class and the CurrentAccount class are the subclasses that extend the BankAccount class. They implement the deposit() and withdraw() methods based on the specific rules for each account type.

Sample Solution:

Java Code:

// BankAccount.java
// Abstract class Shape

abstract class BankAccount {
    private String accountNumber;
    private double balance;

    public BankAccount(String accountNumber, double balance) {
        this.accountNumber = accountNumber;
        this.balance = balance;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public double getBalance() {
        return balance;
    }

    protected void setBalance(double balance) {
        this.balance = balance;
    }

    public abstract void deposit(double amount);

    public abstract void withdraw(double amount);
}

// SavingsAccount.java
// Subclass SavingsAccount

class SavingsAccount extends BankAccount {
    public SavingsAccount(String accountNumber, double balance) {
        super(accountNumber, balance);
    }

    @Override
    public void deposit(double amount) {
        setBalance(getBalance() + amount);
        System.out.println("Deposit of $" + amount + " successful. Current balance: $" + getBalance());
    }

    @Override
    public void withdraw(double amount) {
        if (getBalance() >= amount) {
            setBalance(getBalance() - amount);
            System.out.println("Withdrawal of $" + amount + " successful. Current balance: $" + getBalance());
        } else {
            System.out.println("Insufficient funds. Withdrawal failed.");
        }
    }
}
// CurrentAccount.java
// Subclass CurrentAccount

class CurrentAccount extends BankAccount {
    public CurrentAccount(String accountNumber, double balance) {
        super(accountNumber, balance);
    }

    @Override
    public void deposit(double amount) {
        setBalance(getBalance() + amount);
        System.out.println("Deposit of $" + amount + " successful. Current balance: $" + getBalance());
    }

    @Override
    public void withdraw(double amount) {
        if (getBalance() >= amount) {
            setBalance(getBalance() - amount);
            System.out.println("Withdrawal of $" + amount + " successful. Current balance: $" + getBalance());
        } else {
            System.out.println("Insufficient funds. Withdrawal failed.");
        }
    }
}
// Main.java
// Subclass Main

public class Main {
    public static void main(String[] args) {
		double ibal,damt,wamt;
        ibal = 1000.00;
        SavingsAccount savingsAccount = new SavingsAccount("SA001", ibal);
		System.out.println("Savings A/c: Initial Balace: $"+ibal);
		damt = 500.00;
        savingsAccount.deposit(damt);
		wamt = 250.00;
        savingsAccount.withdraw(wamt);
		wamt = 1600.00;
		System.out.println("\nTry to withdraw: $"+wamt);
        savingsAccount.withdraw(wamt);

        System.out.println();
        ibal = 5000.00;
        CurrentAccount currentAccount = new CurrentAccount("CA001", ibal);
		System.out.println("Current A/c: Initial Balace: $"+ibal);
		damt = 2500.00;
        currentAccount.deposit(1000.0);
		wamt = 1250.00;
        currentAccount.withdraw(3000.0);
		wamt = 6000.00;
		System.out.println("\nTry to withdraw: $"+wamt);
        savingsAccount.withdraw(wamt);		
    }
}

Sample Output:

Savings A/c: Initial Balace: $1000.0
Deposit of $500.0 successful. Current balance: $1500.0
Withdrawal of $250.0 successful. Current balance: $1250.0

Try to withdraw: $1600.0
Insufficient funds. Withdrawal failed.

Current A/c: Initial Balace: $5000.0
Deposit of $1000.0 successful. Current balance: $6000.0
Withdrawal of $3000.0 successful. Current balance: $3000.0

Try to withdraw: $6000.0
Insufficient funds. Withdrawal failed.

Flowchart:

Flowchart: Abstract class Shape
Flowchart: Subclass SavingsAccount
Flowchart: Subclass CurrentAccount
Flowchart: Subclass Main

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Abstract Shape Class with Circle and Triangle Subclasses.
Next: Abstract Animal Class with Lion, Tiger, and Deer Subclasses.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.