A simple Java command line Add/Remove Contacts program (C language)

Source: Internet
Author: User

Disclaimer: Java for Beginners

:

Contact Person class:

 Public classContact {PrivateString name; PrivateString number; PrivateArraylist<contact>contacts;  PublicArraylist<contact>getcontacts () {returncontacts; }     Public voidSetcontacts (arraylist<contact>contacts) {         This. Contacts =contacts; }     PublicContact (string name, string number) {Super();  This. Name =name;  This. Number =Number ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString GetNumber () {returnNumber ; }     Public voidSetnumber (String number) { This. Number =Number ; }    /*** Add a contact *@paramContact person to add *@returnwhether to add success*/     Public Booleanaddcontact (Contact contact) {returnContacts.add (Contact); }    /*** Delete Contacts *@paramContact person to delete *@returnwhether to delete the success*/     Public Booleandeletecontact (Contact contact) {returnContacts.remove (Contact); }}

Main program Entry class:

 Public classContactdemo {Private StaticArraylist<contact>list;  Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); System.out.print ("Please enter your name:"); String name=Scanner.next (); System.out.print ("Please enter your mobile phone number:"); String Number=Scanner.next (); Contact me=NewContact (name,number); System.out.println ("Your name is:" +me.getname () + ", Mobile number:" +me.getnumber () + "."); System.out.println ("Please select your action: \na: Add contact; \NB: Delete contact \NC: Quit the program!"); String option=Scanner.next ();  while(!option.equalsignorecase ("C")){            if(Option.equalsignorecase ("A") {addcontact (scanner,me); }Else if(Option.equalsignorecase ("B") {System.out.println ("Your choice is" +option);            DeleteContact (Scanner,me); } System.out.println ("Please select your action: \na: Add contact; \NB: Delete contact \NC: Quit the program!"); Option=Scanner.next (); } System.out.println ("Thank you for your use!"); }    /*** Add Contact Action *@paramscanner Input Scan character *@paramMe Myself*/     Public Static voidaddcontact (Scanner scanner,contact me) {System.out.print ("Please enter the name of the contact you want to add:"); String ContactName=Scanner.next (); System.out.print ("Please enter the number you want to add the contact to:"); String Contactnumber=Scanner.next (); Contact=NewContact (contactname,contactnumber); if(List = =NULL) {List=NewArraylist<>();        } me.setcontacts (list); if(me.addcontact (Contact)) {System.out.println ("Add success!"); }Else{System.out.println ("Add failed!");    } showcontact (Me); }    /*** Delete Contact action *@paramscanner Input Scan character *@paramMe Myself*/     Public Static voiddeletecontact (Scanner scanner,contact me) {System.out.print ("Please enter the name of the contact you want to delete:"); String name=Scanner.next (); inti = 0; Try{             while(I <me.getcontacts (). Size ()) { Contact Contact=(Contact) me.getcontacts (). ToArray () [i]; if(Contact.getname (). Equals (name)) {if(Me.getcontacts (). Remove (Contact)) {System.out.println ("Delete succeeded!"); }Else{System.out.println ("Delete failed!");                    } showcontact (Me);  Break; }Else{i++; }            }            if(I>=me.getcontacts (). Size () &&me.getcontacts (). Size ()!=0) {System.out.println ("The contact was not found!"); }        } Catch(NullPointerException e) {e.printstacktrace (); }    }    /*** Show All contacts *@paramMe Myself*/     Public Static voidshowcontact (Contact me) {if(Me.getcontacts (). Size () = = 0) {System.out.println ("You don't have a contact yet!"); return; } System.out.println ("Now your contacts are all" +me.getcontacts (). Size () + "bit, respectively:");  for(inti = 0;i<me.getcontacts (). Size (); i++) {Contact Contact1=(Contact) me.getcontacts (). ToArray () [i]; System.out.println ("The first" + (i+1) + "bit contact name is" +contact1.getname () + ", the number is" +contact1.getnumber () + "."); }    }}

Source code Download (extract code: 28YT)

A simple Java command line Add/Remove Contacts program (C language)

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.