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

OOB

The document describes a banking application with Account, CheckingAccount, and SavingsAccount classes that represent different types of bank accounts. The Bank class maintains a list of accounts and can update balances, add accounts, and retrieve a total balance. The example creates a bank with checking and savings accounts, updates the balances, and prints the total balance.

Uploaded by

Murosama
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)
28 views5 pages

OOB

The document describes a banking application with Account, CheckingAccount, and SavingsAccount classes that represent different types of bank accounts. The Bank class maintains a list of accounts and can update balances, add accounts, and retrieve a total balance. The example creates a bank with checking and savings accounts, updates the balances, and prints the total balance.

Uploaded by

Murosama
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

import java.util.

ArrayList;

import java.util.List;

public abstract class Account {

// instance variables

protected String accountNumber;

protected double balance;

// constructor

public Account(String accountNumber, double balance) {

this.accountNumber = accountNumber;

this.balance = balance;

// methods

public void deposit(double amount) {

balance += amount;

public void withdraw(double amount) {

balance -= amount;

public double getBalance() {

return balance;

public abstract void updateBalance();

public class CheckingAccount extends Account {


private double monthlyFee;

public CheckingAccount(String accountNumber, double balance, double monthlyFee) {

super(accountNumber, balance);

this.monthlyFee = monthlyFee;

@Override

public void updateBalance() {

balance -= monthlyFee;

public class SavingsAccount extends Account {

private double interestRate;

public SavingsAccount(String accountNumber, double balance, double interestRate) {

super(accountNumber, balance);

this.interestRate = interestRate;

@Override

public void updateBalance() {

balance += balance * interestRate;

public class Bank {

private List<Account> accounts;

public Bank() {
accounts = new ArrayList<>();

public void addAccount(Account account) {

accounts.add(account);

public void updateAccountBalances() {

for (Account account : accounts) {

account.updateBalance();

public double getTotalBalance() {

double total = 0.0;

for (Account account : accounts) {

total += account.getBalance();

return total;

public class Main {

public static void main(String[] args) {

Bank bank = new Bank();

bank.addAccount(new CheckingAccount("123456", 1000.00, 10.00));

bank.addAccount(new SavingsAccount("789012", 2000.00, 0.01));

bank.updateAccountBalances();

System.out.println("Total balance: " + bank.getTotalBalance());

}
}

Aciklama :

The Account class is an abstract class that represents a bank account. It has instance variables for the
account number and balance, and a constructor to initialize these values. It also has three methods:
deposit, withdraw, and getBalance.

The updateBalance method is an abstract method that must be implemented by any concrete
subclass of Account. It is used to update the balance of the account based on some specific rule,
such as applying monthly fees or interest.

The CheckingAccount class is a concrete subclass of Account that represents a checking account. It
has an additional instance variable for the monthly fee and a constructor to initialize all of its
instance variables.

The updateBalance method in the CheckingAccount class subtracts the monthly fee from the
balance.

The SavingsAccount class is a concrete subclass of Account that represents a savings account. It has
an additional instance variable for the interest rate and a constructor to initialize all of its instance
variables.

The updateBalance method in the SavingsAccount class increases the balance by the interest rate.

The Bank class maintains a list of Account objects and has methods for adding accounts, updating
the balances of all accounts, and calculating the total balance of all accounts.

The addAccount method adds a new Account object to the list.

The updateAccountBalances method calls the updateBalance method on each Account object in the
list.

The getTotalBalance method calculates the total balance of all accounts by adding up the balances of
each Account object in the list.
The Main class creates a Bank object and adds a CheckingAccount and a SavingsAccount to it. It then
calls the updateAccountBalances method to update the balances of all accounts, and prints the total
balance using the getTotalBalance method.

You might also like