Address Book (1)

Source: Internet
Author: User

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/194J36458-0.gif "/> after a period of effort, the address book this simple project is temporarily come to an end, although it is still running on the ugly Black Box console, you have to make a debut, but I believe that after continuous learning and continuous improvement, we will soon be able to meet you again with new faces.

This address book project consists of two modules. The first is the entity class. The second is the function class. The entity class includes the attributes of fields and a method for rewriting strings. The function class mainly includes some field processing functions, such as deleting and updating information and obtaining the content of the entire document. Of course, there are some other classes, such as enumeration classes and comparator classes, which are all improvements to improve user friendliness so that this address book can serve the public more conveniently. Let's take a look at this giant thing:

First: entity class. Check the Code:

[Serializable] // sets the object class to Serializable. Class Card {string name; public string Name {get {return name;} set {name = value ;}} string phone; public string Phone {get {return phone ;} set {phone = value ;}} string qq; public string Qq {get {return qq ;}set {qq = value ;}} string email; public string Email {get {return email;} set {email = value ;}} string address; public string Address {get {return address ;}set {address = value ;}} public override string ToString () {string s = string. format ("{0} \ t {1} \ t {2} \ t {3} \ t {4}", name, phone, qq, email, address ); return s ;}

Second: function class. Check the Code:

