Untitled
unknown
plain_text
10 months ago
1.8 kB
17
Indexable
package com.helloWorld;
import java.text.NumberFormat;
import java.util.Scanner;
public class Mortgage {
public static void main(String[] args) {
final byte MONTHS_IN_YEAR = 12;
final byte PERCENT = 100;
int principal = 0;
float monthlyInterest = 0;
int numberOfPayments = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Principal ($1k - $1M): ");
principal = scanner.nextInt();
if (principal >= 1_000 && principal <= 1_000_000)
break;
System.out.println("Enter a number between 1,000 and 1,000,000");
}
while (true) {
System.out.print("Annual Interest Rate: ");
float annualInterest = scanner.nextFloat();
if (annualInterest >= 1 && annualInterest <= 30) {
monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEAR;
break;
}
System.out.println("Enter a number greater than 0 and less than or equal to 30");
}
while (true) {
System.out.print("Period (Years): ");
int years = scanner.nextInt();
numberOfPayments = years * MONTHS_IN_YEAR;
if (years >= 1 && years <= 30)
break;
System.out.println("Enter a number between 1 and 30");
}
double mortgageTotal = principal
* (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments))
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);
String formatedTotal = NumberFormat.getCurrencyInstance().format(mortgageTotal);
System.out.println("Mortgage: " + formatedTotal);
}
}
Editor is loading...
Leave a Comment