Need to implement this into the code somehow, but I don't know how. Any tips? createReport(): this
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, and writes results into a text file in the format as displayed in the example above.
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 < 0) {
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();
}
}
Human Resource Management A Global And Critical Perspective
ISBN: 9781137521620
2nd Edition
Authors: Jawad Syed, J; Kramar Syed, Robin Kramar