Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Documentation

Name: Seidygali Daryn


Group: IT1-2107

Decorator Pattern

This code is an example of using the Decorator pattern in Java to model a bank
account with various additional functionalities, such as interest calculation and
commission charges. Let's break down the code step by step and explain how it
works in simple terms.

1. Interface BankAccount:
- Defines the basic interface for a bank account with a single method
`getBalance()`, which returns the current account balance.

2. Class BasicAccount:
- Implements the BankAccount interface.
- Represents the basic bank account with an initial balance specified when the
account is created.
- The `getBalance()` method returns the current account balance.

3. Class InterestDecorator:
- Extends the BankAccount interface.
- This class is a decorator that adds the functionality of calculating interest to
an existing bank account.
- When the `getBalance()` method is called, it retrieves the current balance
from the base account, multiplies it by the interest rate, and returns the new
balance.

4. Class CommissionDecorator:
- Extends the BankAccount interface.
- This class is also a decorator that adds the functionality of charging a
commission to an existing bank account.
- When the `getBalance()` method is called, it retrieves the current balance
from the base account, subtracts the commission (a percentage of the balance),
and returns the new balance.

5. In the `main` method of the BankExample class:


- An instance of the Scanner class is created for inputting data from the
keyboard.
- Then, a basic bank account (BasicAccount) is created with an initial balance,
which is entered via the keyboard.
- Next, the user is given the option to add two additional functionalities:
- Interest calculation: The user enters an interest rate, and an
InterestDecorator object is created to add this functionality to the account.
- Commission charges: The user enters a commission rate as a decimal
fraction, and a CommissionDecorator object is created to add this functionality
to the account.
- Finally, the program displays the final account balance after applying all the
decorators.

As a result of running the program, you will get the final balance, taking into
account the initial balance, interest calculation, and commission charges,
depending on the values entered by the user. This demonstrates how the
Decorator pattern allows you to add various functionalities to an object without
changing its core structure.

CODE
import java.util.Scanner;

public class BankExample {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Введите начальный баланс: ");


double initialBalance = scanner.nextDouble();
BankAccount account = new BasicAccount(initialBalance);

System.out.print("Введите процентную ставку: ");


double interestRate = scanner.nextDouble();
account = new InterestDecorator(account, interestRate);

System.out.print("Введите комиссию (в виде десятичной доли): ");


double commissionRate = scanner.nextDouble();
account = new CommissionDecorator(account, commissionRate);

System.out.println("Итоговый баланс: " + account.getBalance());


}
}

interface BankAccount {
double getBalance();
}
class BasicAccount implements BankAccount {
private double balance;
public BasicAccount(double balance) {
this.balance = balance;
}

@Override
public double getBalance() {
return balance;
}
}
class CommissionDecorator implements BankAccount {
private BankAccount account;
private double commissionRate;

public CommissionDecorator(BankAccount account, double commissionRate)


{
this.account = account;
this.commissionRate = commissionRate;
}

@Override
public double getBalance() {
double balance = account.getBalance();
return balance - (balance * commissionRate);
}
}
class InterestDecorator implements BankAccount {
private BankAccount account;
private double interestRate;

public InterestDecorator(BankAccount account, double interestRate) {


this.account = account;
this.interestRate = interestRate;
}

@Override
public double getBalance() {
double balance = account.getBalance();
return balance * (1 + interestRate);
}
}

DIAGRAM

You might also like