Hello, everyone. I need help on my JAVA project. I was able to write my code, but I do not get the output that I desire. I also need to validate user input. For example, all dollar amounts are >=0. Down Payment has to be atleast 10% of total cost or 1000, whichever is less. Interest rate is between 0-25% and length of loan is 1-48 months. Thanks in advance
This is the output that I am supposed to get is in the attachment
This is my code:
import java.text.DecimalFormat;
import java.util.Scanner;
public class testproj2
{
public static void main(String[] args)
{
double autoPrice; //Price of Automobile
double extWarr; //Price of Extended Warranty
double salesTax; //Sales Tax
int lenLoan; //Length of Loan
double monPay; //Monthly Pay
double totCost; //Total Cost
double downPay; //Down Payment
double amtFin; //Amount Financed
double intRate; //Interest Rate
final double taxPercent = 0.0625; //Sales Tax Percent
double rateDec; //Interest Rate as Decimal
double monRate; //Monthy Interest Rate
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
//Create a DecimalFormat object
DecimalFormat formatter = new DecimalFormat("0.00");
String correctInputCheck = "";
char correctInputCheckChar; // Used to create the condition for the following do-while loop
// Checks if the user is satisfied with the input and proceeds if so. Mistakes can be corrected
// if the user is not satisfied with the input. All amounts will be re-read by this loop.
do
{
//Input Price of Automobile
System.out.print("What is the price of your automobile?");
autoPrice = keyboard.nextDouble();
if (autoPrice <0)
{
System.out.print("Invalid Input. Price of Car must be greater than 0");}
else
{//Prompt for Extended Warranty
System.out.print("Please, type the cost of the extended Warranty. Or, press 0 if not purchased.");
extWarr = keyboard.nextDouble();
if (extWarr <0)
{
System.out.print("Invalid Input. Price of Extended Warranty must be 0 or Higher");
}
else{
//Calculate Sales Tax
salesTax = taxPercent * autoPrice;
//Calculate Total Cost
totCost = autoPrice + extWarr + salesTax;
//Input Down Payment
System.out.print("How much is the down payment?");
downPay = keyboard.nextDouble() ;
//Input interest rate
System.out.print("Annual Interest Rate: ");
intRate = keyboard.nextDouble() ;
if (intRate < 0 && intRate > 25)
{System.out.print("Invalid Input. Interest Rate has to between 0-25");
}
else {
//Length of Loan input
System.out.print("Length of Loan (in months): ");
lenLoan = keyboard.nextInt();
if (lenLoan < 1 && intRate > 48)
{ System.out.print("Invalid Input. Length of Loan has to be between 1-48");
}
else {
//Calculate Sales Tax
salesTax = taxPercent * autoPrice;
//Calculate Total Cost
totCost = autoPrice + extWarr + salesTax;
//Calculate Amount Financed
amtFin = totCost - downPay;
//Calculate Monthly Pay
if (intRate > 0)
{ rateDec = intRate/100 ;
monRate = rateDec/12;
monPay = (amtFin * monRate) / (1 - Math.pow(1 / (1 + monRate),
lenLoan));}
else
{monPay =(amtFin/lenLoan);}
// Display Report
System.out.println(" Deals on Wheels!");
System.out.println("\nAutomobile Loan Schedule -- prepared for "
+ "Kunal Patel");
System.out.println("\nAutomobile price:" + " $"
+ formatter.format(autoPrice));
System.out.println("Extended Warranty: "
+ formatter.format(extWarr));
System.out.println("Sales Tax: "
+ formatter.format(salesTax));
System.out.println(" ________");
System.out.println("Total Cost" + " $"
+ formatter.format(totCost));
System.out.println("Down payment: "
+ formatter.format(downPay));
System.out.println(" ________");
System.out.println("Amt. Financed:" + " $"
+ formatter.format(amtFin));
System.out.println("\nAnnual interest rate: "
+ formatter.format(intRate) + "%");
System.out.println("Length of Loan: "
+ lenLoan + " months");
System.out.println("Monthly Payment " + "$"
+ formatter.format(monPay));
System.out.println("\n\nType \"y\"/\"Y\" if the input is correct. Type any other key to retype the data: ");
correctInputCheck = keyboard.next();
correctInputCheckChar = correctInputCheck.charAt(0);
} while()(!(correctInputCheckChar == 'y' || correctInputCheckChar == 'Y'));
double monthlyInterestAmount = monRate * amtFin;
double monthlyBalance = amtFin + monthlyInterestAmount - monPay;
double yearlyInterest = 0;
int m = 0; // # of months counter for table generation
int n = 0; // # of months counter for number of years to generate tables for
for (n = 12; n <= lenLoan; n += 12)
{
if (n % 12 == 0)
{
System.out.println("\n\nAmortization Schedule for Year " + n/12 + " :\n" +
"Month" + "\t" + "Interest" + "\t" + "Payment" + "\t\t" + "Balance");
for (m = 1; m <= 12; m++)
{
System.out.println(m +
"\t" + formatter.format(monthlyInterestAmount) +
"\t\t" + formatter.format(monPay) +
"\t\t" + formatter.format(monthlyBalance));
yearlyInterest += monthlyInterestAmount;
monthlyInterestAmount = monthlyBalance * monthlyInterestRate;
monthlyBalance = monthlyBalance + monthlyInterestAmount - monPay;
if (m == 12)
{
System.out.println("Pmt : " + formatter.format(m * monPay));
System.out.println("Interest: " + formatter.format(yearlyInterest));
System.out.println("Principal: " + formatter.format(m * monPay - yearlyInterest));
System.out.println("Monthly Payment: " + formatter.format(m * monPay - yearlyInterest));
System.out.println("Balance: " + formatter.format(amtFin - (m * monPay - yearlyInterest)));
System.out.println("\n***************************************************");
}
}
}
}
}
}
}