目录
题目:*17.6 (存储 Loan 对象)
在程序清单10-2中的类 Loan 没有实现 Serializable,改写类 Loan 使之实现 Serializable。编写程序创建 5 个 Loan 对象,并且将它们存储在一个名为Excrcise17_06.dat的文件中
-
习题思路
- 在Loan类的声明后面实现Serializable接口,再为类添加一个toString方法。
- 实例化5个Loan。
- 为Excrcise17_06.dat创建一个ObjectOutputStream,写入五个Loan对象。
- 使用ObjectInputStream读取五个Loan对象,并调用loan.toString方法输出描述对象的字符串。
-
代码示例
编程练习题17_6StoreLoanObject.java
package chapter_17;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class 编程练习题17_6StoreLoanObject {
public static void main(String[] args)throws ClassNotFoundException, IOException {
Loan loan1 = new Loan(2.5, 5, 10000);
Loan loan2 = new Loan(3.0, 5, 20000);
Loan loan3 = new Loan(3.5, 5, 30000);
Loan loan4 = new Loan(4.0, 5, 40000);
Loan loan5 = new Loan(4.5, 5, 50000);
try(
ObjectOutputStream output =
new ObjectOutputStream(new FileOutputStream("src/Text/Exercise17_06.dat"));
){
output.writeObject(loan1);
output.writeObject(loan2);
output.writeObject(loan3);
output.writeObject(loan4);
output.writeObject(loan5);
}catch (FileNotFoundException e) {
System.out.println("FileNotFound");
}
try(
ObjectInputStream input =
new ObjectInputStream(new FileInputStream("src/Text/Exercise17_06.dat"));
){
Loan l1 = (Loan)(input.readObject());
Loan l2 = (Loan)(input.readObject());
Loan l3 = (Loan)(input.readObject());
Loan l4 = (Loan)(input.readObject());
Loan l5 = (Loan)(input.readObject());
System.out.println(l1.toString());
System.out.println(l2.toString());
System.out.println(l3.toString());
System.out.println(l4.toString());
System.out.println(l5.toString());
}
}
}
Loan.java
package chapter_17;
import java.io.Serializable;
public class Loan implements Serializable{
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private java.util.Date loanDate;
public Loan() {
this(2.5, 1, 1000);
}
public Loan(double annualInterestRate, int numberOfYears,
double loanAmount) {
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new java.util.Date();
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public int getNumberOfYears() {
return numberOfYears;
}
public void setNumberOfYears(int numberOfYears) {
this.numberOfYears = numberOfYears;
}
public double getLoanAmount() {
return loanAmount;
}
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
}
public double getMonthlyPayment(){
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1-
(1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)));
return monthlyPayment;
}
public double getTotalPayment() {
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
return totalPayment;
}
public java.util.Date getLoanDate(){
return loanDate;
}
public String toString() {
return "Your loan interest is " + getAnnualInterestRate() + ", duration is " + getNumberOfYears() + ", and amount is " + getLoanAmount();
}
}
-
输出结果
Your loan interest is 2.5, duration is 5, and amount is 10000.0
Your loan interest is 3.0, duration is 5, and amount is 20000.0
Your loan interest is 3.5, duration is 5, and amount is 30000.0
Your loan interest is 4.0, duration is 5, and amount is 40000.0
Your loan interest is 4.5, duration is 5, and amount is 50000.0