Question: Need to implement this into the code somehow, but I don't know how. Any tips? createReport(): this void method calculates monthly interest, principal and balance,

Need to implement this into the code somehow, but I don't know how. Any tips?

createReport(): this void method calculates monthly interest, principal and balance, and writes results into a text file in the format as displayed in the example above.


Monthly Payment: $430.10 Month Interest Principal Balance 1234 24.58 405.52 4,594.48 22.59

import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;

public class Amortization {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.##");

do {
/* Data members */
double loan;
int years;
double interestRate;

/* Get amount, interest rate, and years from the user */
System.out.println("Enter the loan amount: ");
loan = keyboard.nextDouble();
System.out.println("Enter the annual interest rate (e.g., for 7.5% enter 7.5): ");
interestRate = keyboard.nextDouble() / 100.0;
System.out.println("Enter the years of the loan: ");
years = keyboard.nextInt();

double payment = calcPayment(loan, interestRate, years);

if (payment System.out.println("Invalid input values.");
} else {
System.out.println("Monthly Payment: " + df.format(payment));
int numberOfPayments = getNumberOfPayments(years);
System.out.println("Total Number of Payments: " + numberOfPayments);

// You can implement the createReport method to save these details to a file.
// createReport("LoanAmortization.txt");
}

System.out.println("Would you like to run another report? Enter Y for yes or N for no: ");
} while (keyboard.next().equalsIgnoreCase("Y"));

keyboard.close();
}

public static double calcPayment(double loan, double interestRate, int years) {
double term = Math.pow(1 + (interestRate / 12), years * 12);
double payment = (loan * (interestRate / 12) * term) / (term - 1);
return payment;
}

public static int getNumberOfPayments(int years) {
return years * 12; // Assuming monthly payments for the given number of years
}

public static void createReport(String filename) throws IOException {
// Sample implementation to save data to a file
PrintWriter out = new PrintWriter(new FileWriter(filename, true));
out.println("Sample Data");
out.close();
}
}
 

Monthly Payment: $430.10 Month Interest Principal Balance 1234 24.58 405.52 4,594.48 22.59 407.51 4,186.97 20.59 409.52 3,777.45 18.57 411.53 3,365.92 5 16.55 413.55 2,952.37 6 14.52 415.59 2,536.78 7 12.47 417.63 2,119.15 8 10.42 419.68 1,699.47 9 8.36 421.75 1,277.72 10 6.28 423.82 853.90 11 4.20 425.90 428.00 12 2.10 428.00 0.00

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!