Question
Java- LoanApp Calculator. Can anyone help me with my code? Here are the requirements: The Data Entry section prompts the user to enter values for
Java- LoanApp Calculator. Can anyone help me with my code?
Here are the requirements:
The Data Entry section prompts the user to enter values for the loan amount, yearly interest rate, and number of years. If the user doesnt enter data thats valid, this section displays an
appropriate error message and prompts the user again.
The Formatted Results section displays a formatted version of the users entries as well as the formatted result of the calculation.
The application prompts the user to continue.
The formula for calculating monthly payment is:double monthlyPayment =loanAmount * monthlyInterestRate/(1-1/Math.pow(1 + monthlyInterestRate, months));
Use the Scannerclass to get all user input.
Accept decimal entries for the loan amount and interest rate entries.
Accept integer values for the years field.
Accept integer and decimal values within the following ranges:
Greater Than Less Than
Loan amount: 0 1,000,000
Yearly interest rate: 0 20
Years: 0 100
Accept the values y, Y, N or n at theContinue? prompt.
Continue only if the user entersy orY at theContinue?prompt.
Display an appropriate, informative error message if the user enters invalid data and continue to prompt the user again until the user enters valid data.
Store validation code in separate methods. For example:
public static double getDoubleWithinRange(Scanner sc, String prompt,double min, double max)
public static double getDouble(Scanner sc, String prompt)
public static int getIntWithinRange(Scanner sc, String prompt, int min,int max)
public static int getInt(Scanner sc, String prompt)
public static String getRequiredString(Scanner sc, String prompt)
public static String getValidString(Scanner sc, String prompt, String s1, String s2)
Use NumberFormat to format output values.
Display Loan amount and Monthly payment as U.S. currency2 decimal places, a dollar symbol($).
DisplayInterest rate as a percentalways show 1 decimal place with a percent symbol (%).
Include a welcome message and section heads for DATA ENTRY and FORMATTED RESULTS
This is what I have:
//import statements
import java.text.NumberFormat;
import java.util.Scanner;
public class LoanAppMGT
{
public static void main(String[] args)
{
// create scanner object
Scanner sc = new Scanner(System.in);
//continue so long as choice is "y" or "Y"
String choice = "y";
while(choice.equalsIgnoreCase("y"))
{
//get input from user
System.out.println("DATA ENTRY");
double loanAmount = getDoubleWithinRange(sc, "Enter loan amount: ", 0, 1000000);
double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 20);
double years = getDoubleWithinRange(sc, "Enter number of years: ", 0, 100);
//convert yearly values to monthly values
double monthlyinterestRate = interestRate/12/100;
int months = years * 12;
//call the future value method
double monthlyPayment = calculateMonthlyPayment(loanAmount, interestRate, months);
//format and display the result
System.out.println("FORMATTED RESULTS");
System.out.println("Loan Amount: " + loanAmount);
System.out.println("Yearly interest rate: " + interestRate);
System.out.println("Number of years: " + years);
NumberFormat currency = NumberFormat.getCurrencyInstance();
System.out.println("Monthly payment: " + currency.format(monthlyPayment));
System.out.println();
//see if user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}// end while loop
}// end main() method
// Validate VALID input
public static double getDoubleWithinRange(Scanner sc, String prompt, double min, double max)
{
//declare local variable
double d = 0.0;
boolean isValid = false;
while(isValid == false)
{
// get the data before we test
d = getDouble(sc, prompt);
// Error line for invalid data
if(d<=min)
System.out.println("Error! Number must be greater than " + min + ".");
else if(d >= max)
System.out.println("Error! Number must be less than " + max + ".");
else
isValid = true;
}// end while loop
//return the valid data within main()
return d;
}// end getDoubleWithinRange
public static double getDouble(Scanner sc, String prompt)
{
//declare local variables
double d = 0.0;
boolean isValid = false;
while(isValid == false)
{
System.out.print(prompt);
if(sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid decimal value. Try again." );
}
// discard another data on the line
sc.nextLine();
}// end while loop
// return the valid data to getDoubleWithinRange()
return d;
}//end getDoulbe()
//calculate
//monthly interest rate and months
private static double calculateLoanAmount(double loanAmount, double interestRate, int months)
{
//declare loanAmount variable here
double monthlyPayment = loanAmount * monthlyinterestRate/(1-1/Math.pow(1 + monthlyinterestRate, months));
}// end calculation method
}// end class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started