Package com.cedarsoft.business.calc

Source Code of com.cedarsoft.business.calc.InterestCalculator

package com.cedarsoft.business.calc;

import com.cedarsoft.business.Money;
import com.cedarsoft.business.MutableMoney;
import com.cedarsoft.business.payment.Payment;
import org.jetbrains.annotations.NotNull;
import org.joda.time.LocalDate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Calculates interests and related numbers
*/
public class InterestCalculator {
  private double interestRate;

  @NotNull
  private final List<Payment> payments = new ArrayList<Payment>();
  @NotNull
  private final InterestCalculationSystem calculationSystem;
  @NotNull
  private final TimeSystem timeSystem;

  /**
   * Creates a new interest calculator.
   * It is necessary to set the interest rate ({@link #setInterestRate(double)})
   *
   * @param calculationSystem the calculation system
   * @param timeSystem        the time system
   */
  @Deprecated
  public InterestCalculator( @NotNull InterestCalculationSystem calculationSystem, @NotNull TimeSystem timeSystem ) {
    this.calculationSystem = calculationSystem;
    this.timeSystem = timeSystem;
  }

  /**
   * Creates a new interest calculator
   *
   * @param calculationSystem the calculation system
   * @param timeSystem        the time system
   * @param interestRate      the interest rate
   */
  public InterestCalculator( @NotNull InterestCalculationSystem calculationSystem, @NotNull TimeSystem timeSystem, double interestRate ) {
    this.calculationSystem = calculationSystem;
    this.timeSystem = timeSystem;
    this.interestRate = interestRate;
  }

  /**
   * Sets the interest rate
   *
   * @param interestRate the interest rate
   */
  public void setInterestRate( double interestRate ) {
    this.interestRate = interestRate;
  }

  /**
   * Adds a payment
   *
   * @param payment the payment
   */
  public void addPayment( @NotNull Payment payment ) {
    payments.add( payment );
  }

  /**
   * Returns the interest rate
   *
   * @return the rate
   */
  public double getInterestRate() {
    return interestRate;
  }

  /**
   * Calculates the interest until the given date.
   * This method requires that at least one payment {@link #addPayment(Payment)}
   * and the interest rate {@link #setInterestRate(double)} are set
   *
   * @param until the end date
   * @return the interest
   */
  @NotNull
  public Money calculateInterestUntil( @NotNull LocalDate until ) {
    if ( payments.isEmpty() ) {
      throw new IllegalStateException( "Need at least one payment" );
    }
    if ( interestRate == 0 ) {
      throw new IllegalStateException( "Interest rate not set" );
    }

    MutableMoney sum = new MutableMoney();

    for ( Payment payment : payments ) {
      double years = timeSystem.calculateYears( payment.getDate(), until );
      double days = timeSystem.calculateDays( payment.getDate(), until );
      sum.plus( calculationSystem.calculateInterest( payment.getAmount(), years, days, interestRate ) );
    }

    return sum.immutable();
  }

  /**
   * Returns the payments
   *
   * @return the payments
   */
  @NotNull
  public List<Payment> getPayments() {
    return Collections.unmodifiableList( payments );
  }

  /**
   * Returns the calculation system
   *
   * @return the calculation system
   */
  @NotNull
  public InterestCalculationSystem getCalculationSystem() {
    return calculationSystem;
  }

  /**
   * Returns the time system
   *
   * @return the time system
   */
  @NotNull
  public TimeSystem getTimeSystem() {
    return timeSystem;
  }
}
TOP

Related Classes of com.cedarsoft.business.calc.InterestCalculator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.