本文實例為大家分享了java聯(lián)系人管理系統(tǒng)畢業(yè)設計,供大家參考,具體內(nèi)容如下
要求:
請使用XML保存數(shù)據(jù),完成一個聯(lián)系人管理系統(tǒng)。
用戶必須經(jīng)過認證登錄后方可以使用系統(tǒng)。
注冊、增加、刪除、查看聯(lián)系人功能。
分模塊進行設計。
兩層框架-用戶交互層,Dao層。
其他支持層-數(shù)據(jù)封裝層。
工具類-加密,工廠Bean。
開發(fā)步驟:
第一步:設計數(shù)據(jù)結(jié)構(gòu)-XML。
第一步:設計數(shù)據(jù)結(jié)構(gòu)-XML。
第三步:準備資源并編碼實現(xiàn)。
第四步:運行測試。
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); //把節(jié)點從樹中移除 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內(nèi)的操作都是對之前的操作,所以要退出到上一階段重新登陸,獲取更新之后的dom樹 System.out.println( "1:顯示聯(lián)系人" ); System.out.println( "2:添加聯(lián)系人" ); System.out.println( "3:刪除聯(lián)系人" ); System.out.println( "4:修改聯(lián)系人" ); 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( "請輸入要修改的聯(lián)系人的序號:" ); int num=sc.nextInt(); sc.nextLine(); //吸掉換行符1 System.out.println( "請輸入要修改的聯(lián)系人的姓名:" ); String name=sc.nextLine(); System.out.println( "請輸入要修改的聯(lián)系人的姓別:" ); String sex=sc.nextLine(); System.out.println( "請輸入要修改的聯(lián)系人的電話:" ); String tel=sc.nextLine(); Element e=dao.change(delIds[num- 1 ],name,sex,tel); if (e!= null ){ System.out.println(num+ "號聯(lián)系人更新之后:姓名:" +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( "請輸入刪除的聯(lián)系人序號:" ); int num=sc.nextInt(); sc.nextLine(); //吸掉換行符 Element e=dao.delete(delIds[num- 1 ]); if (e== null ){ System.out.println( "刪除失敗,無此聯(lián)系人" ); } else { System.out.println( "刪除聯(lián)系人:" +e.getElementsByTagName( "name" ).item( 0 ).getTextContent()+ "成功..." ); } } private void addContact() { System.out.println( "請輸入聯(lián)系人信息:" ); 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( "添加聯(lián)系人" +e.getElementsByTagName( "name" ).item( 0 ).getTextContent()+ "成功..." ); } public static void main(String[] args) { new contanctAction(); } } |
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。