1 /**
2 * Investor, the open-source investment library
3 *
4 * (C) Copyright 2008, by individual contributors as indicated by the @author tag.
5 *
6 * This library is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this software; if not, write to the Free
18 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
20 */
21 package com.nickokiss.investor.calc;
22
23 import java.math.BigDecimal;
24
25 /**
26 * <pre>
27 * V = A * (1 + r*t)
28 *
29 * V - future value
30 * A - present value
31 * r - interest rate
32 * t - time (in years)
33 *
34 * Additional variables:
35 * V/A - growth
36 * r*t - scaled interest rate
37 * </pre>
38 *
39 * @author Tomasz Koscinski <tomasz.koscinski@nickokiss.com>
40 */
41 public class SimpleInterestCalc {
42
43 private MathCalc mathCalc = new MathCalc();
44
45 /**
46 * V = A * (1 + r*t)
47 */
48 public BigDecimal getFutureValue(BigDecimal presentValue, BigDecimal interestRate, BigDecimal time) {
49
50 BigDecimal scaledInterestRate = interestRate.multiply(time);
51 BigDecimal growth = scaledInterestRate.add(mathCalc.ONE);
52 BigDecimal futureValue = growth.multiply(presentValue);
53 return futureValue;
54 }
55
56 /**
57 * TRANSFORMATION:
58 *
59 * <pre>
60 * V = A * (1 + r*t)
61 * A = V / (1 + r*t)
62 * </pre>
63 */
64 public BigDecimal getPresentValue(BigDecimal futureValue, BigDecimal interestRate, BigDecimal time) {
65
66 // r*t
67 BigDecimal scaledInterestRate = interestRate.multiply(time);
68 // r*t + 1
69 BigDecimal growth = scaledInterestRate.add(mathCalc.ONE);
70 BigDecimal presentValue = mathCalc.div(futureValue, growth);
71 return presentValue;
72 }
73
74 /**
75 * TRANSFORMATION:
76 *
77 * <pre>
78 * V = A * (1 + r*t)
79 * 1 + r*t = V / A
80 * r*t = V/A - 1
81 * r = (V/A - 1) / t
82 * </pre>
83 */
84 public BigDecimal getInterestRate(BigDecimal presentValue, BigDecimal futureValue, BigDecimal time) {
85
86 BigDecimal growth = mathCalc.div(futureValue, presentValue);
87 BigDecimal scaledInterestRate = growth.subtract(mathCalc.ONE);
88 BigDecimal interestRate = mathCalc.div(scaledInterestRate, time);
89 return interestRate;
90 }
91
92 /**
93 * TRANSFORMATION:
94 *
95 * <pre>
96 * V = A * (1 + r*t)
97 * 1 + r*t = V / A
98 * r*t = V/A - 1
99 * t = (V/A - 1) / r
100 * </pre>
101 */
102 public BigDecimal getTime(BigDecimal presentValue, BigDecimal futureValue, BigDecimal interestRate) {
103
104 BigDecimal growth = mathCalc.div(futureValue, presentValue);
105 BigDecimal scaledInterestRate = growth.subtract(mathCalc.ONE);
106 BigDecimal time = mathCalc.div(scaledInterestRate, interestRate);
107 return time;
108 }
109
110 }