Java Contact management System simple Design _java

Source: Internet
Author: User
Tags uuid

The example of this article for you to share the Java Contact management System Graduation design for your reference, the specific content as follows

Requirements:

Use XML to save your data and complete a contact management system.
Users must be authenticated and logged in before they can use the system.
Register, add, delete, and view contact features.
The design is divided into modules .

Two-tier framework-user interaction layer, DAO layer.
Other support layer-data encapsulation layer.
Tool class-encryption, factory bean.

Development steps:

The first step: Design data structure-xml.
The first step: Design data structure-xml.
Step three: Prepare the resources and encode the implementation.
Step Fourth: 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);
 The catch (Exception e) {throw new RuntimeException ("XML document parsing failed ...", e);
 The 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 store 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=na
me2;
 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 Ar
 Raylist<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 ("Contacts");
  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.getel
 Ementsbytagname ("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.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: Login");
  System.out.println ("2: Registration");
  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 ("Two times password input inconsistent, please re-register"); } 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 succeeded ...");
 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, it is still the same DOM tree after each deletion.
  While the operation in the while is the previous operation, so to exit to the previous stage of the login, get the updated DOM tree System.out.println ("1: Show contacts");
  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 ("ordinal \ t name \ t sex t phone");
  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;
 }//} operation ();
 private void Changecontact () {System.out.println ("Enter the serial number of the contact you want to modify:");
 int Num=sc.nextint ();
 Sc.nextline ()//Suction swap line 1 System.out.println ("Please enter the name of the contact you want to modify:");
 String Name=sc.nextline ();
 System.out.println ("Please enter the last name of the contact you want to modify:");
 String Sex=sc.nextline ();
 System.out.println ("Please enter the phone number of the contact you want to modify:");

 String Tel=sc.nextline ();
 Element E=dao.change (Delids[num-1],name,sex,tel); if (e!=null) {System.out.println (num+) Contact is updated: Name: "+e.getelementsbytagname (" name "). Item (0). Gettextcontent () +" Sex: "+e.getelementsbytagname (" Sex "). Item (0). Gettextcontent () +" phone Number: "+e.getelementsbytagname (" tel "). Item (0).
 Gettextcontent ()); }else{SystEm.out.println ("Modify failed ...");
 }} private void Delcontact () {System.out.println ("Please enter the deleted contact Number:");
 int Num=sc.nextint ();
 Sc.nextline ()///Suction swap line Element e=dao.delete (delids[num-1);
 if (e==null) {System.out.println ("Delete failed, no this contact");
 }else{System.out.println (Delete contact: +e.getelementsbytagname ("name"). Item (0). Gettextcontent () + "success ...");
 } private void Addcontact () {System.out.println ("Enter contact information:");
 System.out.println ("Name:");
 String Name=sc.nextline ();
 System.out.println ("Surname not:");
 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 contanctaction ();

 }

}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.