0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Mortgage {

    private static double round(Double val) { // round the value to two decimal places
        int vala = (int) (val * 100);
        return (double) (vala / 100.00);

    }

    public static void main(String[] args) throws IOException {
        double loanAmount = 200000.00; // Loan Amount of the Mortgage
        double rate = 0.0575;   // Intrest rate of the Mortgage (yearly)
        int years = 30; //number of payments in the Mortgage in Years
        double payment = 0.00; //monthly payment of the Mortgage
        payment = (loanAmount * ((rate / 12) / (1 - Math.pow((1 + (rate / 12)), -(years * 12)))));
        double AmountLeftToPay = loanAmount; // this will hold amount left to pay after each payments(loan balance)
        double interest; // calculated interest for each month
        double actualprinciple; // actual amount payed ( monthly payment - interest)
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader bfr = new BufferedReader(isr);// creates the input reader to user interaction
        System.out.println("Loan Amount: $200,000");
        System.out.println(); // new line
        System.out.print("Month  "); // Displays the payment Header
        System.out.print("\t\tInterest Paid  ");//Displays the Interest Header
        System.out.print("\tActual Amount Paid  ");//Displays the actual pay Header
        System.out.print("Amount Left to Pay");
        System.out.println();//new line
        for (int i = 0; i < years * 12; i++) { //loops through payments

            interest = (AmountLeftToPay * (rate / 12));
            actualprinciple = (payment - interest);// actual amount payed ( monthly payment - interest)

            AmountLeftToPay = AmountLeftToPay - actualprinciple;


            if (i == years * 12 - 1) { // this  beacuse of java rounding  problem can give negative value
                AmountLeftToPay = 0;

            }
            if (i % 12 == 0 && i != 0) //displays the 12 payments per time and pause for user action
            {
                System.out.println("To Display the rest of the list Press Enter");
                bfr.readLine();


            }

            System.out.print((i + 1) + "                  "); // Displays the payment Number
            System.out.print(round(interest) + "           ");//Displays the Interest amount
            System.out.print(round(actualprinciple) + "            ");//Displays the actual pay amount
            System.out.print(round(AmountLeftToPay) );//Displays the Amount Left to pay
            System.out.println();//new line

        }

    }
    }

Edited by adamb0mb: Alignment Help

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by thines01
0

Have you looked into DecimalFormat?

import java.util.*;
import java.text.*;
// ...
DecimalFormat df=new DecimalFormat("$0.00");
// ...
System.out.println(p.getCode() + "\t" + p.getDescription() + " @ " + df.format(p.getPrice()));
// ...

Edited by thines01: Added Link

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.