Question: Storing borrow objects in a file. Take the Loan class as a starting point from listing 10.2 in the textbook, this is attached. Write about
Storing borrow objects in a file. Take the Loan class as a starting point from listing 10.2 in the textbook, this is attached. Write about the class to implement Serializeable interface. Write about the program in listing 10.1 TestLoanClass.java (attached) as follows: The program must: Load loan objects from file at startup, if there is a file with such. All read loan objects are stored in an ArrayList or equivalent. Information about read-in loan objects is printed, print out the total loan amount and monthly payment. New loan objects can be created Write loan objects, all or a selection, to file if the user wants this. Tips: When loading objects from a file, use EOFException to stop loading, if you do not know the number to be read.
Loan.java
package chapter10;
public class Loan {
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private java.util.Date loanDate;
/** Default constructor */
public Loan() { this(2.5, 1, 1000); }
/** Construct a loan with specified annual interest rate, number of years and loan amount */
public Loan(double annualInterestRate, int numberOfYears, double loanAmount) { this.annualInterestRate = annualInterestRate; this.numberOfYears = numberOfYears; this.loanAmount = loanAmount; loanDate = new java.util.Date(); }
/** Return annualInterestRate */
public double getAnnualInterestRate() { return annualInterestRate; }
/** Set a new annualInterestRate */
public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; }
/** Return numberOfYears */
public int getNumberOfYears() { return numberOfYears; }
/** Set a new numberOfYears */
public void setNumberOfYears(int numberOfYears) { this.numberOfYears = numberOfYears; }
/** Return loanAmount */
public double getLoanAmount() { return loanAmount; }
/** Set a newloanAmount */
public void setLoanAmount(double loanAmount) { this.loanAmount = loanAmount; }
/** Find monthly payment */
public double getMonthlyPayment() { double monthlyInterestRate = annualInterestRate / 1200; double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
return monthlyPayment; }
/** Find total payment */
public double getTotalPayment() { double totalPayment = getMonthlyPayment() * numberOfYears * 12; return totalPayment; }
/** Return loan date */
public java.util.Date getLoanDate() { return loanDate; }}
---------------------------------------------
TestLoanClass.java
package chapter10;
import java.util.Scanner;
public class TestLoanClass {
/** Main method */
public static void main(String[] args) {
// Create a Scanner Scanner input = new Scanner(System.in); //
Enter yearly interest rate System.out.print( "Enter yearly interest rate, for example, 8.25: ");
double annualInterestRate = input.nextDouble();
// Enter number of years System.out.print("Enter number of years as an integer: "); int numberOfYears = input.nextInt();
// Enter loan amount System.out.print("Enter loan amount, for example, 120000.95: "); double loanAmount = input.nextDouble(); //
Create Loan object Loan loan = new Loan(annualInterestRate, numberOfYears, loanAmount);
// Display loan date, monthly payment, and total payment System.out.printf("The loan was created on %s " + "The monthly payment is %.2f The total payment is %.2f ", loan.getLoanDate().toString(), loan.getMonthlyPayment(), loan.getTotalPayment()); }}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
