Java implements simple console address book and java console address book

Source: Internet
Author: User

Java implements simple console address book and java console address book

You can use the main menu to control sub-menus at all levels, add records, search for records, delete records, modify records, sort records, and exit the system. A total of six functional modules.

The figure above shows the functions of each module and uses a regular expression to determine whether the input conforms to the rules. This system reflects the MVC design pattern and clearly calls the division of labor.

It mainly includes the following classes:

APP class: main function class of the program, entry to the beginning of the program
Menu class: it contains simple page display effects and has no function implementation.
TelNoteRegex class: regular expression, which is used to determine whether the input is successful.
Operate class: business logic class. All functions are implemented in this class.
Person class: encapsulate data.

Specific implementation code:

Implementation Code of the App class:

package com.bjsxt.iphone;public class App { public static void main(String[] args) {  new App().start(); } public void start(){  Menu m=new Menu();  Operate o=new Operate();  TelNoteRegex reg=new TelNoteRegex();  while(true){   m.mainMenu();   int key=reg.menuRegex(1, 6);   switch(key){   case 1:    o.addLogic();    break;   case 2:    o.searchLogic();    break;   case 3:    o.modifyLogicLogic();    break;   case 4:    o.deleteLogic();    break;   case 5:    o.orderLogic();    break;   case 6:    System.exit(0);    break;   }  } }}

Specific implementation code of the Person class:

