0% found this document useful (0 votes)
23 views

Banks

Question
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Banks

Question
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

// Abstract class Account using Abstraction and Encapsulation

abstract class Account {

private String accountID;

private double balance;

public Account(String accountID, double initialBalance) {

this.accountID = accountID;

this.balance = initialBalance;

public String getAccountID() {

return accountID;

public double getBalance() {

return balance;

// Abstract method for Polymorphism

public abstract void withdraw(double amount);

// Encapsulation: hiding the details of deposit

public void deposit(double amount) {

if (amount > 0) {

balance += amount;

System.out.println("Deposited: " + amount);

} else {
System.out.println("Invalid deposit amount");

protected void setBalance(double balance) {

this.balance = balance;

// SavingsAccount class using Inheritance and Polymorphism

class SavingsAccount extends Account {

private double interestRate;

public SavingsAccount(String accountID, double initialBalance, double


interestRate) {

super(accountID, initialBalance);

this.interestRate = interestRate;

@Override

public void withdraw(double amount) {

double balance = getBalance();

if (amount > 0 && balance >= amount) {

setBalance(balance - amount);

System.out.println("Withdrawn from Savings: " + amount);

} else {

System.out.println("Insufficient funds in Savings");


}

// CurrentAccount class using Inheritance and Polymorphism

class CurrentAccount extends Account {

private double overdraftLimit;

public CurrentAccount(String accountID, double initialBalance, double


overdraftLimit) {

super(accountID, initialBalance);

this.overdraftLimit = overdraftLimit;

@Override

public void withdraw(double amount) {

double balance = getBalance();

if (amount > 0 && (balance + overdraftLimit) >= amount) {

setBalance(balance - amount);

System.out.println("Withdrawn from Current: " + amount);

} else {

System.out.println("Insufficient funds in Current");

// Bank class using Encapsulation


class Bank {

private List<Account> accounts = new ArrayList<>();

public void addAccount(Account account) {

accounts.add(account);

public Account getAccount(String accountID) {

for (Account account : accounts) {

if (account.getAccountID().equals(accountID)) {

return account;

return null;

// Test class for demonstration

public class BankManagementSystem {

public static void main(String[] args) {

Bank bank = new Bank();

// Creating different account objects

Account savings = new SavingsAccount("SA123", 1000.0, 0.05);

Account current = new CurrentAccount("CA123", 500.0, 200.0);

// Adding accounts to the bank


bank.addAccount(savings);

bank.addAccount(current);

// Performing operations

savings.deposit(200);

savings.withdraw(300);

current.deposit(150);

current.withdraw(700);

// Accessing accounts

Account acc1 = bank.getAccount("SA123");

if (acc1 != null) {

System.out.println("Balance for SA123: " + acc1.getBalance());

You might also like