本文实例为大家分享了java联系人管理系统毕业设计,供大家参考,具体内容如下
要求:
请使用XML保存数据,完成一个联系人管理系统。
用户必须经过认证登录后方可以使用系统。
注册、增加、删除、查看联系人功能。
分模块进行设计。
两层框架-用户交互层,Dao层。
其他支持层-数据封装层。
工具类-加密,工厂Bean。
开发步骤:
第一步:设计数据结构-XML。
第一步:设计数据结构-XML。
第三步:准备资源并编码实现。
第四步:运行测试。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<? xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>< contacts >
< user name = "Jack" pwd = "1234" >
< contact id = "707dede609dd4a2990f7cfa4cd5233f9" >
< name >xiaoming</ name >
< sex >male</ sex >
< tel >123456</ tel ></ contact >
< contact id = "80983802eaa6402d8bac8bb39e71c48f" >
< name >12</ name >
< sex >12</ sex >
< tel >12</ tel >
</ contact ></ user >
< user name = "Rose" pwd = "4321" >
< contact id = "eedb795b97194c3aaa9bacda7e2948e9" >
< name >xiaoming</ name >
< sex >female</ sex >
< tel >123</ tel >
</ contact ></ user >
</ contacts >
|
util
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package cn.hncu.contact.util;
import java.util.UUID;
public class IDGenerate {
private IDGenerate(){
}
public static String getID(){
// return UUID.randomUUID().toString();
return UUID.randomUUID().toString().replace( "-" , "" );
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package cn.hncu.contact.util;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
public class myDocumentFactory {
private static final String FILE_NAME= "./xml/users.xml" ;
private static Document dom= null ;
static {
DocumentBuilder db;
try {
db=DocumentBuilderFactory.newInstance().newDocumentBuilder();
dom=db.parse(FILE_NAME);
} catch (Exception e) {
throw new RuntimeException( "xml文档解析失败..." ,e);
}
}
public static Document getDocument(){
return dom;
}
public static void save(){
try {
Transformer tf=TransformerFactory.newInstance().newTransformer();
tf.transform( new DOMSource(dom), new StreamResult(FILE_NAME));
} catch (Exception e) {
throw new RuntimeException( "xml文档存储失败..." , e);
}
// ConfigurationError:配置异常
}
}
|
dao
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package cn.hncu.contact.dao;
import java.util.List;
import java.util.Map;
import org.w3c.dom.Element;
public interface contactDAO {
public abstract boolean login(String name,String pwd);
public abstract List<Map<String, String>> queryAll();
public abstract Element add(String name,String sex,String tel);
public abstract void reg(String name,String pwd);
public Element delete(String id); //默认abstract
public abstract Element change(String id, String name, String sex,
String tel);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
package cn.hncu.contact.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import cn.hncu.contact.util.IDGenerate;
import cn.hncu.contact.util.myDocumentFactory;
public class contactImpl implements contactDAO{
private Element currentUser;
Document dom= null ;
public contactImpl(){
}
// private static String name=null;
// private static String pwd=null;
@Override
// public boolean login(String name2, String pwd2) {
public boolean login(String name, String pwd) {
// name=name2;
// pwd=pwd2;
dom=myDocumentFactory.getDocument();
Element root=(Element) dom.getFirstChild();
NodeList nodelist=root.getElementsByTagName( "user" );
for ( int i= 0 ;i<nodelist.getLength();i++){
Element e=(Element) nodelist.item(i);
if (e.getAttribute( "name" ).equalsIgnoreCase(name)&&e.getAttribute( "pwd" ).equalsIgnoreCase(pwd)){
currentUser=e;
return true ;
}
}
return false ;
}
@Override
public List<Map<String, String>> queryAll() {
List<Map<String, String>> list= new ArrayList<Map<String,String>>();
if (currentUser== null ){
return list ;
}
NodeList nodeList=currentUser.getElementsByTagName( "contact" );
for ( int i= 0 ;i<nodeList.getLength();i++){
Map<String, String> map= new HashMap<String, String>();
Element e=(Element) nodeList.item(i);
String id=e.getAttribute( "id" );
map.put( "id" , id);
String name=e.getElementsByTagName( "name" ).item( 0 ).getTextContent();
map.put( "name" , name);
String sex=e.getElementsByTagName( "sex" ).item( 0 ).getTextContent();
map.put( "sex" , sex);
String tel=e.getElementsByTagName( "tel" ).item( 0 ).getTextContent();
map.put( "tel" , tel);
list.add(map);
}
return list;
}
@Override
public Element add(String name,String sex,String tel) {
Document dom=myDocumentFactory.getDocument();
Element eNewContact=dom.createElement( "contact" );
eNewContact.setAttribute( "id" , IDGenerate.getID());
Element nameNew=dom.createElement( "name" );
nameNew.setTextContent(name);
eNewContact.appendChild(nameNew);
Element sexNew=dom.createElement( "sex" );
sexNew.setTextContent(sex);
eNewContact.appendChild(sexNew);
Element telNew=dom.createElement( "tel" );
telNew.setTextContent(tel);
eNewContact.appendChild(telNew);
currentUser.appendChild(eNewContact);
myDocumentFactory.save();
// login(name, pwd);
return eNewContact;
}
public Element delete(String id) {
NodeList nodeList=currentUser.getElementsByTagName( "contact" );
for ( int i= 0 ;i<nodeList.getLength();i++){
Element e=(Element) nodeList.item(i);
if (e.getAttribute( "id" ).equals(id)){
currentUser.removeChild(e); //把节点从树中移除
myDocumentFactory.save();
// login(name, pwd);
return e;
}
}
return null ;
}
@Override
public void reg(String name, String pwd) {
Document dom=myDocumentFactory.getDocument();
Element userNew=dom.createElement( "user" );
userNew.setAttribute( "name" , name);
userNew.setAttribute( "pwd" , pwd);
dom.getFirstChild().appendChild(userNew);
myDocumentFactory.save();
}
@Override
public Element change(String id, String name, String sex, String tel) {
NodeList nodeList=currentUser.getElementsByTagName( "contact" );
for ( int i= 0 ;i<nodeList.getLength();i++){
Element e=(Element) nodeList.item(i);
if (e.getAttribute( "id" ).equals(id)){
e.getElementsByTagName( "name" ).item( 0 ).setTextContent(name);
e.getElementsByTagName( "sex" ).item( 0 ).setTextContent(sex);
e.getElementsByTagName( "tel" ).item( 0 ).setTextContent(tel);
myDocumentFactory.save();
// login(name, pwd);
return e;
}
}
return null ;
}
}
|
1
2
3
4
5
6
7
8
9
10
|
package cn.hncu.contact.dao;
public class contactDAOFactory {
private contactDAOFactory(){
}
public static contactDAO getContactDAO(){
return new contactImpl();
}
}
|
cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
package cn.hncu.contact.cmd;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.w3c.dom.Element;
import cn.hncu.contact.dao.contactDAO;
import cn.hncu.contact.dao.contactDAOFactory;
public class contanctAction {
private contactDAO dao=contactDAOFactory.getContactDAO();
private Scanner sc= null ;
private String delIds[];
public contanctAction(){
sc= new Scanner(System.in);
while ( true ) {
System.out.println( "1:登录" );
System.out.println( "2:注册" );
System.out.println( "0:退出" );
String op=sc.nextLine();
if ( "1" .equals(op)) {
Login();
} else if ( "2" .equals(op)) {
Reg();
} else {
// System.exit(0);
break ;
}
}
}
private void Reg() {
System.out.println( "请输入用户名:" );
String name=sc.nextLine();
System.out.println( "请输入用户密码:" );
String pwd=sc.nextLine();
System.out.println( "请确认用户密码:" );
String pwd2=sc.nextLine();
if (pwd.equals(pwd2)){
dao.reg(name,pwd);
} else {
System.out.println( "两次密码输入不一致,请重新注册" );
}
}
private void Login() {
System.out.println( "请输入用户名:" );
String name=sc.nextLine();
System.out.println( "请输入用户密码:" );
String pwd=sc.nextLine();
boolean boo=dao.login(name, pwd);
if (boo){
System.out.println( "登录成功..." );
operation();
} else {
System.out.println( "denglushibai" );
}
}
private void operation() {
List<Map<String, String>> list = dao.queryAll();
delIds= new String[list.size()];
int i= 0 ;
for (Map<String, String> map : list) {
String id=map.get( "id" );
delIds[i++]=id;
}
// while (true) {
//因为共用同一棵dom树,所以每次增删改查之后,还是原来那棵dom树.
//而while内的操作都是对之前的操作,所以要退出到上一阶段重新登陆,获取更新之后的dom树
System.out.println( "1:显示联系人" );
System.out.println( "2:添加联系人" );
System.out.println( "3:删除联系人" );
System.out.println( "4:修改联系人" );
System.out.println( "0:退出" );
String sel = sc.nextLine();
if ( "1" .equals(sel)) {
System.out.println( "序号\t姓名\t性别\t电话" );
System.out.println( "------------------------------" );
int j = 1 ;
for (Map<String, String> map : list) {
String name = map.get( "name" );
String sex = map.get( "sex" );
String tel = map.get( "tel" );
System.out.println((j++) + "\t" + name + "\t" + sex + "\t"
+ tel);
}
} else if ( "2" .equals(sel)) {
addContact();
} else if ( "3" .equals(sel)) {
delContact();
} else if ( "4" .equals(sel)) {
changeContact();
} else if ( "0" .equals(sel)) {
return ;
// break;
}
// }
operation();
}
private void changeContact() {
System.out.println( "请输入要修改的联系人的序号:" );
int num=sc.nextInt();
sc.nextLine(); //吸掉换行符1
System.out.println( "请输入要修改的联系人的姓名:" );
String name=sc.nextLine();
System.out.println( "请输入要修改的联系人的姓别:" );
String sex=sc.nextLine();
System.out.println( "请输入要修改的联系人的电话:" );
String tel=sc.nextLine();
Element e=dao.change(delIds[num- 1 ],name,sex,tel);
if (e!= null ){
System.out.println(num+ "号联系人更新之后:姓名:" +e.getElementsByTagName( "name" ).item( 0 ).getTextContent()
+ "性别:" +e.getElementsByTagName( "sex" ).item( 0 ).getTextContent()
+ "电话号码:" +e.getElementsByTagName( "tel" ).item( 0 ).getTextContent());
} else {
System.out.println( "修改失败..." );
}
}
private void delContact() {
System.out.println( "请输入删除的联系人序号:" );
int num=sc.nextInt();
sc.nextLine(); //吸掉换行符
Element e=dao.delete(delIds[num- 1 ]);
if (e== null ){
System.out.println( "删除失败,无此联系人" );
} else {
System.out.println( "删除联系人:" +e.getElementsByTagName( "name" ).item( 0 ).getTextContent()+ "成功..." );
}
}
private void addContact() {
System.out.println( "请输入联系人信息:" );
System.out.println( "姓名:" );
String name=sc.nextLine();
System.out.println( "姓别:" );
String sex=sc.nextLine();
System.out.println( "电话:" );
String tel=sc.nextLine();
Element e=dao.add( name,sex, tel);
System.out.println( "添加联系人" +e.getElementsByTagName( "name" ).item( 0 ).getTextContent()+ "成功..." );
}
public static void main(String[] args) {
new contanctAction();
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。