Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}