In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to use Java to achieve contact management system", the content is detailed, the steps are clear, and the details are handled properly. I hope this "how to use Java to achieve contact management system" article can help you solve your doubts.
A simple contact management system based on eclipse
A sort is made for the initials of the contact name, and a judgment is made between the mobile phone number and the e-mail address when the information is saved. Only when entering the correct format of the mobile phone number and the e-mail address can the data be successfully saved.
Contact basic property class
This class contains contact number, name, mobile phone number, QQ, email and other information.
/ * * No. * / private int sno;/** name * / private String name;/** Mobile number * / private String phone;/** QQ*/private String qq;/** email * / private String email
Quickly generate corresponding methods and constructors
Public User (int sno, String name, String phone, String qq, String email) {super (); this.sno = sno; this.name = name; this.phone = phone; this.qq = email;} public int getSno () {return sno;} public void setSno (int sno) {this.sno = sno } public String getName () {return name;} public void setName (String name) {this.name = name;} public String getQq () {return qq;} public void setQq (String qq) {this.qq = qq;} public String getEmail () {return email;} public void setEmail (String email) {this.email = email } public String getPhone () {return phone;} public void setPhone (String phone) {this.phone = phone;} @ Override public String toString () {return sno + "\ t" + name + "\ t" + phone + "\ t" + qq + "\ t" + email;}
As a result, the definition of contact attributes is completed, and then the function of managing contacts is realized.
Functional class
I implement the function together in both the function class and the test class, because the method is called repeatedly in the test class
First define an ArrayList array to store the information
Static ArrayList list = new ArrayList ()
Add contact method
/ * * 1-add contact * / public void add (User u) {list.add (u);}
Contact query function (query according to different information: number, name, mobile phone number)
/ * * 2-contact query (by number) * / public User findBySno (int sno) {User us = null; for (User u: list) {if (u.getSno () = = sno) {us = u; break;}} return us } / * * 3-contact query (by name) * / public User findByName (String name) {User us = null; for (User u: list) {if (u.getName (). Equals (name)) {us = u; break;}} return us } / * * 4-contact query (based on mobile phone number) * / public User findByPhone (String phone) {User us = null; for (User u: list) {if (u.getPhone (). Equals (phone)) {us = u; break;}} return us;}
Contact list
/ * * 5-contact list * / public ArrayList findAll () {return list;}
Delete contact
/ * * 6-Delete contact * / public boolean delete (int sno) {User user = findBySno (sno); if (user! = null) {return list.remove (user);} return false;}
This kind of completion
Test implementation class
The application of the method to realize the function in this kind of calling function class
First create the object and the console input function
Private UserManage um = new UserManage (); private Scanner sc = new Scanner (System.in)
Create a window instruction interface to provide user prompt messages
/ * * input window instruction interface * / public void menu () {msg ("= ="); msg ("= SOFTEEM contact management system ="); msg ("= [1] contact addition ="); msg ("= [2] View contact information by number ="); msg ("= [3] View contact information by name =") Msg ("= [4] View contact information by mobile phone number ="); msg ("= [5] View all contacts ="); msg ("= [6] delete contacts by label ="); msg ("= [0] exit system ="); msg ("Please enter instructions:"); start ();}
Follow the prompt message to use the function, and each function is called in this method
/ * * Program key entry * / private void start () {sc = new Scanner (System.in); int I = sc.nextInt (); switch (I) {case 1: add (); break; case 2: queryById (); break; case 3: queryByname (); break Case 4: queryByPhone (); break; case 5: list (); break; case 6: delete (); break; case 0: exit (); break; default: msg ("Please enter correct operation instructions!") Break;} menu ();}
The addition, deletion, modification and search function of the contact, and the function of sorting the initials of the contact name has been added
/ * * 1-client implementation added by the user * / private void add () {String regex = "^ 1 [13456789]\\ d {9} $"; String email= "\\ walled @\\ w+\\.\\ w+"; msg ("Please enter user information: ((in the following format: number / name / mobile number / QQ/ mailbox)"); sc = new Scanner (System.in) String s = sc.nextLine (); / / intercept user information according to "/" String [] info = s.split ("/"); if (um.findBySno (Integer.parseInt (info [0]))! = null) {msg ("the ID user already exists, please re-enter!"); add (); return } else {User u = new User (Integer.parseInt (info [0]), info [1], info [2], info [3], info [4]); if (info [2] .mobile (regex)) {} else {msg ("incorrect format, please enter the correct mobile phone number!"); add () } if (info [4] .email (email)) {} else {msg ("incorrect format, please enter the correct mailbox!"); add ();} um.add (u); msg ("added successfully!") }} / * * 2-query contacts by number * / private void queryById () {sc = new Scanner (System.in); msg ("Please enter contact number:"); int sno = sc.nextInt (); User u = um.findBySno (sno) If (u = = null) {msg (sno + "number does not exist, please re-enter!"); queryById (); return;} msg ("number\ t name\ t mobile phone number\ t\ tQQ\ t mailbox"); msg (u) } / * * 3-query contacts by name * / private void queryByname () {sc = new Scanner (System.in); msg ("Please enter contact name:"); String name = sc.next (); User u = um.findByName (name); if (u = = null) {msg (name + "does not exist, please re-enter") QueryByname (); return;} msg ("No.\ t name\ t Mobile number\ t\ tQQ\ t email"); msg (u);} / * * 4-query contacts by mobile phone number * / private void queryByPhone () {sc = new Scanner (System.in); msg ("Please enter contact phone number:") String phone = sc.next (); User u = um.findByPhone (phone); if (u = = null) {msg (phone + "does not exist, please re-enter"); queryByPhone (); return;} msg ("number\ t name\ t cell phone number\ t\ tQQ\ t mailbox"); msg (u) } / * * 5-display all contacts (sorted by first name pinyin) * / private void list () {(um.findAll ()) .sort ((a, b)-> {/ / get the names of the two contacts to be compared respectively String name1 = a.getName (); String name2 = b.getName () / / declare variables to store converted Hanyu pinyin String p1 = ""; String p2 = ""; / / convert Chinese characters to pinyin for (int I = 0; I < name1.length (); iSum +) {p1 + = PinyinHelper.toHanyuPinyinStringArray (name1.charAt (I)) [0];} for (int I = 0) I < name2.length (); iTunes +) {p2 + = PinyinHelper.toHanyuPinyinStringArray (name2.charAt (I)) [0];} return p1.compareTo (p2);}); msg ("number\ t name\ t mobile phone number\ t\ tQQ\ t mailbox") For (User u: um.findAll ()) {msg (u);} / * * 6-Delete contacts by number * / private void delete () {sc = new Scanner (System.in); msg ("Please enter user ID:"); int sno = sc.nextInt () If (um.delete (sno)) {msg ("deletion successful!");} else {msg ("deletion failed!");}} public void msg (Object obj) {System.out.println (obj);}
The exit function of the program
/ * * 0-formal exit * / private void exit () {sc = new Scanner (System.in); msg ("are you sure you want to quit? (YCompN)"); String op = sc.next (); if (op.equalsIgnoreCase ("Y")) {msg ("Thank you for using, goodbye!"); System.exit (1);}}
The program entrance of the contact management system
Public static void main (String [] args) {TestUser tu = new TestUser (); tu.menu ();}
There is no technical difficulty in the function implementation, as long as the object method call and some simple judgment statements are used flexibly, the system can be easily completed.
Read here, this article "how to use Java to achieve contact management system" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.