0% found this document useful (0 votes)
24 views5 pages

Banking System N

System
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)
24 views5 pages

Banking System N

System
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

// Account.

java

public abstract class Account {

private String accountID;

private String accountHolder;

protected double balance;

public Account(String accountID, String accountHolder) {

this.accountID = accountID;

this.accountHolder = accountHolder;

this.balance = 0;

public void deposit(double amount) {

balance += amount;

System.out.println("Deposited: " + amount + ". New balance: " +


balance);

public abstract void withdraw(double amount);

public void checkBalance() {

System.out.println("Account balance: " + balance);

// SavingsAccount.java

public class SavingsAccount extends Account {


public SavingsAccount(String accountID, String accountHolder) {

super(accountID, accountHolder);

@Override

public void withdraw(double amount) {

if (balance >= amount) {

balance -= amount;

System.out.println("Withdrew: " + amount + ". New balance: " +


balance);

} else {

System.out.println("Insufficient balance.");

// CurrentAccount.java

public class CurrentAccount extends Account {

private double overdraftLimit;

public CurrentAccount(String accountID, String accountHolder, double


overdraftLimit) {

super(accountID, accountHolder);

this.overdraftLimit = overdraftLimit;

@Override
public void withdraw(double amount) {

if (balance + overdraftLimit >= amount) {

balance -= amount;

System.out.println("Withdrew: " + amount + ". New balance: " +


balance);

} else {

System.out.println("Overdraft limit exceeded.");

// Bank.java

import java.util.ArrayList;

import java.util.List;

public class Bank {

private List<Account> accounts;

public Bank() {

accounts = new ArrayList<>();

public void addAccount(Account account) {

accounts.add(account);

public void showAllAccounts() {


for (Account account : accounts) {

account.checkBalance();

// TestBank.java

public class TestBank {

public static void main(String[] args) {

Bank bank = new Bank();

Account savings = new SavingsAccount("001", "Alice");

Account current = new CurrentAccount("002", "Bob", 500);

Account savings2 = new SavingsAccount("003", "Charlie");

bank.addAccount(savings);

bank.addAccount(current);

bank.addAccount(savings2);

savings.deposit(1000);

current.deposit(2000);

savings2.deposit(1500);

savings.withdraw(200);

current.withdraw(2500);

savings2.withdraw(1600);
bank.showAllAccounts();

You might also like