Package com. bjsxt. iphone; public class Person {private int id; // user id attribute private String name; // user name attribute private String age; // user age attribute private String sex; // user gender attribute private String telNum; // user phone number attribute private String address; // user address attribute public Person () {} public Person (int id, String name, string age, String sex, String telNum, String address) {this. id = id; this. name = name; this. age = age; this. sex = sex; this. telNum = telNum; this. address = address;} public int getId () {return id;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getAge () {return age;} public void setAge (String age) {this. age = age;} public String getSex () {return sex;} public void setSex (String sex) {this. sex = sex;} public String getTelNum () {return telNum;} public void setTelNum (String telNum) {this. telNum = telNum;} public String getAddress () {return address;} public void setAddress (String address) {this. address = address ;}}

TelNoteRegex class code:

Package com. bjsxt. iphone; import java. util. secret; public class TelNoteRegex {// check the menu item @ SuppressWarnings ("resource") public int menuRegex (int min, int max) {String regex = "[0-9] {1}"; Limit SC = new limit (System. in); while (true) {String input = SC. nextLine (); // verify if (input. matches (regex) {int key = Integer. parseInt (input); if (key >=min & key <= max) {return key;} else {System. out. println ("the input menu item does not match Requirement! ") ;}} Else {System. out. println (" the entered content is incorrect! ") ;}}// User input name verification @ SuppressWarnings (" resource ") public String nameRegex (String name) {String pattern = "[\ u4e00-\ u9fa5] +"; String str = name; symbol SC = new symbol (System. in); while (true) {if (str. matches (pattern) {return str;} else {System. out. println ("enter invalid characters, Please re-enter Chinese characters"); str = SC. nextLine (); continue ;}}// user input age verification @ SuppressWarnings ("resource") public String ageRegex (String age) {String pattern = "^ ([0-9] | [0-9] {2} | 100) $"; String str = age; sorted SC = new partition (System. in); while (true) {if (str. matches (pattern) {return str;} else {System. out. println ("enter invalid characters, enter a number again"); str = SC. nextLine (); continue ;}}// user gender verification @ SuppressWarnings ("resource") public String sexRegex (String sex) {String pattern = "[Male and Female MmFf]"; string str = sex; Transport SC = new transport (System. in); while (true) {if (str. matches (pattern) {return str;} else {System. out. println ("enter invalid characters, Please re-enter MmFf"); str = SC. nextLine (); continue ;}}// verify the phone number entered by the user @ SuppressWarnings ("resource") public String telNumRegex (String telNum) {String pattern = "[0-9] {11}"; String str = telNum; interval SC = new partition (System. in); while (true) {if (str. matches (pattern) {return str;} else {System. out. println ("enter invalid characters, Please re-enter the 11-digit mobile phone number"); str = SC. nextLine (); continue ;}}// verify the address entered by the user @ SuppressWarnings ("resource") public String addressRegex (String address) {String pattern = "[a-zA-Z [0-9] {1, 15}"; String str = address; Transport SC = new partition (System. in); while (true) {if (str. matches (pattern) {return str;} else {System. out. println ("the input is invalid. Please enter 1 to 15 uppercase/lowercase letters or numbers again"); str = SC. nextLine (); continue ;}}}}

Implementation of the Menu code:

Package com. bjsxt. iphone; public class Menu {// main interface public void mainMenu () {System. out. println ("*************************"); System. out. println ("** 1 add record **"); System. out. println ("** 2 query records **"); System. out. println ("** 3 modify record **"); System. out. println ("** 4 Delete record **"); System. out. println ("** 5 sorting records **"); System. out. println ("** 6 exit System **"); System. out. println ("*************************");} // Add public void addMenu () {System. out. println ("*************************"); System. out. println ("** 1 Add new record **"); System. out. println ("** 2 view full records **"); System. out. println ("** 3 returns the upper level **"); System. out. println ("*************************");} // search interface public void searchMenu () {System. out. println ("*************************"); System. out. println ("** 1 search by name **"); System. out. println ("** 2 search by age **"); System. out. println ("** 3 search by gender **"); System. out. println ("** 4 search by number **"); System. out. println ("** 5 search by address **"); System. out. println ("** 6 view full records **"); System. out. println ("** 7 returns the upper level **"); System. out. println ("*************************");} // modify the public void modifyMenu () {System. out. println ("*************************"); System. out. println ("** 1 view all records **"); System. out. println ("** 2 modify the specified record **"); System. out. println ("** 3 returns the upper level **"); System. out. println ("*************************");} // modify the subinterface public void subModifyMenu () {System. out. println ("*************************"); System. out. println ("** 1 Change name **"); System. out. println ("** 2 Change age **"); System. out. println ("** 3 modify gender **"); System. out. println ("** 4 modify number **"); System. out. println ("** 5 Modify address **"); System. out. println ("** 6 returns the upper level **"); System. out. println ("*************************");} // Delete the public void deleteMenu () {System. out. println ("*************************"); System. out. println ("** 1 view all records **"); System. out. println ("** 2 delete a specified record **"); System. out. println ("** 3 Delete all records **"); System. out. println ("** 4 returns the upper level **"); System. out. println ("*************************");} // sorting interface public void orderMenu () {System. out. println ("*************************"); System. out. println ("** 1 sort by name **"); System. out. println ("** 2 sort by age **"); System. out. println ("** 3 sort by gender **"); System. out. println ("** 4 view all records **"); System. out. println ("** 5 returns the upper level **"); System. out. println ("*************************");}}

Operate class implementation:

Package com. bjsxt. iphone; import java. util. arrayList; import java. util. collections; import java. util. comparator; import java. util. iterator; import java. util. list; import java. util. authorization; public class Operate {private List <Person> list = new ArrayList <Person> (); // Add information to the user. Business Logic Control: public void addLogic () {Menu m = new Menu (); TelNoteRegex reg = new TelNoteRegex (); while (true) {m. addMenu (); int key = reg. menuRegex (1, 3); switch (key) {case 1: addOperation (); break; case 2: showAll (); break; case 3: return ;}}} // public void searchLogic () {Menu m = new Menu (); TelNoteRegex reg = new TelNoteRegex (); while (true) {m. searchMenu (); int key = reg. menuRegex (1, 7); switch (key) {case 1: searchByName (); break; case 2: searchByAge (); break; case 3: searchBySex (); break; case 4: searchByTelNum (); break; case 5: sea RchByAdd (); break; case 6: showAll (); break; case 7: return ;}}// modify information business logic control public void modifyLogicLogic () {Menu m = new Menu (); TelNoteRegex reg = new TelNoteRegex (); while (true) {m. modifyMenu (); int key = reg. menuRegex (1, 3); switch (key) {case 1: showAll (); break; case 2: modifyLogicLogic1 (); break; case 3: return ;}}} // modify the public void modifyLogicLogic1 () {Menu m = new Menu (); TelNoteRegex re G = new TelNoteRegex (); while (true) {m. subModifyMenu (); int key = reg. menuRegex (1, 6); switch (key) {case 1: nameModify (); break; case 2: ageModify (); break; case 3: sexModify (); break; case 4: telNumModify (); break; case 5: addressModify (); break; case 6: return ;}}// delete information business logic control public void deleteLogic () {Menu m = new Menu (); TelNoteRegex reg = new TelNoteRegex (); while (true) {m. deleteMenu (); int key = r Eg. menuRegex (1, 4); switch (key) {case 1: showAll (); break; case 2: delete (); break; case 3: deleteAll (); break; case 4: return ;}}// business logic control of sorting information public void orderLogic () {Menu m = new Menu (); TelNoteRegex reg = new TelNoteRegex (); while (true) {m. orderMenu (); int key = reg. menuRegex (1, 5); switch (key) {case 1: orderName (); break; case 2: orderAge (); break; case 3: orderSex (); break; case 4: showAll (); Break; case 5: return ;}}// add new user information @ SuppressWarnings ("resource") public void addOperation () {TelNoteRegex reg = new TelNoteRegex (); using SC = new using (System. in); System. out. println ("Enter the contact id:"); String id = SC. nextLine (); System. out. println ("Enter the name to add:"); String name = SC. nextLine (); name = reg. nameRegex (name); System. out. println ("Enter the age to add:"); String age = SC. nextLine (); age = reg. ageRegex (age); Sy Stem. out. println ("Enter the gender to add:"); String sex = SC. nextLine (); sex = reg. sexRegex (sex); System. out. println ("Enter the phone number to add:"); String telNum = SC. nextLine (); telNum = reg. telNumRegex (telNum); System. out. println ("Enter the address to add:"); String address = SC. nextLine (); address = reg. addressRegex (address); int personId = Integer. parseInt (id); Person p = new Person (personId, name, age, sex, telNum, address); list. add (p) ;}//// determine whether the sequence number already exists Insertion successful // @ SuppressWarnings ("resource") // public String judgeId (String id) {// login SC = new Login (System. in); // Iterator <Person> it = list. iterator (); // String newId = id; // while (it. hasNext () {// Person p = (Person) it. next (); // if (newId. equals (p. getId () {// System. out. println ("this serial number already exists, Please re-enter the serial number"); // newId = SC. nextLine (); // continue; //} else {// return newId; //} // return id; // query all user information in public vo Id showAll () {System. out. println ("No. \ t" + "Name \ t" + "Age \ t" + "Gender \ t" + "mobile phone no. \ t "+" Address "); iterator <Person> it = list. iterator (); while (it. hasNext () {Person p = (Person) it. next (); System. out. println (p. getId () + "#" + "\ t" + p. getName () + "\ t" + p. getAge () + "\ t" + p. getSex () + "\ t" + p. getTelNum () + "\ t" + p. getAddress () ;}}// query user information by name @ SuppressWarnings ("resource") public void searchByName () {TelNoteRegex reg = new TelNoteRegex (); System. out. println ("Enter the name you want to query:"); then SC = new then (System. in); String name = SC. nextLine (); name = reg. nameRegex (name); Iterator <Person> it = list. iterator (); while (it. hasNext () {Person p = (Person) it. next (); if (name. equals (p. getName () {System. out. println (p. getId () + "#" + "\ t" + p. getName () + "\ t" + p. getAge () + "\ t" + p. getSex () + "\ t" + p. getTelNum () + "\ t" + p. getAddress () ;}}// query by age User Information @ SuppressWarnings ("resource") public void searchByAge () {TelNoteRegex reg = new TelNoteRegex (); System. out. println ("Enter the age you want to query:"); average SC = new average (System. in); String age = SC. nextLine (); age = reg. ageRegex (age); Iterator <Person> it = list. iterator (); while (it. hasNext () {Person p = (Person) it. next (); if (age. equals (p. getAge () {System. out. println (p. getId () + "#" + "\ t" + p. getName () + "\ t" + p. getAge () + "\ T" + p. getSex () + "\ t" + p. getTelNum () + "\ t" + p. getAddress () ;}}// query user information by gender @ SuppressWarnings ("resource") public void searchBySex () {TelNoteRegex reg = new TelNoteRegex (); System. out. println ("Enter the gender you want to query:"); then SC = new then (System. in); String sex = SC. nextLine (); sex = reg. sexRegex (sex); Iterator <Person> it = list. iterator (); while (it. hasNext () {Person p = (Person) it. next (); if (sex. equals (p. getS Ex () {System. out. println (p. getId () + "#" + "\ t" + p. getName () + "\ t" + p. getAge () + "\ t" + p. getSex () + "\ t" + p. getTelNum () + "\ t" + p. getAddress () ;}}// query user information by phone number @ SuppressWarnings ("resource") public void searchByTelNum () {TelNoteRegex reg = new TelNoteRegex (); System. out. println ("Enter the phone number you want to query:"); telephone SC = new telephone (System. in); String telNum = SC. nextLine (); telNum = reg. telNumRegex (telNum); Iterator <P Erson> it = list. iterator (); while (it. hasNext () {Person p = (Person) it. next (); if (telNum. equals (p. getTelNum () {System. out. println (p. getId () + "#" + "\ t" + p. getName () + "\ t" + p. getAge () + "\ t" + p. getSex () + "\ t" + p. getTelNum () + "\ t" + p. getAddress () ;}}// query user information by address @ SuppressWarnings ("resource") public void searchByAdd () {TelNoteRegex reg = new TelNoteRegex (); System. out. println ("Enter the address you want to query:"); required SC = New partition (System. in); String address = SC. nextLine (); address = reg. addressRegex (address); Iterator <Person> it = list. iterator (); while (it. hasNext () {Person p = (Person) it. next (); if (address. equals (p. getAddress () {System. out. println (p. getId () + "#" + "\ t" + p. getName () + "\ t" + p. getAge () + "\ t" + p. getSex () + "\ t" + p. getTelNum () + "\ t" + p. getAddress () ;}}//// modify the specified record information // public void modify () {// System. out. Println ("enter the information you want to modify:"); //} // modify the name @ SuppressWarnings ("resource") public void nameModify () {TelNoteRegex reg = new TelNoteRegex (); System. out. println ("Enter the sequence number you want to modify:"); then SC = new then (System. in); String num = SC. nextLine (); int id = Integer. parseInt (num); System. out. println ("Enter the name you want to modify:"); String name = SC. nextLine (); name = reg. nameRegex (name); Iterator <Person> it = list. iterator (); while (it. hasNext () {Person p = (Person) it. next (); if (id = p. getId () {p. setName (name); System. out. println (p. getId () + "#" + "\ t" + p. getName () + "\ t" + p. getAge () + "\ t" + p. getSex () + "\ t" + p. getTelNum () + "\ t" + p. getAddress ();} else {System. out. println ("modification failed. Please try again! ") ;}}// Modify the age information @ SuppressWarnings (" resource ") public void ageModify () {TelNoteRegex reg = new TelNoteRegex (); System. out. println ("Enter the sequence number you want to modify:"); then SC = new then (System. in); String num = SC. nextLine (); int id = Integer. parseInt (num); System. out. println ("Enter the age you want to modify:"); String age = SC. nextLine (); age = reg. ageRegex (age); Iterator <Person> it = list. iterator (); while (it. hasNext () {Person p = (Person) it. ne Xt (); if (id = p. getId () {p. setAge (age); System. out. println (p. getId () + "#" + "\ t" + p. getName () + "\ t" + p. getAge () + "\ t" + p. getSex () + "\ t" + p. getTelNum () + "\ t" + p. getAddress ();} else {System. out. println ("modification failed. Please try again! ") ;}}// Modify gender information @ SuppressWarnings (" resource ") public void sexModify () {TelNoteRegex reg = new TelNoteRegex (); System. out. println ("Enter the sequence number you want to modify:"); then SC = new then (System. in); String num = SC. nextLine (); int id = Integer. parseInt (num); System. out. println ("Enter the gender you want to modify:"); String sex = SC. nextLine (); sex = reg. sexRegex (sex); Iterator <Person> it = list. iterator (); while (it. hasNext () {Person p = (Person) it. ne Xt (); if (id = p. getId () {p. setSex (sex); System. out. println (p. getId () + "#" + "\ t" + p. getName () + "\ t" + p. getAge () + "\ t" + p. getSex () + "\ t" + p. getTelNum () + "\ t" + p. getAddress ();} else {System. out. println ("modification failed. Please try again! ") ;}}// Modify the number @ SuppressWarnings (" resource ") public void telNumModify () {System. out. println ("Enter the sequence number you want to modify:"); then SC = new then (System. in); String num = SC. nextLine (); int id = Integer. parseInt (num); System. out. println ("Enter the phone number you want to modify:"); String telNum = SC. nextLine (); Iterator <Person> it = list. iterator (); while (it. hasNext () {Person p = (Person) it. next (); if (id = p. getId () {p. setTelNum (telNum); System. Out. println (p. getId () + "#" + "\ t" + p. getName () + "\ t" + p. getAge () + "\ t" + p. getSex () + "\ t" + p. getTelNum () + "\ t" + p. getAddress ();} else {System. out. println ("modification failed. Please try again! ") ;}}// Modify the address information @ SuppressWarnings (" resource ") public void addressModify () {TelNoteRegex reg = new TelNoteRegex (); System. out. println ("Enter the sequence number you want to modify:"); then SC = new then (System. in); String num = SC. nextLine (); int id = Integer. parseInt (num); System. out. println ("Enter the address you want to modify:"); String address = SC. nextLine (); address = reg. addressRegex (address); Iterator <Person> it = list. iterator (); while (it. hasNext () {Pe Rson p = (Person) it. next (); if (id = p. getId () {p. setAddress (address); System. out. println (p. getId () + "#" + "\ t" + p. getName () + "\ t" + p. getAge () + "\ t" + p. getSex () + "\ t" + p. getTelNum () + "\ t" + p. getAddress ();} else {System. out. println ("modification failed. Please try again! ") ;}}// Delete specified user information @ SuppressWarnings (" resource ") public void delete () {System. out. println ("Enter the id you want to delete:"); then SC = new then (System. in); String num = SC. nextLine (); int id = Integer. parseInt (num); Iterator <Person> it = list. iterator (); while (it. hasNext () {Person p = (Person) it. next (); if (id = p. getId () {list. remove (id); System. out. println ("deleted successfully");} else {System. out. println ("failed to delete") ;}}// delete all user information public void deleteAll () {list. clear (); System. out. println ("deleted successfully");} // public void orderName () {Comparator <Person> comparator = new Comparator <Person> () {@ Override public int compare (Person o1, Person o2) {return o1.getName (). compareTo (o2.getName () ;}}; Collections. sort (list, comparator); showAll () ;}// public void orderAge () {Comparator <Person> comparator = new Comparator <Person> () {@ Override public int compare (Person o1, Person o2) {return o1.getAge (). compareTo (o2.getAge () ;}}; Collections. sort (list, comparator); showAll () ;}// public void orderSex () {Comparator <Person> comparator = new Comparator <Person> () {@ Override public int compare (Person o1, Person o2) {return o1.getSex (). compareTo (o2.getSex () ;}}; Collections. sort (list, comparator); showAll ();}}

Summary:This project makes me clearly understand what object orientation is, Why java is an object-oriented programming language, and program code is called by objects, I also understand how a project should be clearly written by division of labor, and the functions and views should be separated. Each class has its own specific functions, and the functions of each class should not be detached from each other, this type of code is basically not highly reusable. If the code is clearly separated, you only need to add features when you add functional modules later, without modifying the previous Code. This reflects the open and closed principles, for function development and modification content closure, this project also reflects the MVC design mode. For me, this project has benefited me a lot.

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.