Skip to main content

Mortgage Calculator with User Input and Mathematical Expression - Example0005

Mortgage Calculator with User Input and Mathematical Expression - Example0005

In this code sample, we will see how mortgage(EMI) calculator can be made
Mortgage formula M=P * (r(1+r)**n)/((1+r)**n-1) will be used.
Scanner Class will be used to get user input.
We need to get the rightly formatted output (with dollar sign as currency). For this purpose we will use Class NumberFormat:NumberFormat.getCurrencyInstance.
We use NumberFormat.getCurrencyInstance method.

Keyword: java, mortgage calculator, user input, scanner, currency format, dollar sign, numberformat, getcurrencyinstance

package com.swprogramdeveloper;


import java.text.NumberFormat;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
// write your code here

        //Mortgage Calculator

        Scanner userInput = new Scanner(System.in);

        System.out.print("Please Input the Principal Amount : ");
        float principal = userInput.nextFloat();

        System.out.print("Please Input the Annual Interest Rate : ");
        float monthlyInterest = userInput.nextFloat() /12/100;

        System.out.print("Please Input Period of Mortgage in years : ");
        float mortgageMonths = userInput.nextFloat() * 12;

        System.out.println("Principal : " + principal + "Monthly Interest : " + monthlyInterest + " Mortgage Months : " + mortgageMonths);

        //Mortage Formula
        // M=P * (r(1+r)**n)/((1+r)**n-1)
        //principal * (monthlyInterest(1+monthlyInterest)**mortageMonths)/(((1+monthlyInterest)**mortageMonths-1)
        float multiplier= (float) Math.pow((1+monthlyInterest),mortgageMonths);
        float mortgage;
        mortgage = (principal*monthlyInterest*multiplier)/(multiplier-1);

        System.out.println("multiplier : " + multiplier);
        System.out.println("mortgage EMI : " + mortgage);
       
        // To get currency format, we use NumberFormat Class
        NumberFormat mortgageCurrency = NumberFormat.getCurrencyInstance();
        String finalMortgage=mortgageCurrency.format(mortgage);
        System.out.println("mortgage EMI(with currency style) : " + finalMortgage);

    }
}



Output0005
==========

Please Input the Principal Amount : 100000
Please Input the Annual Interest Rate : 3.92
Please Input Period of Mortgage in years : 30
Principal : 100000.0Monthly Interest : 0.0032666668 Mortgage Months : 360.0
multiplier : 3.2352057
mortgage EMI : 472.8128
mortgage EMI(with currency style) : $472.81

Process finished with exit code 0

Comments

Popular posts from this blog

Basic Java Program Various Variables and Point - Example0002

Basic Java Program Various Variables and Point - Example 0002 This example is same as Example1 but with added Class Point Using Point - (It is to store x and y coordinates of a point) This is example of a basic java program which shows structure of the program. It is basic java program showing different type of Java variables. Primitive types: Integer, float, double, Boolean are shown Creating an object. Reference types and methods to that object. How to print output in a screen is shown using System Class package com.swprogramdeveloper; import java.awt.*; import java.util.Date; public class Main {     public static void main(String[] args) {         // write your code here         //variable types double and intege r         double age = 30;         int herage = (int) age + 20;         //Printing output using System Class         Sy...

Creating Class with attributes and method and using them- Example0006

Creating Class with attributes and method and using them- Example0006 This example shows creating own Class We will create a class called 'Product' with Attribute, Contructor and Methods We will see how we can create an Object of that Class and use the methods in that Class.  We can see two ways to access the attributes of the Class Keyword: java, class, attribute, constructor, methods, object, instance Creating own Class and use it ====================== package com.swprogramdeveloper; //Textual presentation of an object, how it will look like in the memory //Whatever we write in class is actually belong to object. Note: if you want something for class, need to write static class Product {     //Attribute         int pid;         String name;         int price;         //Constructor         Product() {         System.out.printl...

Basic Java Program with Variable Introduction and Class Introduction - Example0001

Basic Java Program with Variable Introduction and Class Introduction - Example0001  Example1: ========= This is example of a basic java program which shows structure of the program. It is basic java program showing different type of Java variables. Primitive types: Integer, float, double, Boolean are shown Creating an object. Reference types and methods to that object. How to print output in a screen is shown using System Class package com.swprogramdeveloper; import java.util.Date; public class Main {     public static void main(String[] args) {  // write your code here      //variable types double and integer         double age = 30;         int herage = (int) age + 20;   //Printing output using System Class         System.out.println("Hello World "+age + "---" + herage);      //float type. Note the F- it is required as the compiler will take doub...