Java simulation and a phone book operation

Source: Internet
Author: User

Ha ha. People usually use the phone book. Here's a simple phone book that uses Java to simulate ...

First, give the abstract class of the contact.

Package net.itaem.po;/** * * Phone person's information * */public class User {private string name;private string Phonenumber;private string Companyname;private string Email;private string address;private string Birthday;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getphonenumber () {return phonenumber;} public void Setphonenumber (String phonenumber) {this.phonenumber = PhoneNumber;} Public String Getcompanyname () {return companyName;} public void Setcompanyname (String companyName) {this.companyname = CompanyName;} Public String Getemail () {return email;} public void Setemail (String email) {this.email = email;} Public String getaddress () {return address;} public void setaddress (String address) {this.address = address;} Public String Getbirthday () {return birthday;} public void Setbirthday (String birthday) {this.birthday = birthday;} @Overridepublic String toString () {return "User [name=" + name + ", phonenumber=" + phonenumber+ ", companyname=" + compan YnamE + ", email=" + email+ ", address=" + address + ", birthday=" + Birthday + "]";} Public User (string name, String phonenumber, String companyname,string Email, string address, string birthday) {THIS.name = Name;this.phonenumber = Phonenumber;this.companyname = Companyname;this.email = email;this.address = Address; This.birthday = Birthday;} Public User (string name, String phonenumber) {this.name = Name;this.phonenumber = PhoneNumber;}}

Gives the Userdao of the operational database. Here is just a simple analog operation ... So use Print to prompt for action results


The following is a tool class for obtaining the request content of view in MVC

Package Net.itaem.tool;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStreamReader; public class GetInput {    private static BufferedReader Bufreader;        static{    bufreader = new BufferedReader (new InputStreamReader (system.in));    } public static string getinputstring (String info) throws ioexception{system.out.println (info); String result = Null;result = Bufreader.readline ();   Reads a row of data from a memory input return result; public static void Main (string[] args) throws ioexception{}}

The following gives the control class, equivalent to the C,controller in MVC, which is responsible for interacting with other classes such as Dao,view,tool ...

Package Net.itaem.operate;import Java.io.ioexception;import Net.itaem.dao.userdao;import net.itaem.po.User;import net.itaem.tool.getinput;/** * Operation class * */public class Operate {public Operate () {Start ();} public void Start () {try {while (true) {//execute run ();}} catch (IOException e) {e.printstacktrace ()}} private void Run () throws Ioexception{system.out.println ("====================== telephone ==================="); System.out.println ("1 View all Contacts"); System.out.println ("2 Add Contacts"); System.out.println ("3 delete Contacts"); System.out.println ("4 Search Contacts"); System.out.println ("5 exit"); String Choose = Getinput.getinputstring ("Enter your selection"), char choice = Choose.tochararray () [0];switch (choice) {case ' 1 ':  System.out.println (Userdao.getinstance (). FINDALL ()); Output All phones This user break;case ' 2 ': userdao.getinstance (). Add (New User (getinput.getinputstring ("input name"), Getinput.getinputstring ("Input phone number")); Break;case ' 3 ': userdao.getinstance (). Delete (new User (getinput.getinputstring ("Input name"), getinput.getinputstring ("Input phone number")); Break;case ' 4 ': System.out.println(Userdao.getinstance (). Findusersbykeyword (getinput.getinputstring ("Input to query keyword")); Break;case ' 5 ': Exit (); BREAK;DEFAULT:SYSTEM.OUT.PRINTLN ("Sorry your input is wrong, please input 1, 2, 3, 4");}}  private void Exit () {system.exit (0); Exit}public static void Main (string[] args) {new Operate (). Start ();}}

Detailed program execution results you can test it yourself, or read the code to see if it should be clear ...

The following is a class diagram:




watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmdq2otawmw==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Summary: This simulation is not much of a challenge. But the point is to use object-oriented thinking to design, and then decoupling between the various classes.

Java simulation and a phone book operation

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.