目录
一、项目简介
这是一个基于文本界面,使用I/O流的图书管理系统,通过I/O流对数据进行一定的增删查改。项目的大体功能为:
二、实体对象
Book
/** 图书编号 */
private String ISBN;
/** 图书名称 */
private String bookName;
/** 作者 */
private String author;
/** 出版社 */
private String publish;
/** 出版日期 */
private String publishDate;
/** 印刷次数 **/
private int printTime;
/** 单价 */
private double unitPrice;
/** 图书类型 */
private String typeName;
BookType
/** 图书类型编号 */
private String typeId;
/** 类型名称 */
private String typeName;
BorrowBook
/** 借阅的读者ID */
private String readerId;
/** 借阅的书籍ID */
private String ISBM;
/** 借阅时间 */
private Date borrowDate;
/** 归还时间 */
private Date returnDate;
/** 是否归还 */
private boolean fine;
Fine
private int fineId;
private String fineName;
private double fine;
Reader
/** 读者ID */
private String readerId;
/** 姓名 */
private String readerName;
/** 性别 */
private String sex;
/** 年龄 */
private int age;
/** 所在部门 */
private String dept;
/** 电话 */
private String phone;
/** 注册时间 */
private String regDat;
/** 限度 */
private String limit;
/** 最大借阅数 */
private int maxBorrowNum;
/** 读者类型 */
private String type;
/** 类型名称 */
private String typeName;
ReaderType
/** 类型ID */
private Integer typeId;
/** 类型名称 */
private String typeName;
/** 天数限度 */
private Integer limit;
/** 最大借阅数 */
private int maxBorrowNum;
User
/** 用户ID */
private String userId;
/** 用户名称 */
private String userName;
/** 用户密码 */
private String password;
三、功能实现
1、用户管理
功能描述:实现对登录的用户信息管理,子功能为修改密码,新增用户,删除用户。
新增用户
/**
* insertUser()方法
* 新增用户
* */
public static int insertUser(String name, String password) throws IOException {
// 新增用户
File[] files = file.listFiles();
for (File f : files){
if (f.getName().equals(name + ".txt")) return 0;
}
id++;
BufferedWriter writer = new BufferedWriter(new FileWriter(file + "\\" + name + ".txt"));
writer.write("userId=" + String.valueOf(id));
writer.newLine();
writer.write("userName=" + name);
writer.newLine();
writer.write("password=" + password);
writer.close();
return 1;
}
修改密码
/**
* updateUserPWD()方法
* 修改用户密码
* */
public static int updateUserPwd(Integer id, String pwd) throws IOException {
int i = 0;
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("userId").equals(String.valueOf(id))){
i = Dao.executeUpdate(file + "\\" + properties.getProperty("userName") + ".txt","password",pwd);
}
properties.clear();
}
return i;
}
删除用户
/**
* deleteUser()方法
* 删除用户
* */
public static int deleteUser(Integer id) throws IOException {
String userName = "";
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("userId").equals(String.valueOf(id))){
userName = properties.getProperty("userName");
}
}
File delFile = new File(file + "\\" + userName + ".txt");
if(delFile.exists()){
delFile.delete();
return 1;
}
return 0;
}
2、基本信息管理
功能描述:主要对一些特殊字段的增删改查,比如逾期罚金,读者的类别等级这些,需要通过读者的类型来判断,读者能借多少天的书,最大借阅数量,以及逾期过后相应的罚金。
图书类别设置
public static List<BookType> selectBookType() throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
list.add(setAttribute(properties));
properties.clear();
}
return list;
}
public static List<BookType> selectBookType(String type) throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("typeName").equals(type)){
list.add(setAttribute(properties));
}
properties.clear();
}
return list;
}
public static int insertBookType(Integer id, String typename) throws IOException {
File[] files = file.listFiles();
for (File f : files){
properties = Dao.executeQuery(f.getAbsolutePath());
if (properties.getProperty("typeId").equals(String.valueOf(id)) ||
properties.getProperty("typeName").equals(typename)) return 0;
}
BufferedWriter writer = new BufferedWriter(new FileWriter(file + "\\" + id + ".txt"));
writer.write("typeId=" + id);
writer.newLine();
writer.write("typeName=" + typename);
writer.close();
return 1;
}
public static int updateBookType(Integer id, String typename) throws IOException {
File[] files = file.listFiles();
for(File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("typeName").equals(typename)){
return 0;
}
}
boolean flag = true;
for(File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("typeId").equals(String.valueOf(id))){
flag = false;
}
}
if(flag) return 0;
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("typeId").equals(String.valueOf(id))){
dao.executeUpdate(f.getAbsolutePath(),"typeName",typename);
break;
}
}
return 1;
}
public static int deleteBookType(Integer id) throws IOException {
File[] files = file.listFiles();
for(File f : files){
properties = Dao.executeQuery(f.getAbsolutePath());
if (properties.getProperty("typeId").equals(String.valueOf(id))){
f.delete();
return 1;
}
}
return 0;
}
/** 获取单个对象属性 */
private static BookType setAttribute(Properties properties){
BookType bt = new BookType();
bt.setTypeId(properties.getProperty("typeId"));
bt.setTypeName(properties.getProperty("typeName"));
return bt;
}
读者类别设置
public static List<ReaderType> selectReaderType() throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
list.add(setAttribute(properties));
properties.clear();
}
return list;
}
public static List<ReaderType> selectReaderType(String type) throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("typeName").equals(type)){
list.add(setAttribute(properties));
}
properties.clear();
}
return list;
}
public static int insertReaderType(Integer id, String typename, Integer num, Integer limit) throws IOException {
File[] files = file.listFiles();
for (File f : files){
properties = Dao.executeQuery(f.getAbsolutePath());
if (properties.getProperty("typeId").equals(String.valueOf(id)) ||
properties.getProperty("typeName").equals(typename)) return 0;
}
BufferedWriter writer = new BufferedWriter(new FileWriter(file + "\\" + id + ".txt"));
writer.write("typeId=" + id);
writer.newLine();
writer.write("typeName=" + typename);
writer.newLine();
writer.write("limit=" + limit);
writer.newLine();
writer.write("maxBorrowNum=" + num);
writer.close();
return 1;
}
public static int updateReaderType(Integer id, String typename, Integer num, Integer limit) throws IOException {
File[] files = file.listFiles();
boolean flag = true;
for(File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("typeId").equals(String.valueOf(id))){
flag = false;
}
}
if(flag) return 0;
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("typeId").equals(String.valueOf(id))){
dao.executeUpdate(f.getAbsolutePath(),"typeName",typename);
dao.executeUpdate(f.getAbsolutePath(),"limit",String.valueOf(limit));
dao.executeUpdate(f.getAbsolutePath(),"maxBorrowNum",String.valueOf(num));
break;
}
}
return 1;
}
public static int deleteReaderType(Integer id) throws IOException {
File[] files = file.listFiles();
for(File f : files){
properties = Dao.executeQuery(f.getAbsolutePath());
if (properties.getProperty("typeId").equals(String.valueOf(id))){
f.delete();
return 1;
}
}
return 0;
}
/** 获取单个对象属性 */
private static ReaderType setAttribute(Properties properties){
ReaderType rt = new ReaderType();
rt.setTypeId(Integer.parseInt(properties.getProperty("typeId")));
rt.setTypeName(properties.getProperty("typeName"));
rt.setLimit(Integer.parseInt(properties.getProperty("limit")));
rt.setMaxBorrowNum(Integer.parseInt(properties.getProperty("maxBorrowNum")));
return rt;
}
罚金设置
public static List<Fine> selectFineRules() throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
list.add(setAttribute(properties));
properties.clear();
}
return list;
}
public static int insertFineRule(Integer id, String fineName,double fine) throws IOException {
File[] files = file.listFiles();
for (File f : files){
properties = Dao.executeQuery(f.getAbsolutePath());
if (properties.getProperty("fineId").equals(String.valueOf(id)) ||
properties.getProperty("fineName").equals(fineName)) return 0;
}
BufferedWriter writer = new BufferedWriter(new FileWriter(file + "\\" + id + ".txt"));
writer.write("fineId=" + id);
writer.newLine();
writer.write("fineName=" + fineName);
writer.newLine();
writer.write("fine=" + fine);
writer.close();
return 1;
}
public static int updateFineRule(Integer id, String fineName,double fine) throws IOException {
File[] files = file.listFiles();
boolean flag = true;
for(File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("fineId").equals(String.valueOf(id))){
flag = false;
}
}
if(flag) return 0;
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("fineId").equals(String.valueOf(id))){
dao.executeUpdate(f.getAbsolutePath(),"fineName",fineName);
dao.executeUpdate(f.getAbsolutePath(),"fine",String.valueOf(fine));
break;
}
}
return 1;
}
public static int deleteFineRule(Integer id) throws IOException {
File[] files = file.listFiles();
for(File f : files){
properties = Dao.executeQuery(f.getAbsolutePath());
if (properties.getProperty("fineId").equals(String.valueOf(id))){
f.delete();
return 1;
}
}
return 0;
}
/** 获取单个对象属性 */
private static Fine setAttribute(Properties properties) {
Fine fine = new Fine();
fine.setFineId(Integer.parseInt(properties.getProperty("fineId")));
fine.setFineName(properties.getProperty("fineName"));
fine.setFine(Double.parseDouble(properties.getProperty("fine")));
return fine;
}
3、读者信息管理
功能描述:
需要对读者的信息进行增删改查。
读者信息查询
通过编号查询
/** 通过ID查找 */
public static List<Reader> selectReaderById(String id) throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("readerId").equals(id)){
list.add(setAttribute(properties));
}
}
return list;
}
通过名字查询
/** 通过名字查找 */
public static List<Reader> selectReaderByName(String name) throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("readerName").equals(name)){
list.add(setAttribute(properties));
}
properties.clear();
}
return list;
}
通过类型查询
/** 通过类型查找 */
public static List<Reader> selectReaderByType(String type) throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("typeName").equals(type)){
list.add(setAttribute(properties));
}
properties.clear();
}
return list;
}
通过部门查询
/** 通过部门查找 */
public static List<Reader> selectReaderByDept(String dept) throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("dept").equals(dept)){
list.add(setAttribute(properties));
}
properties.clear();
}
return list;
}
查询全部信息
/** 查询所有读者 */
public static List<Reader> selectReader() throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
list.add(setAttribute(properties));
properties.clear();
}
return list;
}
读者信息添加
/** 新增读者 */
public static int insertReader(String id, String typename,String name, int age,String sex, String phone, String dept, String reg) throws IOException {
File[] files = file.listFiles();
for (File f : files){
if (f.getName().equals(id + ".txt")) return 0;
}
BufferedWriter writer = new BufferedWriter(new FileWriter(file + "\\" + id + ".txt"));
writer.write("readerId=" + id);
writer.newLine();
writer.write("readerName=" + name);
writer.newLine();
writer.write("sex=" + sex);
writer.newLine();
writer.write("age=" + age);
writer.newLine();
writer.write("dept=" + dept);
writer.newLine();
writer.write("phone=" + phone);
writer.newLine();
writer.write("regDat=" + reg);
writer.newLine();
writer.write("typeName=" + typename);
writer.newLine();
writer.close();
return 1;
}
读者信息修改
/** 更新信息 */
public static int updateReader(String id, String typename, String name, Integer age,String sex, String phone, String dept, String reg) throws IOException {
dao.executeUpdate(file + "\\" + id + ".txt","typeName",typename);
dao.executeUpdate(file + "\\" + id + ".txt","readerName",name);
dao.executeUpdate(file + "\\" + id + ".txt","age",String.valueOf(age));
dao.executeUpdate(file + "\\" + id + ".txt","sex",sex);
dao.executeUpdate(file + "\\" + id + ".txt","dept",dept);
dao.executeUpdate(file + "\\" + id + ".txt","phone",phone);
dao.executeUpdate(file + "\\" + id + ".txt","regDat",reg);
return 1;
}
4、图书信息管理
功能描述:
需要对图书信息进行增删改查。
/** 新增图书信息 */
public static int insertBook(String ISBN, String typeName, String bookName, String author, String publish, String publishDate, Integer printTime, Double price) throws IOException {
File[] files = file.listFiles();
for (File f : files){
if (f.getName().equals(ISBN + ".txt")) return 0;
}
BufferedWriter writer = new BufferedWriter(new FileWriter(file + "\\" + ISBN + ".txt"));
writer.write("ISBN=" + ISBN);
writer.newLine();
writer.write("bookName=" + bookName);
writer.newLine();
writer.write("author=" + author);
writer.newLine();
writer.write("publish=" + publish);
writer.newLine();
writer.write("publishDate=" + publishDate);
writer.newLine();
writer.write("printTime=" + printTime);
writer.newLine();
writer.write("unitPrice=" + price);
writer.newLine();
writer.write("typeName=" + typeName);
writer.newLine();
writer.close();
return 1;
}
/** 查询所有图书 */
public static List<Book> selectBook() throws IOException {
list.clear();
File[] files = file.listFiles();
list.clear();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
list.add(setAttribute(properties));
properties.clear();
}
return list;
}
/** 根据图书编号查询 */
public static List<Book> selectBookByISBN(String ISBN) throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("ISBN").equals(ISBN)){
list.add(setAttribute(properties));
}
properties.clear();
}
return list;
}
/** 根据图书名称查询 */
public static List<Book> selectBookByName(String name) throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("bookName").equals(name)){
list.add(setAttribute(properties));
}
properties.clear();
}
return list;
}
/** 根据图书类型查询 */
public static List<Book> selectBookByType(String type) throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("typeName").equals(type)){
list.add(setAttribute(properties));
}
properties.clear();
}
return list;
}
/** 根据作者查询 */
public static List<Book> selectBookByAuthor(String author) throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("author").equals(author)){
list.add(setAttribute(properties));
}
properties.clear();
}
return list;
}
/** 根据出版社查询 */
public static List<Book> selectBookByPublish(String publish) throws IOException {
list.clear();
File[] files = file.listFiles();
for (File f : files){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("publish").equals(publish)){
list.add(setAttribute(properties));
}
properties.clear();
}
return list;
}
/** 修改数据 */
public static int update(String ISBN, String typeName, String bookName, String author, String publish, String publishDate, Integer printTime,Double unitPrice) throws IOException {
//dao.executeUpdate(file + "\\" + ISBN + ".txt","ISBN",ISBN);
dao.executeUpdate(file + "\\" + ISBN + ".txt","typeName",typeName);
dao.executeUpdate(file + "\\" + ISBN + ".txt","bookName",bookName);
dao.executeUpdate(file + "\\" + ISBN + ".txt","author",author);
dao.executeUpdate(file + "\\" + ISBN + ".txt","publish",publish);
dao.executeUpdate(file + "\\" + ISBN + ".txt","publishDate",publishDate);
dao.executeUpdate(file + "\\" + ISBN + ".txt","printTime",String.valueOf(printTime));
dao.executeUpdate(file + "\\" + ISBN + ".txt","unitPrice",String.valueOf(unitPrice));
return 1;
}
/** 获取单个对象属性 */
private static Book setAttribute(Properties properties){
Book book = new Book();
book.setISBN(properties.getProperty("ISBN"));
book.setTypeName(properties.getProperty("typeName"));
book.setBookName(properties.getProperty("bookName"));
book.setAuthor(properties.getProperty("author"));
book.setPublish(properties.getProperty("publish"));
book.setPublishDate(properties.getProperty("publishDate"));
book.setPrintTime(Integer.parseInt(properties.getProperty("printTime")));
book.setUnitPrice(Double.parseDouble(properties.getProperty("unitPrice")));
return book;
}
/** 删除原信息 */
public static int deleteFile(String ISBN){
File f = new File(file + "\\" + ISBN + ".txt");
if (f.exists()) f.delete();
else return 0;
return 1;
}
/** 判断存在 */
public static int exeits(String ISBN){
File f = new File(file + "\\" + ISBN + ".txt");
if (f.exists()) return 1;
return 0;
}
5、图书借阅管理
功能描述:
包含三个功能:图书借阅,图书归还,查看借阅情况。
图书借阅
public static int borrowBook(String readerId,String ISBN, Date borrowDate) throws IOException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String borrowTime = sdf.format(borrowDate);
File[] files = file.listFiles();
for (File f : files){
if (f.getName().equals(readerId + "-" + ISBN + ".txt")){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("isReturn").equals(false)){
return 0;
}else{
dao.executeUpdate(f.getAbsolutePath(),"isReturn","false");
dao.executeUpdate(f.getAbsolutePath(),"borrowDate",borrowTime);
dao.executeUpdate(f.getAbsolutePath(),"returnDate","");
return 1;
}
}
}
BufferedWriter writer = new BufferedWriter(new FileWriter(file + "\\" + readerId + "-" + ISBN + ".txt"));
writer.write("readerName=" + getReaderInfo(readerId).getReaderName());
writer.newLine();
writer.write("bookName=" + getBookInfo(ISBN).getBookName());
writer.newLine();
writer.write("borrowDate=" + borrowTime);
writer.newLine();
writer.write("returnDate=");
writer.newLine();
writer.write("isReturn=false");
writer.close();
return 1;
}
图书归还
public static int returnBook(String readerId,String ISBN,Date returnDate) throws IOException, ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String returnTime = sdf.format(returnDate);
File[] files = file.listFiles();
for (File f : files){
if (f.getName().equals(readerId + "-" + ISBN + ".txt")){
properties = dao.executeQuery(f.getAbsolutePath());
if(properties.getProperty("isReturn").equals("true")) return 0;
if(returnDate.getTime() - sdf.parse(properties.getProperty("borrowDate")).getTime() < 0) return 0;
dao.executeUpdate(f.getAbsolutePath(),"returnDate",returnTime);
dao.executeUpdate(f.getAbsolutePath(),"isReturn","true");
return 1;
};
}
return 0;
}
查看情况
public static void borrowQuery() throws IOException {
System.out.println("读者\t图书\t借阅时间\t归还时间\t是否归还");
for (File f : borrowFile.listFiles()){
Properties p = Dao.executeQuery(f.getAbsolutePath());
System.out.println(p.getProperty("readerName") + " " +
p.getProperty("bookName") + " " +
p.getProperty("borrowDate") + " " +
p.getProperty("returnDate") + " " +
p.getProperty("isReturn"));
}
}
上面的代码是较核心的代码,并没有加入验证等,详情自己摸索,杜绝白拿。