【java文件处理】图书借阅(增加图书信息存储)

题目

第一版详见【java类与对象】图书借阅
第二版新增:将各种信息保存在文件中。
 

代码

相比第一版,其实只有图书类增加了一个信息存储到文件的方法,学生类和Test类都没有变,就不粘了

图书类

package Homework002;

import java.io.*;
import java.util.*;

public class Books {
	//数据成员
	private String name;
	private String number;
	private String stuName;
	private int borrowed;//是否被借出
	
	//构造方法
	public Books(String name,String number)
	{
		this.name=name;
		this.number=number;
		this.borrowed=0;
	}
	
	//一般方法
	public String getStuName() {
		return stuName;
	}

	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	public String getName() {
		return name;
	}
	
	//借书
	public void lend(String stuName)
	{
		this.stuName=stuName;
		this.borrowed=1;
	}
	
	//还书
	public void Return()
	{
		borrowed=0;
	}
	
	//显示书籍信息
	public void display()
	{
		System.out.println("图书信息");
		System.out.print("书名:"+name+"\n书号:"+number+"\n借阅情况:");
		if(borrowed==0)
			System.out.println("未借出");
		else
			System.out.println("已借出\n"+"借书学生:"+stuName);
		
		store();
	}
	
	//将图书借阅信息存储在文件中
	public void store()
	{
		try
		{
		    //PrintWriter类可以文本格式写入各种类型的数据
			PrintWriter out=new PrintWriter(new FileWriter("bookInfo.txt",true));//true代表追加,否则每次会覆盖前一次内容
			
			//用properties显示每一次时间
			Properties res=new Properties();
			res.store(out,"Book Information");
			
			out.println("书名:"+name);
			out.println("书号:"+number);
			out.print("借阅情况:");
			if(borrowed==0)
				out.println("未借出");
			else
			{
				out.println("已借出");
				out.println("借书学生:"+stuName);
			}
			
			out.println();
			out.close();
		}
		catch(IOException exception)
		{
			exception.printStackTrace();
		}
	}
}

 

运行结果

存储到文件中的信息

#Book Information
#Wed Apr 01 16:36:23 GMT+08:00 2020
书名:Gone with the wind
书号:001
借阅情况:已借出
借书学生:Amy

#Book Information
#Wed Apr 01 16:36:23 GMT+08:00 2020
书名:The Mocking Bird
书号:002
借阅情况:已借出
借书学生:Amy

#Book Information
#Wed Apr 01 16:36:23 GMT+08:00 2020
书名:Harry Potter
书号:003
借阅情况:已借出
借书学生:Amy

#Book Information
#Wed Apr 01 16:36:23 GMT+08:00 2020
书名:Gone with the wind
书号:001
借阅情况:未借出

#Book Information
#Wed Apr 01 16:36:23 GMT+08:00 2020
书名:The Mocking Bird
书号:002
借阅情况:未借出

#Book Information
#Wed Apr 01 16:36:23 GMT+08:00 2020
书名:Harry Potter
书号:003
借阅情况:已借出
借书学生:Amy

#Book Information
#Wed Apr 01 16:36:45 GMT+08:00 2020
书名:Gone with the wind
书号:001
借阅情况:已借出
借书学生:Teddy

#Book Information
#Wed Apr 01 16:36:45 GMT+08:00 2020
书名:The Mocking Bird
书号:002
借阅情况:已借出
借书学生:Teddy

#Book Information
#Wed Apr 01 16:36:45 GMT+08:00 2020
书名:Harry Potter
书号:003
借阅情况:已借出
借书学生:Teddy

#Book Information
#Wed Apr 01 16:36:45 GMT+08:00 2020
书名:Gone with the wind
书号:001
借阅情况:未借出

#Book Information
#Wed Apr 01 16:36:45 GMT+08:00 2020
书名:The Mocking Bird
书号:002
借阅情况:未借出

#Book Information
#Wed Apr 01 16:36:45 GMT+08:00 2020
书名:Harry Potter
书号:003
借阅情况:已借出
借书学生:Teddy

#Book Information
#Wed Apr 01 16:37:00 GMT+08:00 2020
书名:Gone with the wind
书号:001
借阅情况:已借出
借书学生:Bob

#Book Information
#Wed Apr 01 16:37:01 GMT+08:00 2020
书名:The Mocking Bird
书号:002
借阅情况:已借出
借书学生:Bob

#Book Information
#Wed Apr 01 16:37:01 GMT+08:00 2020
书名:Harry Potter
书号:003
借阅情况:已借出
借书学生:Bob

#Book Information
#Wed Apr 01 16:37:01 GMT+08:00 2020
书名:Gone with the wind
书号:001
借阅情况:未借出

#Book Information
#Wed Apr 01 16:37:01 GMT+08:00 2020
书名:The Mocking Bird
书号:002
借阅情况:未借出

#Book Information
#Wed Apr 01 16:37:01 GMT+08:00 2020
书名:Harry Potter
书号:003
借阅情况:已借出
借书学生:Bob
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值