package com.book; public class Book {
private int id; //图书编号
private String name; //图书名称
private String author; //图书作者
private int states; //书本借出状态 0可借阅 1已借出
private float price; //图书价格
private String date; //借出时间
private int count; //借出次数 public Book() {
} public Book(int id,String name, String author, int states, float price, String date, int count) {
this.id=id;
this.name = name;
this.author = author;
this.states = states;
this.price = price;
this.date = date;
this.count = count;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getStates() {
return states;
}
public void setStates(int states) {
this.states = states;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
} @Override
public String toString() {
return this.id + "\t\t" +(this.states==0? "可借阅":"已借出")+ "\t" + this.name + "\t"
+ this.date+ "\t" + this.count;
} }
package com.book; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List; import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter; import com.book.Book; public class XmlUtil { /**
* 解析xml, 并把xml中的图书转换成集合输出
* @return 图书集合
*/
public static List<Book> queryBooks(){
List<Book> booksList=new ArrayList<Book>();
SAXReader saxReader=new SAXReader();
try {
Document doc=saxReader.read(new File("books\\books.xml"));
//获取根节点
Element root=doc.getRootElement();
//返回集合
List<Element> eleBooks=root.elements("book");
//遍历集合,从集合里找到每一本书
for (Element eleBook : eleBooks) {
Book b=new Book();
b.setId(Integer.parseInt(eleBook.attributeValue("id"))); //赋值图书编号,是根据xml的book元素的id获得值
b.setName(eleBook.element("name").getText()); //赋值书名,根据book元素下的name元素获得文本内容
b.setAuthor(eleBook.element("author").getText()); //赋值作者,根据book元素下的author元素获得文本内容
b.setPrice(Float.parseFloat(eleBook.element("price").getText())); //赋值租金,根据book元素下的price元素获得文本内容
b.setStates(Integer.parseInt(eleBook.element("states").getText())); //赋值状态,根据book元素下的states元素获得文本内容
b.setCount(Integer.parseInt(eleBook.element("count").getText())); //赋值借出次数,根据book元素下的count元素获得文本内容
b.setDate(eleBook.element("date").getText()); //赋值借出时间,根据book元素下的date元素获得文本内容 booksList.add(b); //把赋值好的图书添加到ArrayList集合
} } catch (DocumentException e) {
e.printStackTrace();
}
return booksList;
} /**
* 把集合里的图书写入xml里
* @param books
* @return
*/
public static boolean saveBook(List<Book> books) {
SAXReader reader=new SAXReader();
Document doc =reader.getDocumentFactory().createDocument(); //创建一个新的xml文档
Element root=doc.addElement("books"); //新增根元素<books> for (Book book : books) {
Element eleBook=root.addElement("book");
eleBook.addAttribute("id", book.getId()+""); //新增属性id,添加节点文本内容
eleBook.addElement("name").setText(book.getName()); //新增name节点,添加节点文本内容
eleBook.addElement("author").setText(book.getAuthor()); //新增author节点,添加节点文本内容
eleBook.addElement("price").setText(book.getPrice()+""); //新增price节点,添加节点文本内容,float类型转String类型
eleBook.addElement("states").setText(book.getStates()+""); //新增states节点,添加节点文本内容
eleBook.addElement("count").setText(book.getCount()+""); //新增count节点,添加节点文本内容
eleBook.addElement("date").setText(book.getDate()==null?"":book.getDate()); //新增date节点,添加节点文本内容,三目运算符判断归还日期是否为空
} try {
//创建XML输出流对象,并设置xml对齐样式
XMLWriter writer=new XMLWriter(new FileOutputStream("books\\books.xml"),OutputFormat.createPrettyPrint());
//把内容写入xml文档
writer.write(doc);
//关闭输出流
writer.close();
} catch (UnsupportedEncodingException | FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
} /**
* 测试集合内容是否写入xml中
* @param args
*/
public static void main(String[] args) {
/* //创建ArrayList<Book>集合对象
List<Book> books=new ArrayList<Book>();
//往集合里添加新的图书
books.add(new Book(001,"失落邮寄","陈先生",0,15,"",8));
books.add(new Book(002,"人性的弱点","扎先生",1,20,"2018-12-22",18));
//调用方法,把集合里的图书写入xml里
boolean b=saveBook(books);
System.out.println(b);*/ //创建ArrayList<Book>集合对象,调用集合查询方法
List<Book> booksList=queryBooks();
//遍历每一本书,重写tostring方法输出book
for (Book book : booksList) {
System.out.println(book);
}
}
}
package com.book; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import com.book.Book;
import com.util.XmlUtil; public class BookManagement { /**
* 图书新增
* @param b
*/
public void addBook(Book b) {
//创建ArrayList<Book>集合对象,调用集合查询方法
List<Book> booksList=XmlUtil.queryBooks();
for (Book book : booksList) {
if(book.getName().equals(b.getName())) {
System.out.println("图书已存在,新增失败!");
return;
}
}
//判断图书编号,如果没有图书编号设置1,否则在最后一本图书的编号上加1
if(booksList.size()==0) {
b.setId(1);
}else {
b.setId(booksList.get(booksList.size()-1).getId()+1);
}
booksList.add(b); //在新增的图书添加到集合里 XmlUtil.saveBook(booksList); //调用保存方法把集合写入xml里
System.out.println("新增图书成功!");
} /**
* 图书查看,重写toString()方法
*/
public void queryBooks() {
List<Book> booksList=XmlUtil.queryBooks();
System.out.println("序号\t\t状态\t\t名称\t\t借出日期\t\t借出次数");
for (Book book : booksList) {
System.out.println(book);
}
} /**
* 图书删除
* @param name
*/
public void deleteBook(String name) {
List<Book> booksList=XmlUtil.queryBooks();
//遍历集合里的图书
for(int i=0;i<booksList.size();i++) {
//判断输入的图书名字是否存在集合里,且是否是可借阅状态
if(booksList.get(i).getName().equals(name) && booksList.get(i).getStates() == 0) {
booksList.remove(i); //删除图书
System.out.println("删除成功!");
XmlUtil.saveBook(booksList); //保存
return;
}else if(booksList.get(i).getName().equals(name) && booksList.get(i).getStates() == 1){
System.out.println("图书未归还不能删除!");
return;
}
}
System.out.println("你要删除的图书不存在!");
} /**
* 图书借出
* @param name
*/
public void loanBook(String name) {
queryBooks(); //查询所有图书
List<Book> booksList=XmlUtil.queryBooks();
//当前的日期
Date date=new Date();
//将日期和时间的格式变为我们要求的格式:年-月-日
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String sdate=sdf.format(date);
//检测图书是否存在
for (Book book : booksList) {
if(book.getName().equals(name) && book.getStates()==0) {
book.setStates(1);
book.setCount(book.getCount()+1);
book.setDate(sdate);
System.out.println("图书借阅成功!");
XmlUtil.saveBook(booksList); //保存
return;
}else if(book.getName().equals(name) && book.getStates()==1) {
System.out.println("你要借阅的图书已借出!");
return;
}
}
System.out.println("你要借阅的图书不存在!");
} /**
* 图书归还
* @param name
*/
public void giveBook(String name) {
List<Book> booksList=XmlUtil.queryBooks(); for (Book book : booksList) {
if(book.getName().equals(name) && book.getStates()==1) {
book.setStates(0);
try {
//当前的日期
Date date=new Date();
//将日期和时间的格式变为我们要求的格式:年-月-日
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String sdate=sdf.format(date);
Date passDate = sdf.parse(book.getDate()); //String类型转Date类型
long time = (date.getTime() - passDate.getTime())/1000/3600/24; //图书租赁的时间
System.out.println("\""+book.getName()+"\""+"归还成功!\n借出日期是:"+book.getDate()+"\n归还日期为:"+sdate+"\n应付租金:"+time+"元");
book.setDate(""); //清空图书借出时间
XmlUtil.saveBook(booksList); //保存
return;
} catch (ParseException e) {
e.printStackTrace();
}
}else if(book.getName().equals(name) && book.getStates()==0){
System.out.println("图书未借出,无需归还!");
return;
}
}
System.out.println("不存在你要归还的图书!");
}
}
package com.book; import java.util.Scanner; import com.book.Book;
import com.book.service.BookManagement; public class BookMain {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
BookManagement bm=new BookManagement();
int zero =-1;
do {
System.out.println("欢迎使用迷你图书管理器\n------------------------------------------");
System.out.println("1、新增图书\n2、查看图书\n3、删除图书\n4、借出图书\n5、归还图书\n6、退出系统\n------------------------------------------");
System.out.print("请选择:");
int choose=input.nextInt();
switch(choose) {
case 1:
//新增图书
Book b=new Book();
System.out.print("请输入图书的名称:");
b.setName(input.next());
System.out.print("请输入图书的作者:");
b.setAuthor(input.next());
System.out.print("请输入图书价格:");
b.setPrice(input.nextFloat());
bm.addBook(b);
break;
case 2:
//查看图书
bm.queryBooks();
break;
case 3:
//删除图书
System.out.print("请输入要删除的图书名称:");
String name = input.next();
bm.deleteBook(name);
break;
case 4:
//借出图书
System.out.print("请输入要借阅图书的名称:");
name=input.next();
bm.loanBook(name);
break;
case 5:
//归还图书
System.out.print("请输入要归还的图书名称:");
name=input.next();
bm.giveBook(name);
break;
case 6:
//退出系统
System.out.println("欢迎再次使用!");
return;
}
System.out.print("输入0返回:");
zero = input.nextInt();
}while(zero==0);
System.out.println("谢谢使用!");
}
}
需要注意的是创建项目后首先要导入这个jar包