Java语言程序设计基础篇_编程练习题*17.6 (存储 Loan 对象)

目录

题目:*17.6 (存储 Loan 对象)

习题思路 

代码示例 

编程练习题17_6StoreLoanObject.java

Loan.java 

输出结果


题目:*17.6 (存储 Loan 对象)

  在程序清单10-2中的类 Loan 没有实现 Serializable,改写类 Loan 使之实现 Serializable。编写程序创建 5 个 Loan 对象,并且将它们存储在一个名为Excrcise17_06.dat的文件中

  • 习题思路 
  1. 在Loan类的声明后面实现Serializable接口,再为类添加一个toString方法。
  2. 实例化5个Loan。
  3. 为Excrcise17_06.dat创建一个ObjectOutputStream,写入五个Loan对象。
  4. 使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值