Class CardManager {// prepare an empty set to store user-read-write fields as the global variables of this class), so that users can read and write public List <Card> cards = new List <Card> (); /// <summary> /// create a constructor for the set and write the value to the set. /// </summary> public CardManager () {// define a variable of the bool type to determine whether the target File exists bool B = File. exists (path); // if the target file does not exist, if (! B) {return;} // create an XML document XmlDocument doc = new XmlDocument (); doc. load ("test. xml "); XmlNode root = doc. documentElement; foreach (XmlNode Node in root. childNodes) {Card card = new Card (); string name = Node. childNodes [0]. innerText; string phone = Node. childNodes [1]. innerText; string qq = Node. childNodes [2]. innerText; string email = Node. childNodes [3]. innerText; string address = Node. childNodes [4]. InnerText; card. name = name; card. phone = phone; card. qq = qq; card. email = email; card. address = address; cards. add (card );}} /// <summary> /// return the content in the node to an object /// </summary> /// <param name = "node"> </param> // /<returns> </returns> private Card GetCardFromNode (XmlNode node) {Card card = new Card (); foreach (XmlNode childNode in node. childNodes) {string nodeName = childNode. name; string nodeText = ChildNode. innerText; CardEnum ce = (CardEnum) Enum. parse (typeof (CardEnum), nodeName); switch (ce) {case CardEnum. name: card. name = nodeText; break; case CardEnum. phone: card. phone = nodeText; break; case CardEnum. QQ: card. qq = nodeText; break; case CardEnum. email: card. email = nodeText; break; case CardEnum. address: card. address = nodeText; break; default: break;} return card;} // The prepared target path stri. Ng path = "f: \ cards.txt"; // <summary> // function method: add feature /// </summary> /// <param name = "card"> </param> public bool Add (Card card) {// determine the field to be added, whether the collection exists. if yes, false if (GetCardByName (card. name )! = Null) {return false;} // if it does not exist, the fields to be added will be appended to the cards in the prepared set. add (card); // write the set to a file and return true Save (); return true ;} /// <summary> /// write the set to a file /// </summary> private void Save () {// create a writer object, streamWriter sw = new StreamWriter (path, false); // create an XML document XmlDocument doc = new XmlDocument (); // define the node XmlDeclaration declare = doc. createXmlDeclaration ("1.0", "UTF-8", null); // Add a root node XmlNode ro Ot = doc. createElement ("cards"); // write the definition node and root node to the XML document to go to doc. appendChild (declare); doc. appendChild (root); // traverses the set and writes data one by one. After writing the data, close the stream foreach (Card card in cards) {// create a sub-node card XmlNode childNode = doc. createElement ("card"); // Add the child node to the root node as the root node. appendChild (childNode); // create five Sun Tzu nodes (attribute fields) XmlNode grandSon1 = doc. createElement ("name"); XmlNode grandSon2 = doc. createElement ("phone"); XmlNode grandSon3 = doc. create Element ("qq"); XmlNode grandSon4 = doc. createElement ("email"); XmlNode grandSon5 = doc. createElement ("address"); // Add five Sun Tzu nodes to the child node to childNode. appendChild (grandSon1); childNode. appendChild (grandSon2); childNode. appendChild (grandSon3); childNode. appendChild (grandSon4); childNode. appendChild (grandSon5); // obtain the value of the Sun Tzu node.) grandSon1.InnerText = "pan Jing"; grandSon2.InnerText = "18700101911"; grandSon3.InnerTex T = "1052828278"; grandSon4.InnerText = "105282782qq.com"; grandSon5.InnerText = "Shaanxi yongshou"; // save all objects as XML documents, the XML document is designed to describe the sub-function and identify whether it is a name or a phone number. save ("test. xml "); // XmlNode childNode = doc. createElement ("card"); // root. appendChild (childNode); // childNode. value = "name"; // root. attributes. setNamedItem (childNode); // doc. save ("test. xml ");} sw. flush (); sw. close () ;}/// <summary> // obtain all address books Information /// </summary> /// <returns> address book set </returns> public List <Card> GetAllCards () {return cards ;} /// <summary> /// delete an address book object /// </summary> /// <param name = "name"> according to a name </param> public void Delete (string name) {// define an index to store the position of the block to be deleted in the set. The value-1 indicates that int index =-1 does not exist. // search for this element from the set, if it is found, the loop for (int I = 0; I <cards. count; I ++) {if (cards [I]. name = name) {index = I; break ;}// if not found, if (in Dex =-1) {return;} // if found, delete this field cards. removeAt (index); Save ();} public Card GetCardByName (string name) {// obtain all address book objects int index =-1; // search for this element from the set. If this element is found, the loop for (int I = 0; I <cards. count; I ++) {if (cards [I]. name = name) {index = I; break ;}// if not found, null if (index =-1) {return null;} is returned ;} // return this object Card = cards [index]; return card ;}/// <summary> /// search for object objects by name /// </su Mmary> /// <param name = "phone"> </param> /// <returns> set </returns> public List <Card> GetCardByPhone (string phone) {// create a collection Object List <Card> list = new List <Card> (); // query this element from the collection for (int I = 0; I <cards. count; I ++) {// query by phone and add the retrieved object to the set to go if (cards [I]. phone = phone) {list. add (cards [I]) ;}// return this collection of people with the same phone number) return list ;} /// <summary> /// update the address book /// </summary> /// <param name = "card"> </para M> public void UpdateCard (Card card) {// defines an index to store the position of the object to be updated in the set. int index =-1; // traverse the entire set to find the object to be updated, find and update the object information, and end the loop for (int I = 0; I <cards. count; I ++) {if (cards [I]. name = card. name) {cards [I]. phone = card. phone; cards [I]. qq = card. qq; cards [I]. email = card. email; cards [I]. address = card. address; index = I; break; }}// if index =-1 indicates no result is found, if (index =-1) {return;} is returned ;} // If an updated set is found, write it to the file Sa. Ve () ;}/// <summary> /// sorting function, sort the elements in the Set /// </summary> /// <param name = "cards"> </param> /// public List <Card> Sort (CardEnum cardEnum) {for (int I = 0; I <cards. count-1; I ++) {for (int j = 0; j <cards. count-I-1; j ++) {int compareResult = 0; IComparer <Card> temp = null; switch (cardEnum) {case CardEnum. name: temp = new CardNameComparer (); break; case CardEnum. phone: temp = new CardPho NeComparer (); break; case CardEnum. QQ: temp = new CardQqComparer (); break; case CardEnum. email: temp = new CardEmailComparer (); break; case CardEnum. address: temp = new CardAddressComparer (); break; default: break;} // sort by bubble sort. Compare two adjacent objects. If the preceding value is large, the switch location is compareResult = temp. compare (cards [j], cards [j + 1]); if (compareResult> 0) {Card tempCard = cards [j]; cards [j] = cards [j + 1]; cards [j + 1] = tempCard ;}} return cards ;}}

Below is a comparator for several fields. Here I write only one, which is basically similar. Check the Code:

class CardNameComparer : IComparer<Card>    {        public int Compare(Card x, Card y)        {            return x.Name.CompareTo(y.Name);        }    }

There is also an enumeration class, which is especially important in function classes to restrict user input. Check the Code:

enum CardEnum   {       Name,       Phone,       QQ,       Email,       Address   }

Finally, the program is implemented. This step is still carried out in the main method. You can call the methods in the function class one by one. When calling the function class, check whether the return types are consistent. The detailed code is not provided here.

I believe there will be Address Book 2 soon.) I'm looking forward to it!

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/194J36360-1.gif "/> Ajax Girl, come on! 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/194J36360-1.gif "/>


This article is from the "Ajax girl" blog!

Related Article

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.