Created
December 10, 2020 17:04
-
-
Save jamesxsc/c2f7a038bc5b91d98145449d3e0d96de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.text.DecimalFormat; | |
| class Scratch { | |
| public static void main(String[] args) { | |
| try { | |
| int interestPercentage = Integer.parseInt(args[0]); | |
| int repaymentPercentage = Integer.parseInt(args[1]); | |
| double repaid = calculateTotalRepaid(interestPercentage, repaymentPercentage); | |
| DecimalFormat decimalFormat = new DecimalFormat("#.00"); | |
| System.out.printf("Total amount repaid: $%s\n", decimalFormat.format(repaid)); | |
| } catch (NumberFormatException | ArrayIndexOutOfBoundsException ex) { | |
| throw new RuntimeException("Invalid input!", ex); | |
| } | |
| } | |
| private static double calculateTotalRepaid(int interestPercentage, int repaymentPercentage) { | |
| int minRepayment = 50; | |
| double debt = 100d; | |
| double paid = 0; | |
| while (debt > 0d) { | |
| // add interest | |
| debt *= 1 + (interestPercentage / 100d); | |
| // perform payment | |
| double repayment = Math.min( | |
| Math.max( | |
| minRepayment, | |
| debt * (repaymentPercentage / 100d) | |
| ), debt); | |
| debt -= repayment; | |
| paid += repayment; | |
| } | |
| return paid; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment