Advertisement
AlexandrP

SpecialDeposit

Mar 17th, 2024
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. package com.epam.rd.qa.aggregation;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5.  
  6. public class SpecialDeposit extends Deposit {
  7.     public SpecialDeposit(BigDecimal amount, int period) {
  8.         super(amount, period);
  9.     }
  10.  
  11.     @Override
  12.     public BigDecimal income() {
  13.         BigDecimal totalIncome = BigDecimal.ZERO;
  14.         BigDecimal currentAmount = getAmount();
  15.         for (int i = 1; i <= getPeriod(); i++) {
  16.             BigDecimal divisor = BigDecimal.valueOf(100);
  17.             BigDecimal interestRate = BigDecimal.valueOf(i);
  18.             BigDecimal interestFraction = interestRate.divide(divisor, 2, RoundingMode.HALF_EVEN);
  19.             BigDecimal rawInterest = currentAmount.multiply(interestFraction);
  20.             BigDecimal interest = rawInterest.setScale(2, RoundingMode.HALF_EVEN);
  21.  
  22.             totalIncome = totalIncome.add(interest);
  23.             currentAmount = currentAmount.add(interest);
  24.         }
  25.         return totalIncome;
  26.     }
  27.  
  28. }
  29.  
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement