diff --git a/src/main/java/com/williamfiset/algorithms/math/CompoundInterest.java b/src/main/java/com/williamfiset/algorithms/math/CompoundInterest.java new file mode 100644 index 000000000..7bdf884ad --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/math/CompoundInterest.java @@ -0,0 +1,14 @@ +package com.williamfiset.algorithms.math; + +public class CompoundInterest { + public static double CompoundInterest(double principalAmount, double interestRateDecimal, double compoundsPerYear, double totalYears) { + double unroundedNum = (principalAmount * Math.pow((1+(interestRateDecimal/compoundsPerYear)),compoundsPerYear*totalYears)); + //rounds to the nearest cent + return Math.round(unroundedNum*100.00)/100.00; + } + public static void main(String[] args) { + //example cases + System.out.println(CompoundInterest(100,0.12,12,2)); + System.out.println(CompoundInterest(2001,0.21,4,8)); + } +} diff --git a/src/test/java/com/williamfiset/algorithms/math/CompoundInterestTest.java b/src/test/java/com/williamfiset/algorithms/math/CompoundInterestTest.java new file mode 100644 index 000000000..f8e0354e6 --- /dev/null +++ b/src/test/java/com/williamfiset/algorithms/math/CompoundInterestTest.java @@ -0,0 +1,18 @@ +package com.williamfiset.algorithms.math; + +import org.junit.Test; + +import static com.google.common.truth.Truth.assertThat; + +import java.util.*; + +public class CompoundInterestTest { + @Test + //example tests + public void testInterest() { + assertThat(CompoundInterest.CompoundInterest(120,0.1,12,2)).isEqualTo(146.45); + assertThat(CompoundInterest.CompoundInterest(.5,0.25,4,12)).isEqualTo(9.18); + assertThat(CompoundInterest.CompoundInterest(12000,0.05,1,5)).isEqualTo(15315.38); + assertThat(CompoundInterest.CompoundInterest(10,0.1,2,10)).isEqualTo(26.53); + } +}