3月15日 dom4j、JavaBean综合练习 图书管理系统
图书管理系统(由于只是为了练习dom4j,一些异常没有处理,还有些bug)
实现功能:
1.增:用户输入A,添加数据
2.删:用户输入D,删除数据
3.改:用户输入C,修改数据
4.查:用户输入F,根据id号查询数据
5.退出:输入exit
原理: JavaBean 技术写一个Book类
将数据存入Books.xml文件中
用户输入相应指令实质上就是通过dom4j技术修改xml中的数据
文件:
books.xml XML文件
Book.java 使用JavaBean技术描述
Add.java 实现添加功能
Change.java 修改数据
Delete.java 删除数据
Find.java 查询数据
XMLUtil.java 工具类
Main.java 主文件
代码:
1.books.xml
<?xml version="1.0" encoding="UTF-8"?> <books> <book id="b001"> <title>core java</title> <price>34</price> <author>qw</author> </book> <book id="b002"> <title>java world</title> <price>91</price> <author>er</author> </book> <book id="b003"> <title>my way</title> <price>66</price> <author>qw</author> </book> <book id="b004"> <title>javaee</title> <price>45</price> <author>ty</author> </book> <book id="b005"> <title>javascript</title> <price>87</price> <author>yu</author> </book> <book id="b006"> <title>news javese</title> <price>36</price> <author>ui</author> </book> <book id="b007"> <title>xml</title> <price>192</price> <author>io</author> </book> <book id="b008"> <title>sevlet</title> <price>58</price> <author>jh</author> </book> <book id="b009"> <title>dom4j</title> <price>30</price> <author>gb</author> </book> <book id="b010"> <title>thinking of java</title> <price>60</price> <author>fv</author> </book>
2.Book.java
package my.system.bean; public class Book { private String id; private String title; private String price; private String author; public Book() { super(); } public Book(String id, String title, String price, String author) { super(); this.id = id; this.title = title; this.price = price; this.author = author; } @Override public String toString() { return "Book [id=" + id + ", title=" + title + ", price=" + price + ", author=" + author + "]"; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
3.Add.java
//传入参数,对数据进行添加 package my.system.tool; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; public class Add { public static void add(Document document,String id, String title,String price,String author) { //添加元素 Element newEle = DocumentHelper.createElement("book"); Element root = document.getRootElement(); root.add(newEle); //添加id属性 Attribute attr = DocumentHelper.createAttribute(newEle, "id", id); newEle.add(attr); //添加子元素name,并赋值 Element newChild_name = DocumentHelper.createElement("title"); newEle.add(newChild_name); newChild_name.setText(title); //添加子元素price,并赋值 Element newChild_price = DocumentHelper.createElement("price"); newEle.add(newChild_price); newChild_price.setText(price); //添加子元素price,并赋值 Element newChild_author = DocumentHelper.createElement("author"); newEle.add(newChild_author); newChild_author.setText(author); } }
4.Change.java
package my.system.tool; import org.dom4j.Document; import org.dom4j.Element; public class Change { public static void change(Document document,String id,String key,String value) { Element bookEle = (Element) document.selectSingleNode("//book[@id=‘"+id+"‘]"); Element keysEle = bookEle.element(key); keysEle.setText(value); } }
5.Delete.java
package my.system.tool; import org.dom4j.Document; import org.dom4j.Element; public class Delete { //删除整本书 public static void deleteAll(Document document,String id) { Element bookEle = (Element) document.selectSingleNode("//book[@id=‘"+id+"‘]"); Element bookEleParent = bookEle.getParent(); bookEleParent.remove(bookEle); } }
6.Find.java
package my.system.tool; import org.dom4j.Document; import org.dom4j.Element; public class Find { public static Element findById(Document document,String id) { Element bookEle = (Element) document.selectSingleNode("//book[@id=‘"+id+"‘]"); return bookEle; } }
7.XMLUtil.java
package my.system.util; import java.io.FileOutputStream; import org.dom4j.Document; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class XMLUtil { public static Document create() throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read("book.xml"); return document; } public static void save(Document document) throws Exception { XMLWriter writer = new XMLWriter(new FileOutputStream("book.xml")); writer.write(document); writer.close(); } }
8.Main.java
package my.system.main; import java.io.BufferedReader; import java.io.InputStreamReader; import org.dom4j.Document; import my.system.tool.Add; import my.system.tool.Change; import my.system.tool.Delete; import my.system.tool.Find; import my.system.util.XMLUtil; public class Main { public static void main(String[] args) throws Exception { //欢迎 System.out.println("欢迎进入图书管理系统!"); //选择操作: System.out.println("请输入您要进行的操作指令:"); while(true) { System.out.println("添加数据:A \r\n删除数据:D \r\n修改数据:C \r\n查询数据:F \r\n退出:exit(不区分大小写)"); //创建流 BufferedReader bfw = new BufferedReader (new InputStreamReader(System.in)); String line = bfw.readLine(); if("A".equalsIgnoreCase(line)) { Document document = XMLUtil.create(); System.out.print("请输入书名:"); BufferedReader bfw_t = new BufferedReader (new InputStreamReader(System.in)); String title = bfw_t.readLine(); System.out.print("设置价格:"); BufferedReader bfw_p = new BufferedReader (new InputStreamReader(System.in)); String price = bfw_p.readLine(); System.out.print("作者:"); BufferedReader bfw_a = new BufferedReader (new InputStreamReader(System.in)); String author = bfw_a.readLine(); System.out.print("请配置id:(注意,配置完毕以后将无法修改!!)"); BufferedReader bfw_i = new BufferedReader (new InputStreamReader(System.in)); String id = bfw_i.readLine(); Add.add(document, id, title, price, author); XMLUtil.save(document); continue; } else if("D".equalsIgnoreCase(line)) { Document document = XMLUtil.create(); System.out.println("请输入id号"); BufferedReader bfw_i = new BufferedReader (new InputStreamReader(System.in)); String id = bfw_i.readLine(); Delete.deleteAll(document,id); XMLUtil.save(document); continue; } else if("C".equalsIgnoreCase(line)) { Document document = XMLUtil.create(); System.out.print("请输入要修改的书籍的id:"); BufferedReader bfw_i = new BufferedReader (new InputStreamReader(System.in)); String id = bfw_i.readLine(); System.out.println("输入要修改的项目:(书名(A)价格(B) 作者(C))"); BufferedReader bfw_k = new BufferedReader (new InputStreamReader(System.in)); String key = bfw_k.readLine(); if(!("A".equalsIgnoreCase(key) ||"B".equalsIgnoreCase(key) ||"C".equalsIgnoreCase(key))) { System.out.println("数据错误!!程序自动停止!"); break; } else { System.out.println("输入相应的值:"); BufferedReader bfw_ = new BufferedReader (new InputStreamReader(System.in)); String value = bfw_.readLine(); Change.change(document,id,key,value); } XMLUtil.save(document); continue; } else if("F".equalsIgnoreCase(line)) { Document document = XMLUtil.create(); System.out.println("请输入id号"); BufferedReader bfw_i = new BufferedReader (new InputStreamReader(System.in)); String id = bfw_i.readLine(); Find.findById(document,id); XMLUtil.save(document); continue; } else if("exit".equalsIgnoreCase(line)) break; else { System.out.println("指令错误,自动停止!"); } } } }
package my.system.bean; public class Book { private String id; private String title; private String price; private String author; public Book() { super(); } public Book(String id, String title, String price, String author) { super(); this.id = id; this.title = title; this.price = price; this.author = author; } @Override public String toString() { return "Book [id=" + id + ", title=" + title + ", price=" + price + ", author=" + author + "]"; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。