设计一个通讯录程序
联系人: 编号 姓名 性别 年龄 电话 QQ 邮箱
功能要求:
添加联系人
修改联系人
删除联系人
查询所有联系人
要求:
1)交互使用console(控制台)
2)数据存储到xml文件(作为“数据库”)(使用dom4j)
Main:
package com.contactSystem;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* Author:Liu Zhiyong(QQ:1012421396)
* Version:Version_1
* Date:2016年11月11日17:30:00
* Desc:通讯录程序
================
【1】添加联系人
【2】修改联系人
【3】删除联系人
【4】查询所有联系人
【Q】退出程序
================
*/
public class ContactMain {
private static BufferedReader buffReader = null;
public static void main(String[] args) throws Exception {
init();
}
private static void init() throws IOException, Exception {
while(true){
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("==================");
System.out.println("请选择功能:(1、2、3、4、Q)");
//获取输入流
buffReader = new BufferedReader(new InputStreamReader(System.in));
//读取键盘输入选择的功能
String option = buffReader.readLine();
char opt = option.trim().charAt(0);//字符串两边去空格后,取第一个字符
switch(opt){
case '1':
addContact();
break;
case '2':
editContact();
break;
case '3':
deleteContact();
break;
case '4':
getAllContacts();
break;
case 'Q':
case 'q':
existSystem();
break;
}
}
}
/**
* 【1】添加联系人
* @throws Exception
*/
private static void addContact() throws Exception {
System.out.print("请输入编号:");
String id = buffReader.readLine();
System.out.print("请输入姓名:");
String name = buffReader.readLine();
System.out.print("请输入性别:");
String sex = buffReader.readLine();
System.out.print("请输入年龄:");
String age = buffReader.readLine();
System.out.print("请输入电话:");
String phone = buffReader.readLine();
System.out.print("请输入QQ:");
String qq = buffReader.readLine();
System.out.print("请输入邮箱:");
String email = buffReader.readLine();
Contact contact = new Contact();
contact.setId(id);
contact.setName(name);
contact.setSex(sex);
contact.setAge(age);
contact.setPhone(phone);
contact.setQq(qq);
contact.setEmail(email);
//加载xml文件,获取Document对象
Document document = new SAXReader().read(new File("./src/com/contactSystem/contacts.xml"));
//获取到ContactList根标签
Element root = document.getRootElement();
//添加contact标签
Element contactEle = root.addElement("contact");
//contact下添加其它标签
Element idEle = contactEle.addElement("id");
idEle.setText(id);
Element nameEle = contactEle.addElement("name");
nameEle.setText(name);
Element sexEle = contactEle.addElement("sex");
sexEle.setText(sex);
Element ageEle = contactEle.addElement("age");
ageEle.setText(age);
Element phoneEle = contactEle.addElement("phone");
phoneEle.setText(phone);
Element qqEle = contactEle.addElement("qq");
qqEle.setText(qq);
Element emailEle = contactEle.addElement("email");
emailEle.setText(email);
/*
* 将document对象写入xml文件中
*/
writeDoc(document);
//提示信息
System.out.println("添加成功!");
//刷新显示联系人列表
getAllContacts();
}
/**
* 将document对象写入xml文件中
*/
private static void writeDoc(Document document) throws FileNotFoundException,
UnsupportedEncodingException, IOException {
//指定文件输出的位置
FileOutputStream out = new FileOutputStream("./src/com/contactSystem/contacts.xml", false);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
//创建写出对象
XMLWriter xmlWriter = new XMLWriter(out, format);
xmlWriter.write(document);
//关闭资源
xmlWriter.close();
}
/**
* 【2】修改联系人
* @throws Exception
*/
private static void editContact() throws Exception {
//修改前先显示所有的联系人
Document document = getAllContacts();
System.out.println("请输入您要修改联系人的id:");
while(true){
//获取输入流
buffReader = new BufferedReader(new InputStreamReader(System.in));
//读取键盘输入的id值
String idValue = buffReader.readLine().trim();
//找到id标签,且其间的文本值为idValue
Element idEle = (Element)document.selectSingleNode("//contact/id[text()='" + idValue + "']");
if(idEle == null){
System.out.println("对不起你输入的id不存在,请重新输入:");
continue;
}
//获取找到文本值为idValue的id标签的父标签
Element ele = idEle.getParent();
//获取下面所有标签集合
List<Element> eles = ele.elements();
//接收键盘输入的修改信息
System.out.println("请依次输入修改信息:");
System.out.print("id:");
String id = buffReader.readLine();
System.out.print("name:");
String name = buffReader.readLine();
System.out.print("sex:");
String sex = buffReader.readLine();
System.out.print("age:");
String age = buffReader.readLine();
System.out.print("phone:");
String phone = buffReader.readLine();
System.out.print("qq:");
String qq = buffReader.readLine();
System.out.print("email:");
String email = buffReader.readLine();
//修改录入的信息
eles.get(0).setText(id);
eles.get(1).setText(name);
eles.get(2).setText(sex);
eles.get(3).setText(age);
eles.get(4).setText(phone);
eles.get(5).setText(qq);
eles.get(6).setText(email);
//将document写入xml
writeDoc(document);
//提示信息
System.out.println("修改成功!");
//刷新显示联系人列表
getAllContacts();
//退出循环
break;
}
}
/**
* 【3】删除联系人
* @throws Exception
*/
private static void deleteContact() throws Exception {
//删除前先显示所有的联系人
Document document = getAllContacts();
while(true){
System.out.println("请输入您要删除联系人的id:");
//获取输入流
buffReader = new BufferedReader(new InputStreamReader(System.in));
//读取键盘输入的id值
String idValue = buffReader.readLine().trim();
//找到id标签,且其间的文本值为idValue
Element idEle = (Element)document.selectSingleNode("//contact/id[text()='" + idValue + "']");
if(idEle == null){
System.out.println("对不起你输入的id不存在!!任意键退出,继续请按0");
String opt = buffReader.readLine();
if(opt.equals("0")){
continue;
}else{
//退出,回到主界面
break;
}
}
//获取找到文本值为idValue的id标签的父标签,删除
idEle.getParent().detach();
//将document写入xml
writeDoc(document);
//提示信息
System.out.println("删除成功!");
//刷新显示
getAllContacts();
//退出循环
break;
}
}
/**
* 【4】查询所有联系人
* @throws Exception
*/
private static Document getAllContacts() throws Exception {
//存放获取到的所有Contact对象
List<Contact> list = new ArrayList<Contact>();
//加载xml文件,获取Document对象
Document document = new SAXReader().read(new File("./src/com/contactSystem/contacts.xml"));
//获取到ContactList根标签
Element root = document.getRootElement();
List<Element> contactEles = root.elements("contact");
for(Element ele : contactEles){
//创建一个Contact对象
Contact contact = new Contact();
//将获取到的标签文本内容,存入Contact对象
contact.setId(ele.elementText("id"));
contact.setName(ele.elementText("name"));
contact.setSex(ele.elementText("sex"));
contact.setAge(ele.elementText("age"));
contact.setPhone(ele.elementText("phone"));
contact.setQq(ele.elementText("qq"));
contact.setEmail(ele.elementText("email"));
//将Contact对象存入list集合
list.add(contact);
}
//遍历集合,打印输出
System.out.print("*****************************************************");
System.out.println("*****************************************************");
int count = 1;
for(Contact c : list){
System.out.println("联系人" + (count++) + "->" + c);
}
System.out.print("*****************************************************");
System.out.println("*****************************************************");
return document;
}
/**
* 退出系统
*/
private static void existSystem() {
System.exit(1);
}
}
Contact.java
package com.contactSystem;
public class Contact {
private String id;
private String name;
private String sex;
private String age;
private String phone;
private String qq;
private String email;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "【id=" + id + ",name=" + name + ",sex=" + sex
+ ",tage=" + age + ",phone=" + phone + ",qq=" + qq
+ ",email=" + email + "】";
}
xml通讯录:
<?xml version="1.0" encoding="utf-8"?>
<ContactList>
<contact>
<id>001</id>
<name>木丁西</name>
<sex>男</sex>
<age>18</age>
<phone>18071897425</phone>
</contact>
<contact>
<id>002</id>
<name>刘先森</name>
<sex>男</sex>
<age>37</age>
<phone>18771897421</phone>
</contact>
<contact>
<id>004</id>
<name>小龙女</name>
<sex>女</sex>
<age>3</age>
<phone>18071897425</phone>
<qq>1012421396</qq>
<email>[email protected]</email>
</contact>
</ContactList>
效果:
①开始界面
②添加联系人
③修改联系人
④删除联系人
⑤查询所有联系人
标签:xPath,xml,String,联系人,System,contact,通讯录,id,out From: https://blog.51cto.com/u_15769923/5974287