[Japi-cvs] SF.net SVN: japi: [38] trunk/src/app/net/sf/japi
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2006-04-10 22:47:40
|
Revision: 38 Author: christianhujer Date: 2006-04-10 15:47:20 -0700 (Mon, 10 Apr 2006) ViewCVS: http://svn.sourceforge.net/japi/?rev=38&view=rev Log Message: ----------- Finance is not a util package, but a package of its own. Added Paths: ----------- trunk/src/app/net/sf/japi/finance/ trunk/src/app/net/sf/japi/finance/InterestCalculator.java trunk/src/test/net/sf/japi/finance/ trunk/src/test/net/sf/japi/finance/InterestCalculatorTest.java Removed Paths: ------------- trunk/src/app/net/sf/japi/util/finance/ trunk/src/test/net/sf/japi/util/finance/ Copied: trunk/src/app/net/sf/japi/finance/InterestCalculator.java (from rev 36, trunk/src/app/net/sf/japi/util/finance/InterestCalculator.java) =================================================================== --- trunk/src/app/net/sf/japi/finance/InterestCalculator.java (rev 0) +++ trunk/src/app/net/sf/japi/finance/InterestCalculator.java 2006-04-10 22:47:20 UTC (rev 38) @@ -0,0 +1,95 @@ +/* JAPI - (Yet another (hopefully) useful) Java API + * + * Copyright (C) 2006 Anja Heim + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +package net.sf.japi.finance; + +/** Class contains some functionality for calculating capital after some periods of time. + * @author <a href="mailto:miz...@we...">A. Heim</a> + */ +public class InterestCalculator { + + private final float initialCapital; + private float interestRate; + private float currentCapital; + + public InterestCalculator(final float initialCapital, final float interestRate) { + this.initialCapital = initialCapital; + this.interestRate = interestRate; + this.currentCapital = this.initialCapital; + } + + /** Calculates the overall capital after some periods beginnig with given initial capital and interest rate. + * <br />The formula is: <code>capital_after_n_periods = start_captial * ( 1 + p/100 )^n</code> + * (with p as interest rate) + * @param numberOfPeriods + * @return the overall capital after numberOfPeriods periods + */ + public float calculateCapital( int numberOfPeriods ) { + if ( numberOfPeriods < 0 ) throw new IllegalArgumentException("Number of periods has to be at least 1!"); + if ( numberOfPeriods == 0 ) + return initialCapital; + if( numberOfPeriods == 1 ) { + currentCapital = currentCapital *(1 + interestRate/100); + } else { + currentCapital = calculateCapital( numberOfPeriods - 1 ); + } + return currentCapital; + } + + /** Returns the current capital. + * + * @return current capital + */ + public float getCurrentCapital() { + return currentCapital; + } + + /** Sets new current capital. + * + * @param currentCapital + */ + public void setCurrentCapital(float currentCapital) { + this.currentCapital = currentCapital; + } + + /** Sets new interest rate. + * + * @param interestRate + */ + public void setInterestRate(float interestRate) { + this.interestRate = interestRate; + } + + /** Returns the current interest rate. + * + * @return the current interest rate + */ + public float getInterestRate() { + return interestRate; + } + + /** Resets the current capital to initial capital. + * + */ + public void resetCapital() { + this.currentCapital = this.initialCapital; + } + +} // class InterestCalculator Property changes on: trunk/src/app/net/sf/japi/finance/InterestCalculator.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Copied: trunk/src/test/net/sf/japi/finance/InterestCalculatorTest.java (from rev 37, trunk/src/test/net/sf/japi/util/finance/InterestCalculatorTest.java) =================================================================== --- trunk/src/test/net/sf/japi/finance/InterestCalculatorTest.java (rev 0) +++ trunk/src/test/net/sf/japi/finance/InterestCalculatorTest.java 2006-04-10 22:47:20 UTC (rev 38) @@ -0,0 +1,89 @@ +/* JAPI - (Yet another (hopefully) useful) Java API + * + * Copyright (C) 2006 Anja Heim + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +package test.net.sf.japi.finance; + +import junit.framework.TestCase; +import net.sf.japi.finance.InterestCalculator; + +/** Tests the capital finance class. + * Date: 09.04.2006 + * + */ +public class InterestCalculatorTest extends TestCase { + + InterestCalculator ic = new InterestCalculator(100, 15); + + public void testInterestCalculator() throws Exception { + //ic = new InterestCalculator(100, 15); + } + + /** Tests capital after 0, 1, 2 , 3 years. + * @throws Exception + */ + public void testCalculateCapital() throws Exception { + //ic = new InterestCalculator(100, 15); + + + /* first try it with invalid argument */ + try { + ic.calculateCapital(-1); + fail("Expected IllegalArgumentException."); + } catch (final IllegalArgumentException ignore) { + /* ignore */ + } + /* then use some different periods */ + assertEquals("Capital after a time period of 0 must be the starting capital.", (float)100.0 , ic.calculateCapital( 0 ) ); + assertEquals( (float)115.0 , ic.calculateCapital( 1 ) ); + assertEquals( (float)132.25 , ic.calculateCapital( 2 ) ); + assertEquals( (float)152.0875, ic.calculateCapital( 3 ) ); + } + + public void testGetCurrentCapital() throws Exception { + assertEquals( (float)100.00, ic.getCurrentCapital() ); + ic.calculateCapital(1); + assertEquals( (float)115.00, ic.getCurrentCapital() ); + } + + public void testSetCurrentCapital() throws Exception { + assertEquals( (float)100.00, ic.getCurrentCapital() ); + ic.calculateCapital(1); + assertEquals( (float)115.00, ic.getCurrentCapital() ); + ic.setCurrentCapital((float)100.00); + assertEquals( (float)100.00, ic.getCurrentCapital() ); + } + + public void testGetInterestRate() throws Exception { + assertEquals( (float)15, ic.getInterestRate() ); + } + + public void testSetInterestRate() throws Exception { + ic.setInterestRate(16); + assertEquals( (float)16, ic.getInterestRate() ); + } + + public void testResetCapital() throws Exception { + ic.calculateCapital(1); + assertEquals( (float)115, ic.getCurrentCapital() ); + ic.resetCapital(); + assertEquals( (float)100, ic.getCurrentCapital() ); + } + +} // class InterestCalculatorTest Property changes on: trunk/src/test/net/sf/japi/finance/InterestCalculatorTest.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |