Advertisement
AlexandrP

LongDeposit

Mar 17th, 2024
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.epam.rd.qa.aggregation;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5.  
  6. public class LongDeposit extends Deposit {
  7.     public LongDeposit(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 rawInterest;
  17.             BigDecimal interest;
  18.             if (i > 6) {
  19.                 rawInterest = currentAmount.multiply(BigDecimal.valueOf(0.15));
  20.             } else {
  21.                 rawInterest = BigDecimal.ZERO; // No interest for the first 6 months
  22.             }
  23.             interest = rawInterest.setScale(2, RoundingMode.HALF_EVEN);
  24.  
  25.             totalIncome = totalIncome.add(interest);
  26.             currentAmount = currentAmount.add(interest);
  27.         }
  28.         return totalIncome;
  29.     }
  30.  
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement