CompMortgage

Updated Dani 0 Tallied Votes 198 Views Share

A simple demonstration of how MyInput.class can be used to input data from the keyboard. Computation is then done to compute the interest rate on a mortage.

package compmortgage;
public class CompMortgage {
    // Main method
    public static void main(String[] args) {
        double annualInterestRate;
        int numOfYears;
        double loanAmount;
        String prompt = "\n\nEnter yearly interest rate, for example 8.25 ";
        // Enter monthly interest rate
        System.out.println(prompt);
        annualInterestRate = MyInput.readDouble();
        while (annualInterestRate > 0) {
            // Obtain monthly interest rate
            double monthlyInterestRate = annualInterestRate / 1200;
            // Enter number of years
            System.out.println(
                "Enter number of years as an integer, for example 5: ");
            numOfYears = MyInput.readInt();
            // Enter loan amount
            System.out.println("Enter loan amount, for example 120000.95: ");
            loanAmount = MyInput.readDouble();
            // Calculate payment
            double monthlyPayment = loanAmount * monthlyInterestRate /
                (1 - (Math.pow(1 / (1 + monthlyInterestRate), numOfYears * 12)));
            double totalPayment = monthlyPayment * numOfYears * 12;
            // Display results
            System.out.println("The monthly payment is " + monthlyPayment);
            System.out.println("The total payment is " + totalPayment);
            // Enter monthly interest rate
            System.out.println(prompt + "(0 to exit) ");
            annualInterestRate = MyInput.readDouble();
        }
    }
}