Pls Help me!!!
package com.codewithmosh;
import java.text.NumberFormat;
import java.util.Scanner;
public class Main {
final static byte MONTH_IN_YEAR = 12;
final static byte PERCENT = 100;
public static void main(String[] args) {
int principal=(int)readNumber("Principal: ",1000 , 1000000);
float annualInterest=(float)readNumber("Annual Interest rate: ",1 , 30 );
byte years=(byte)readNumber("Period(Years): " , 1 , 30 );
printMortgage(principal, years, annualInterest);
printPaymentDetails(principal, years, annualInterest);
}
public static void printMortgage(int principal, byte years, float annualInterest) {
double monthlyPayment=calculateMortgage(principal, annualInterest, years);
System.out.println("MORTGAGE");
System.out.println("--------");
System.out.println("Monthly Payments = "+ NumberFormat.getCurrencyInstance().format(monthlyPayment));
}
public static void printPaymentDetails(int principal, byte years, float annualInterest) {
System.out.println("Payment Schedule");
System.out.println("----------------");
for(double month = 1; month<= years *MONTH_IN_YEAR; month++) {
double balance = calculateBalance(principal, annualInterest, years, month);
System.out.println(NumberFormat.getCurrencyInstance().format(balance));
}
}
public static double readNumber(String prompt , double min , double max){
Scanner scanner = new Scanner(System.in);
float value= 0;
while(true){
System.out.println(prompt);
value= scanner.nextFloat();
if(value>=min && value<=max)
break;
System.out.println("Enter a value greater than "+min+" and less than "+max);
}
return value;
}
public static double calculateMortgage(int principal , float annualInterest , byte years){
double monthlyInterest= annualInterest / PERCENT / MONTH_IN_YEAR;
short numberOfPayment= (short) (years * 12);
double monthlyPayment;
monthlyPayment = principal * ( monthlyInterest * Math.pow ( 1 + monthlyInterest ,numberOfPayment ) /
Math.pow ( 1 + monthlyInterest , numberOfPayment )-1 );
return monthlyPayment;
}
public static double calculateBalance(int principal , float annualInterest , byte years, double numberOfPaymentsMade){
double monthlyInterest= annualInterest / PERCENT / MONTH_IN_YEAR;
short numberOfPayment= (short) (years * 12);
double balance = principal * (Math.pow(1+monthlyInterest,numberOfPayment)-Math.pow(1+monthlyInterest,numberOfPaymentsMade)/Math.pow(1+monthlyInterest,numberOfPayment)-1);
return balance;
}
}`
output:
180000
Annual Interest rate:
4
Period(Years):
10
MORTGAGE
--------
Monthly Payments = -₹179,400.00
Payment Schedule
----------------
-₹32,790.48
-₹33,194.28
-₹33,599.43
-₹34,005.92
-₹34,413.78
-₹34,822.99
-₹35,233.57
-₹35,645.51
-₹36,058.83
-₹36,473.52
-₹36,889.60
-₹37,307.07
-₹37,725.92
-₹38,146.18
-₹38,567.83
-₹38,990.89
-₹39,415.36
-₹39,841.24
-₹40,268.55
-₹40,697.27
-₹41,127.43
-₹41,559.02
-₹41,992.05
-₹42,426.52
-₹42,862.45
-₹43,299.82
-₹43,738.65
-₹44,178.95
-₹44,620.71
-₹45,063.95
-₹45,508.66
-₹45,954.85
-₹46,402.54
-₹46,851.71
-₹47,302.38
-₹47,754.56
-₹48,208.24
-₹48,663.43
-₹49,120.14
-₹49,578.38
-₹50,038.14
-₹50,499.43
-₹50,962.26
-₹51,426.64
-₹51,892.56
-₹52,360.03
-₹52,829.07
-₹53,299.66
-₹53,771.83
-₹54,245.57
-₹54,720.88
-₹55,197.79
-₹55,676.28
-₹56,156.37
-₹56,638.05
-₹57,121.35
-₹57,606.25
-₹58,092.77
-₹58,580.91
-₹59,070.68
-₹59,562.08
-₹60,055.12
-₹60,549.81
-₹61,046.14
-₹61,544.13
-₹62,043.77
-₹62,545.08
-₹63,048.07
-₹63,552.73
-₹64,059.07
-₹64,567.10
-₹65,076.82
-₹65,588.25
-₹66,101.37
-₹66,616.21
-₹67,132.76
-₹67,651.04
-₹68,171.04
-₹68,692.78
-₹69,216.25
-₹69,741.47
-₹70,268.45
-₹70,797.17
-₹71,327.66
-₹71,859.92
-₹72,393.95
-₹72,929.77
-₹73,467.37
-₹74,006.76
-₹74,547.95
-₹75,090.94
-₹75,635.74
-₹76,182.36
-₹76,730.80
-₹77,281.07
-₹77,833.17
-₹78,387.12
-₹78,942.91
-₹79,500.55
-₹80,060.05
-₹80,621.42
-₹81,184.65
-₹81,749.77
-₹82,316.77
-₹82,885.66
-₹83,456.44
-₹84,029.13
-₹84,603.73
-₹85,180.24
-₹85,758.67
-₹86,339.03
-₹86,921.33
-₹87,505.57
-₹88,091.75
-₹88,679.89
-₹89,269.99
-₹89,862.06
-₹90,456.10
-₹91,052.12
-₹91,650.12
Process finished with exit code 0
Why am i getting negative value??