001 /* JAPI - (Yet another (hopefully) useful) Java API
002 *
003 * Copyright (C) 2006 Anja Heim
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License as
007 * published by the Free Software Foundation; either version 2 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful, but
011 * WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018 * 02111-1307, USA.
019 */
020
021 package net.sf.japi.finance;
022
023 /** Class contains some functionality for calculating capital after some periods of time.
024 * @author <a href="mailto:mizzzora@web.de">A. Heim</a>
025 */
026 public class InterestCalculator {
027
028 private final float initialCapital;
029 private float interestRate;
030 private float currentCapital;
031
032 public InterestCalculator(final float initialCapital, final float interestRate) {
033 this.initialCapital = initialCapital;
034 this.interestRate = interestRate;
035 this.currentCapital = this.initialCapital;
036 }
037
038 /** Calculates the overall capital after some periods beginnig with given initial capital and interest rate.
039 * <br />The formula is: <code>capital_after_n_periods = start_captial * ( 1 + p/100 )^n</code>
040 * (with p as interest rate)
041 * @param numberOfPeriods
042 * @return the overall capital after numberOfPeriods periods
043 */
044 public float calculateCapital( int numberOfPeriods ) {
045 if ( numberOfPeriods < 0 ) throw new IllegalArgumentException("Number of periods has to be at least 1!");
046 if ( numberOfPeriods == 0 )
047 return initialCapital;
048 if( numberOfPeriods == 1 ) {
049 currentCapital = currentCapital *(1 + interestRate/100);
050 } else {
051 currentCapital = calculateCapital( numberOfPeriods - 1 );
052 }
053 return currentCapital;
054 }
055
056 /** Returns the current capital.
057 *
058 * @return current capital
059 */
060 public float getCurrentCapital() {
061 return currentCapital;
062 }
063
064 /** Sets new current capital.
065 *
066 * @param currentCapital
067 */
068 public void setCurrentCapital(float currentCapital) {
069 this.currentCapital = currentCapital;
070 }
071
072 /** Sets new interest rate.
073 *
074 * @param interestRate
075 */
076 public void setInterestRate(float interestRate) {
077 this.interestRate = interestRate;
078 }
079
080 /** Returns the current interest rate.
081 *
082 * @return the current interest rate
083 */
084 public float getInterestRate() {
085 return interestRate;
086 }
087
088 /** Resets the current capital to initial capital.
089 *
090 */
091 public void resetCapital() {
092 this.currentCapital = this.initialCapital;
093 }
094
095 } // class InterestCalculator