题目
第一版详见【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