The main menu controls the submenu at each level, and realizes the implementation of adding records, finding records, deleting records, modifying records, sorting records, and exiting the system functions. A total of six functional modules.
The above diagram shows the functions of each module, and it also uses regular expressions to determine whether the input complies with the rules. This system reflects the design pattern of MVC, and the division of labor is very clear.
It mainly includes the following categories:
APP class: the main function class of the program, the entry to the program start
Menu class: It is all the effects displayed on simple pages, and there is no function implementation.
TelNoteRegex class: Regular expression, which determines whether the input is qualified, is all in this method.
Operate class: Business logic class, all functions are basically concentrated in this class.
Person class: encapsulate data.
Specific implementation code:
The 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; } } }}The specific implementation code of 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; }}Specific code of TelNoteRegex class:
package com.bjsxt.iphone;import java.util.Scanner;public class TelNoteRegex { //Check for menu items @SuppressWarnings("resource") public int menuRegex(int min,int max){ String regex="[0-9]{1}"; Scanner sc=new Scanner(System.in); while(true){ String input=sc.nextLine(); //Check based on legality if(input.matches(regex)){ int key=Integer.parseInt(input); if(key>=min && key<=max){ return key; }else{ System.out.println("The entered menu item does not meet the requirements!"); } }else{ System.out.println("The input content is incorrect!"); } } } } //Verification of user input name @SuppressWarnings("resource") public String nameRegex(String name){ String pattern ="[//u4e00-//u9fa5]+"; String str=name; Scanner sc=new Scanner(System.in); while(true){ if(str.matches(pattern)){ return str; }else{ System.out.println("Enter illegal characters, please re-enter Chinese characters"); str=sc.nextLine(); continue; } } } //Verification of user input age @SuppressWarnings("resource") public String ageRegex(String age){ String pattern ="^([0-9]|[0-9]{2}|100)$"; String str=age; Scanner sc=new Scanner(System.in); while(true){ if(str.matches(pattern)){ return str; }else{ System.out.println("Enter illegal characters, please re-enter the numbers"); str=sc.nextLine(); continue; } } } //Verification of user gender @SuppressWarnings("resource") public String sexRegex(String sex){ String pattern ="[Men and Female MmFf]"; String str=sex; Scanner sc=new Scanner(System.in); while(true){ if(str.matches(pattern)){ return str; }else{ System.out.println("Enter illegal characters, please re-enter male and female MmFf"); str=sc.nextLine(); continue; } } } //Verification of user input phone number @SuppressWarnings("resource") public String telNumRegex(String telNum){ String pattern ="[0-9]{11}"; String str=telNum; Scanner sc=new Scanner(System.in); while(true){ if(str.matches(pattern)){ return str; }else{ System.out.println("Input illegal characters, please re-enter the 11-digit mobile phone number"); str=sc.nextLine(); continue; } } } } //Verification of user input address @SuppressWarnings("resource") public String addressRegex(String address){ String pattern ="[a-zA-Z[0-9]]{1,15}"; String str=address; Scanner sc=new Scanner(System.in); while(true){ if(str.matches(pattern)){ return str; }else{ System.out.println("Input illegal characters, please re-enter 1 to 15 English cases or numbers"); str=sc.nextLine(); continue; } } }}Specific implementation of Menu class 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 Find record**"); System.out.println("** 3 Modify record**"); System.out.println("** 4 Delete record**"); System.out.println("** 5 Sort records**"); System.out.println("** 6 Exit the system**"); System.out.println("***************************"); } //Add interface public void addMenu () { System.out.println("*********************"); System.out.println("** 1 Add new record**"); System.out.println("** 2 View all records**"); System.out.println("** 3 Return to previous 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. Click on the address to find **"); System.out.println("** 6. View full records**"); System.out.println("** 7. Return to the previous level**"); System.out.println("************************"); } //Modify the interface public void modifyMenu () { System.out.println("************************"); System.out.println("** 1. View full records**"); System.out.println("** 2 Modify the specified record**"); System.out.println("** 3 Return to the previous level**"); System.out.println("************************"); } //Modify the subinterface public void subModifyMenu () { System.out.println("************************"); System.out.println("** 1 Modify the name**"); System.out.println("** 2 Modify the age**"); System.out.println("** 3 Modify gender**"); System.out.println("** 4 Modify number**"); System.out.println("** 5 Modify address**"); System.out.println("** 6 Return to previous level**"); System.out.println("************************"); } //Delete interface public void deleteMenu () { System.out.println("******************************"); System.out.println("** 1 View all records**"); System.out.println("** 2 Delete the specified records**"); System.out.println("** 3 Delete all records**"); System.out.println("** 4 Return to the previous 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 Return to previous level**"); System.out.println("************************"); }}Specific implementation of Operate class:
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.Scanner;public class Operate{ private List<Person> list=new ArrayList<Person>(); //User Add Information 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; } } } //User query information business logic control 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: searchByAdd(); 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 three-layer jumps public void modifyLogicLogic1(){ Menu m=new Menu(); TelNoteRegex reg=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=reg.menuRegex(1, 4); switch(key){ case 1: showAll(); break; case 2: delete(); break; case 3: deleteAll(); break; case 4: return; } } } //Sorting information business logic control 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(); Scanner sc=new Scanner(System.in); System.out.println("Please enter the contact id:"); String id=sc.nextLine(); System.out.println("Please enter the name to be added:"); String name=sc.nextLine(); name=reg.nameRegex(name); System.out.println("Please enter the age to be added:"); String age=sc.nextLine(); age=reg.ageRegex(age); System.out.println("Please enter the gender you want to add:"); String sex=sc.nextLine(); sex=reg.sexRegex(sex); System.out.println("Please enter the mobile number you want to add:"); String telNum=sc.nextLine(); telNum=reg.telNumRegex(telNum); System.out.println("Please enter the address you want 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); }// // //Judge whether the serial number has already existed and the insertion failed successfully// @SuppressWarnings("resource")// public String judgeId(String id){// Scanner sc=new Scanner(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 public void showAll(){ System.out.println("serial number/t/t"+"name/t/t"+"age/t/t"+"gender/t/t"+"mobile number/t/t"+"address"); Iterator<Person> it=list.iterator(); while(it.hasNext()){ Person p=(Person)it.next(); System.out.println(p.getId()+"#"+"/t/t"+p.getName()+"/t/t"+p.getAge()+"/t/t"+p.getSex()+"/t/t"+p.getTelNum()+"/t/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:"); Scanner sc=new Scanner(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/t"+p.getName()+"/t/t"+p.getAge()+"/t/t"+p.getSex()+"/t/t"+p.getTelNum()+"/t/t"+p.getAddress()); } } } //Query user information by age @SuppressWarnings("resource") public void searchByAge(){ TelNoteRegex reg=new TelNoteRegex(); System.out.println("Enter the age you want to query:"); Scanner sc=new Scanner(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/t"+p.getName()+"/t/t"+p.getAge()+"/t/t"+p.getSex()+"/t/t"+p.getTelNum()+"/t/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:"); Scanner sc=new Scanner(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.getSex())){ System.out.println(p.getId()+"#"+"/t/t"+p.getName()+"/t/t"+p.getAge()+"/t/t"+p.getSex()+"/t/t"+p.getTelNum()+"/t/t"+p.getAddress()); } } } //Check 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:"); Scanner sc=new Scanner(System.in); String telNum=sc.nextLine(); telNum=reg.telNumRegex(telNum); Iterator<Person> it=list.iterator(); while(it.hasNext()){ Person p=(Person)it.next(); if(telNum.equals(p.getTelNum())){ System.out.println(p.getId()+"#"+"/t/t"+p.getName()+"/t/t"+p.getAge()+"/t/t"+p.getSex()+"/t/t"+p.getTelNum()+"/t/t"+p.getAddress()); } } } //Check user information by address @SuppressWarnings("resource") public void searchByAdd(){ TelNoteRegex reg=new TelNoteRegex(); System.out.println("Enter the address you want to query:"); Scanner sc=new Scanner(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/t"+p.getName()+"/t/t"+p.getAge()+"/t/t"+p.getSex()+"/t/t"+p.getTelNum()+"/t/t"+p.getAddress()); } } }// // Modify the specified record information// public void modify(){// System.out.println("Please enter the information you want to modify:");// // } // Modify the name information @SuppressWarnings("resource") public void nameModify(){ TelNoteRegex reg=new TelNoteRegex(); System.out.println("Please enter the serial number you want to modify:"); Scanner sc=new Scanner(System.in); String num=sc.nextLine(); int id=Integer.parseInt(num); System.out.println("Please 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/t"+p.getName()+"/t/t"+p.getAge()+"/t/t"+p.getSex()+"/t/t"+p.getTelNum()+"/t/t"+p.getAddress()); }else{ System.out.println("Modification failed, please try again!"); } } } //Modify age information @SuppressWarnings("resource") public void ageModify(){ TelNoteRegex reg=new TelNoteRegex(); System.out.println("Please enter the serial number you want to modify:"); Scanner sc=new Scanner(System.in); String num=sc.nextLine(); int id=Integer.parseInt(num); System.out.println("Please 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.next(); if(id==p.getId()){ p.setAge(age); System.out.println(p.getId()+"#"+"/t/t"+p.getName()+"/t/t"+p.getAge()+"/t/t"+p.getSex()+"/t/t"+p.getTelNum()+"/t/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("Please enter the serial number you want to modify:"); Scanner sc=new Scanner(System.in); String num=sc.nextLine(); int id=Integer.parseInt(num); System.out.println("Please 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.next(); if(id==p.getId()){ p.setSex(sex); System.out.println(p.getId()+"#"+"/t/t"+p.getName()+"/t/t"+p.getAge()+"/t/t"+p.getSex()+"/t/t"+p.getTelNum()+"/t/t"+p.getAddress()); }else{ System.out.println("Modification failed, please try again!"); } } } //Modify number information @SuppressWarnings("resource") public void telNumModify(){ System.out.println("Please enter the serial number you want to modify:"); Scanner sc=new Scanner(System.in); String num=sc.nextLine(); int id=Integer.parseInt(num); System.out.println("Please 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/t"+p.getName()+"/t/t"+p.getAge()+"/t/t"+p.getSex()+"/t/t"+p.getTelNum()+"/t/t"+p.getAddress()); }else{ System.out.println("Modification failed, please try again!"); } } } //Modify address information @SuppressWarnings("resource") public void addressModify(){ TelNoteRegex reg=new TelNoteRegex(); System.out.println("Please enter the serial number you want to modify:"); Scanner sc=new Scanner(System.in); String num=sc.nextLine(); int id=Integer.parseInt(num); System.out.println("Please enter the address you want to modify:"); String address=sc.nextLine(); address=reg.addressRegex(address); Iterator<Person> it=list.iterator(); while(it.hasNext()){ Person p=(Person)it.next(); if(id==p.getId()){ p.setAddress(address); System.out.println(p.getId()+"#"+"/t/t"+p.getName()+"/t/t"+p.getAge()+"/t/t"+p.getSex()+"/t/t"+p.getTelNum()+"/t/t"+p.getAddress()); }else{ System.out.println("Modification failed, please try again!"); } } } //Delete the specified user information @SuppressWarnings("resource") public void delete(){ System.out.println("Please enter the id you want to delete:"); Scanner sc=new Scanner(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("Delete successful"); }else{ System.out.println("Delete failed"); } } } //Delete all user information public void deleteAll(){ list.clear(); System.out.println("Delete successfully"); } //Sort information by user name 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(); } //Sort information by user age 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(); } //Sort information by user gender 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 made me very clear about what object-oriented is and why Java is an object-oriented programming language. The code of the program is called by objects, and I also understood how a project should be written clearly. The functions and views should be separated. Each class has its own specific functions. The functions of each class should not be uninstalled together. Such codes are basically not reusable. If they are separated and clear, when adding functional modules in the future, only functions need to be added without modifying the previous code. This well reflects the principle of opening and closing. For function development and modification content, this project also reflects the design pattern of MVC. For me, this project has benefited me a lot.