实体类:
package vo; public class Contact { private String id; private String name; private String gender; private int age; private String phone; private String email; private String qq; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } public String toString() { return "Contact [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + ", phone=" + phone + ", email=" + email + ", qq=" + qq + "]"; } }
接口类:
package service; import java.util.List; import vo.Contact; public interface ContactOperator { public void addContact(Contact contact); public void updateContact(Contact contact); public void deleteContact(String id); public List<Contact> findAll(); }
接口的实现类:
package serviceImp; import java.io.File; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import service.ContactOperator; import util.XMLUtil; import vo.Contact; public class IContactOperator implements ContactOperator { @Override public void addContact(Contact contact) { try { File file = new File("d:\\contact.xml"); Document doc = null; Element rootElem = null; if(!file.exists()) { //如果没有xml文件,则创建xml文件 doc = DocumentHelper.createDocument(); //创建根标签 rootElem = doc.addElement("contactList"); } else { //如果有xml文件,则读取xml文件 doc = XMLUtil.getDocument(); //如果有xml文件,读取根标签 rootElem = doc.getRootElement(); } //添加contact标签 /* * <contact id="1"> <name>eric</name> <gender>男</gender> <age>20</age> <phone>1343333</phone> <email>eric@qq.com</email> <qq>554444</qq> </contact> */ Element contactElem = rootElem.addElement("contact"); contactElem.addAttribute("id", contact.getId()); contactElem.addElement("name").setText(contact.getName()); contactElem.addElement("gender").setText(contact.getGender()); contactElem.addElement("age").setText(contact.getAge()+""); contactElem.addElement("phone").setText(contact.getPhone()); contactElem.addElement("email").setText(contact.getEmail()); contactElem.addElement("qq").setText(contact.getQq()); //把Document写出到xml文件 XMLUtil.write2xml(doc); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } @Override public void updateContact(Contact contact) { /** * 需求: 修改id值为2的联系人 * 1)查询id值为2的contact标签 * 2)修改contact标签的内容 */ try { //1.读取xml文件 Document doc = XMLUtil.getDocument(); Element contactElem = (Element) doc.selectSingleNode("//contact[@id='" + contact.getId() + "']"); //2.修改contact标签内容 contactElem.element("name").setText(contact.getName()); contactElem.element("gender").setText(contact.getGender()); contactElem.element("age").setText(contact.getAge()+""); contactElem.element("phone").setText(contact.getPhone()); contactElem.element("email").setText(contact.getEmail()); contactElem.element("qq").setText(contact.getQq()); //3.把Document写出到xml文件 XMLUtil.write2xml(doc); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } /** * 根据id删除节点 */ @Override public void deleteContact(String id) { try { // 1.读取xml文件 Document doc = XMLUtil.getDocument(); Element contactElem = (Element) doc.selectSingleNode("//contact[@id='" + id + "']"); //删除标签 contactElem.detach(); //3.把Document写出到xml文件 XMLUtil.write2xml(doc); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } @Override public List<Contact> findAll() { // 1. 读取xml文件 Document doc = XMLUtil.getDocument(); //2.创建List对象 List<Contact> list = new ArrayList<Contact>(); //3.读取contact标签 List<Element> conList = (List<Element>)doc.selectNodes("//contact"); for(Element e : conList) { //创建Contact对象 Contact c = new Contact(); c.setId(e.attributeValue("id")); c.setName(e.elementText("name")); c.setGender(e.elementText("gender")); c.setAge(Integer.parseInt(e.elementText("age"))); c.setPhone(e.elementText("phone")); c.setEmail(e.elementText("email")); c.setQq(e.elementText("qq")); //把Contact放入list中 list.add(c); } return list; } }
xml操作的工具类:
package util; import java.io.File; import java.io.FileOutputStream; import org.dom4j.Document; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; /** * xml操作的工具类 * @author Administrator * */ public class XMLUtil { /* * 用于读取xml文件 */ public static Document getDocument() { try { Document doc = new SAXReader().read(new File("d:/contact.xml")); return doc; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } } /* * 写入xml文件 */ public static void write2xml(Document doc) { try { FileOutputStream out = new FileOutputStream("d:/contact.xml"); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter writer = new XMLWriter(out, format); writer.write(doc); writer.close(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } }
测试类:
package test; import java.util.List; import org.junit.Before; import org.junit.Test; import service.ContactOperator; import serviceImp.IContactOperator; import vo.Contact; public class ContactTest { ContactOperator operator = null; @Before public void init() { operator = new IContactOperator(); } @Test public void testAddContact(){ Contact contact = new Contact(); contact.setId("1"); contact.setName("rose"); contact.setGender("男"); contact.setAge(20); contact.setPhone("134222233333"); contact.setEmail("eric@qq.com"); contact.setQq("33334444"); operator.addContact(contact); } @Test public void testUploadContact() { Contact contact = new Contact(); contact.setId("1"); //修改的ID contact.setName("zhangsan"); contact.setGender("女"); contact.setAge(30); contact.setPhone("135222233333"); contact.setEmail("zhangsan@qq.com"); contact.setQq("33334444"); operator.updateContact(contact); } @Test public void testDelete() { Contact contact = new Contact(); contact.setId("1"); //修改的ID operator.updateContact(contact); } @Test public void testFindAll() { List<Contact> list = operator.findAll(); for (Contact c : list) { System.out.println(c); } } }
main方法:
package main; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import service.ContactOperator; import serviceImp.IContactOperator; import vo.Contact; public class MainProgram { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); ContactOperator operator = new IContactOperator(); // 不断接收输入 while(true) { //1.看到菜单 printMenu(); //2.接收用户输入的命令 String command = br.readLine(); //3.根据用户的输入执行不同的操作 if("1".equals(command)){ //添加联系人 //获取用输入的数据 Contact contact = new Contact(); //获取ID System.out.println("请输入编号:"); String id = br.readLine(); //获取姓名 System.out.println("请输入姓名:"); String name = br.readLine(); //获取性别 System.out.println("请输入性别:"); String gender = br.readLine(); //获取年龄 System.out.println("请输入年龄:"); String age = br.readLine(); //获取电话 System.out.println("请输入电话:"); String phone = br.readLine(); //获取邮箱 System.out.println("请输入邮箱:"); String email = br.readLine(); //获取QQ System.out.println("请输入QQ:"); String qq = br.readLine(); contact.setId(id); contact.setName(name); contact.setGender(gender); contact.setAge(Integer.parseInt(age)); contact.setPhone(phone); contact.setEmail(email); contact.setQq(qq); operator.addContact(contact); } else if ("2".equals(command)) { //修改联系人 //获取用输入的数据 Contact contact = new Contact(); //获取ID System.out.println("请输入需要修改的编号:"); String id = br.readLine(); //获取姓名 System.out.println("请输入修改后的姓名:"); String name = br.readLine(); //获取性别 System.out.println("请输入修改后的性别:"); String gender = br.readLine(); //获取年龄 System.out.println("请输入修改后的年龄:"); String age = br.readLine(); //获取电话 System.out.println("请输入修改后的电话:"); String phone = br.readLine(); //获取邮箱 System.out.println("请输入修改后的邮箱:"); String email = br.readLine(); //获取QQ System.out.println("请输入修改后的QQ:"); String qq = br.readLine(); contact.setId(id); contact.setName(name); contact.setGender(gender); contact.setAge(Integer.parseInt(age)); contact.setPhone(phone); contact.setEmail(email); contact.setQq(qq); operator.updateContact(contact); } else if ("3".equals(command)){ //删除联系人 //获取需要删除的ID System.out.println("请输入删除的编号:"); String id = br.readLine(); operator.deleteContact(id); } else if ("4".equals(command)){ //查询所有联系人 List<Contact> list = operator.findAll(); //打印输出 for (Contact con : list) { System.out.println(con.toString()); } } else if ("Q".equals(command)){ break; } else { System.out.println("输入的命令有误!"); } } } catch (Exception e) { } } public static void printMenu() { System.out.println("===================="); System.out.println("[1]添加联系人"); System.out.println("[2]修改联系人"); System.out.println("[3]删除联系人"); System.out.println("[4]查看所有联系人"); System.out.println("[Q]退出系统"); System.out.println("===================="); } }