The examples in this article share with you the graduation design of the Java contact management system for your reference. The specific content is as follows
Require:
Please use XML to save data and complete a contact management system.
Users must be authenticated and logged in before they can use the system.
Register, add, delete, view contact functions.
Designed in modules .
Two-layer framework-user interaction layer, Dao layer.
Other support layers - data encapsulation layer.
Tools - encryption, factory bean.
Development steps:
Step 1: Design the data structure - XML.
Step 1: Design the data structure - XML.
Step 3: Prepare resources and encode them to implement them.
Step 4: Run the test.
<?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
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("-", ""); }} 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 document parsing failed...",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 document storage failed...", e); } // ConfigurationError:Configuration exception}}dao
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);//Default abstract public abstract Element change(String id, String name, String sex, String tel);} 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);//Remove the node from the tree 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; }} package cn.hncu.contact.dao;public class contactDAOFactory { private contactDAOFactory(){ } public static contactDAO getContactDAO(){ return new contactImpl(); }}cmd
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.contactDAO;import cn.hncu.contact.dao.contactDAOFactory;public class contactAction { private contactDAO dao=contactDAOFactory.getContactDAO(); private Scanner sc=null; private String delIds[]; public contactAction(){ sc=new Scanner(System.in); while (true) { System.out.println("1:Login"); System.out.println("2:Register"); System.out.println("0:Exit"); 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("Please enter username:"); String name=sc.nextLine(); System.out.println("Please enter user password:"); String pwd=sc.nextLine(); System.out.println("Please confirm user password:"); String pwd2=sc.nextLine(); if(pwd.equals(pwd2)){ dao.reg(name,pwd); }else{ System.out.println("The password inputs are inconsistent, please register again"); } } private void Login() { System.out.println("Please enter user name:"); String name=sc.nextLine(); System.out.println("Please enter user password:"); String pwd=sc.nextLine(); boolean boo=dao.login(name, pwd); if(boo){ System.out.println("Login successfully..."); 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) { //Because the same dom tree is shared, after each addition, deletion, modification and check, it is still the original dom tree. //The operations in while are the previous operations, so you have to exit to the previous stage and log in again to get the updated dom tree System.out.println("1:Show contact"); System.out.println("2:Add contact"); System.out.println("3:Delete contact"); System.out.println("4:Modify contact"); System.out.println("0:Exit"); String sel = sc.nextLine(); if ("1".equals(sel)) { System.out.println("Serial number/tname/tGender/tTel"); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 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("Please enter the serial number of the contact to be modified:"); int num=sc.nextInt(); sc.nextLine();//Suck out the newline character 1 System.out.println("Please enter the name of the contact to be modified:"); String name=sc.nextLine(); System.out.println("Please enter the last name of the contact to be modified:"); String sex=sc.nextLine(); System.out.println("Please enter the phone number of the contact to be modified:"); String tel=sc.nextLine(); Element e=dao.change(delIds[num-1],name,sex,tel); if(e!=null){ System.out.println(num+" sign after the contact is updated: name: "+e.getElementsByTagName("name").item(0).getTextContent() +"Gender:"+e.getElementsByTagName("sex").item(0).getTextContent() +"Tel Number:"+e.getElementsByTagName("tel").item(0).getTextContent()); }else{ System.out.println("Modification failed..."); } } private void delContact() { System.out.println("Please enter the deleted contact number:"); int num=sc.nextInt(); sc.nextLine();//Suck out the newline character Element e=dao.delete(delIds[num-1]); if(e==null){ System.out.println("Delete failed, no contact"); }else{ System.out.println("Delete contact:"+e.getElementsByTagName("name").item(0).getTextContent()+"Success..."); } } private void addContact() { System.out.println("Please enter contact information:"); System.out.println("Name:"); String name=sc.nextLine(); System.out.println("Name:"); String sex=sc.nextLine(); System.out.println("Tel:"); String tel=sc.nextLine(); Element e=dao.add( name,sex, tel); System.out.println("Add contact"+e.getElementsByTagName("name").item(0).getTextContent()+"Success..."); } public static void main(String[] args) { new contactAction(); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.