Saturday 7 January 2017

Chapter 17 Exercise 17, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

17.7 (Restore objects from a file)
Suppose a file named Exercise17_07.dat has been created using
the ObjectOutputStream. The file contains Loan objects. The Loan
class in Listing 10.2 does not implement Serializable. Rewrite the
Loan class to implement Serializable. Write a program that reads the
Loan objects from the file and displays the total loan amount. Suppose
you don’t know how many Loan objects are there in the file,
use EOFException to end the loop.

import java.io.Serializable;

public class Loan implements Serializable {
    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 -
                (1 / Math.pow(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;
    }
}

import java.io.*;

public class Exercise_07 {

    public static void main(String[] args) throws IOException, ClassNotFoundException {

        File file = new File("Exercise17_07.dat");

        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {

            double totalAmount = 0;
            try {
                Loan loan;
                while ((loan = (Loan)in.readObject()) != null) {

                totalAmount += loan.getLoanAmount();
                }
            } catch (EOFException ex) {

                System.out.println("Total loan amount = " + totalAmount);
            }
        }
    }
}

No comments :

Post a Comment