Skip to content

Instantly share code, notes, and snippets.

@jamesxsc
Created December 10, 2020 17:04
Show Gist options
  • Select an option

  • Save jamesxsc/c2f7a038bc5b91d98145449d3e0d96de to your computer and use it in GitHub Desktop.

Select an option

Save jamesxsc/c2f7a038bc5b91d98145449d3e0d96de to your computer and use it in GitHub Desktop.
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