Untitled
unknown
plain_text
3 years ago
1.9 kB
14
Indexable
import java.util.Scanner;
import java.text.DecimalFormat;
public class PayOffDebt {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
// Get User Input Of Principle, Annual Interest Rate, Monthly Payment,
System.out.print("Principle:\t\t\t");
double principle = keyboard.nextDouble();
System.out.print("Annual Interest Rate (%):\t");
double annualInterestRate = keyboard.nextDouble();
System.out.print("Monthly Payment:\t\t");
double monthlyPayment = keyboard.nextDouble();
System.out.println();
// Calculate raw value of months needed to pay off
double rawMonthsNeededToPayOff = (Math.log(monthlyPayment) - Math.log(monthlyPayment - (annualInterestRate/1200.0) * principle))
/ Math.log((annualInterestRate / 1200.0) + 1);
// Turning it into an integer type
int monthsNeededToPayOff = (int)Math.ceil(rawMonthsNeededToPayOff);
// Calculating totalAmountPaid, totalInterestPaid, overPayment
double totalAmountPaid = monthlyPayment * monthsNeededToPayOff;
double totalInterestPaid = totalAmountPaid - principle;
double overPayment = (monthlyPayment * monthsNeededToPayOff) - (monthlyPayment * rawMonthsNeededToPayOff);
// Define the format for the dollar amount to have two digits after the decimal point
DecimalFormat df = new DecimalFormat("0.00");
// Return the results
System.out.println("Months Needed To Pay Off:\t" + monthsNeededToPayOff);
System.out.println("Total Amount Paid:\t\t$" + df.format(totalAmountPaid));
System.out.println("Total Interest Paid:\t\t$" + df.format(totalInterestPaid));
System.out.println("Overpayment:\t\t\t$" + df.format(overPayment));
keyboard.close();
}
}Editor is loading...