one , preface

    We have learned so much , It's a sad thing that optical knowledge can't be used , So we should finish it

Practice some items or questions in part , To consolidate our knowledge , Stimulate our motivation for continuous learning .

two , Function of creating communication

*
Add contact information

*
Delete contact information

*
Modify contact information

*
View all contact information

*
Exit address book management system

First define a People class , Provide corresponding get,set method

( stay idea Shortcut keys in Alt+insert generate get,set method , And construction method )
public class People { private String sid; private String name; private String
age; private String address; private String num; public People(){} public
People(String sid, String name, String age, String address, String num) {
this.sid = sid; this.name = name; this.age = age; this.address = address;
this.num=num; } public String getSid() { return sid; } public void
setSid(String sid) { this.sid = sid; } 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
getAddress() { return address; } public void setAddress(String address) {
this.address = address; } public String getNum() { return num; } public void
setNum(String num) { this.num = num; }
stay People Add menu to class , And call it in the test class .
public void menu (){ System.out.println("------- Welcome to the address book management system -------");
System.out.println("1. Add a contact "); System.out.println("2. Delete Contact ");
System.out.println("3. Modify contact "); System.out.println("4. View all contacts ");
System.out.println("5. sign out "); System.out.println(" Please select :"); }
In order to get the information we want directly through the printed object , Instead of getting
View all entered information , No rewriting toString Time :[controller.Student@6e0be858]
Rewritten toString The method can be understood . You can also pass Alt+Inset Shortcut keys to quickly generate toString method
@Override public String toString(){// rewrite toString method , Make it a designated message that we can understand return
"[ full name :"+name+", Age :"+age+", Telephone :"+num+", relationship :"+sid+", address :"+address+"]\n"; } }
All the above are in People In class .

We are PeopleManner Under class , Write the following .

main Method :
public static void main(String[] args) { ArrayList<People> array = new
ArrayList(); while (true) { People s = new People();// create object , Call method . s. menu ();
Scanner sc1 = new Scanner(System.in); int d = sc1.nextInt(); switch (d) { case
1: { addPeople(array); System.out.println(" Contact added successfully "); break; } case 2:{
deletePeople(array); break; } case 3:{ revisePeople(array); break; } case 4:{
checkPeople(array); break; } case 5:{ System.out.println(" Exit successful , Thank you for using ");
System.exit(0); break; } default:{ System.out.println(" Input error , Please re-enter "); } } }
How to add contacts in addPeople in
public static void addPeople(ArrayList<People> array){ Scanner sc=new
Scanner(System.in); System.out.println(" Please enter your name "); String name=sc.nextLine();
System.out.println(" Please enter the phone number "); String num=sc.nextLine();
System.out.println(" Please enter your relationship "); String sid=sc.nextLine();
System.out.println(" Please enter age "); String age=sc.nextLine();
System.out.println(" Please enter your home address "); String address=sc.nextLine(); People s1=new
People();// create object , call set member , Set values for members s1.setAddress(address); s1.setAge(age);
s1.setName(name); s1.setSid(sid); s1.setNum(num); array.add(s1);// Add all set variables to the collection
}
How to view all contacts in checkPeople in
public static void checkPeople(ArrayList<People> array){
if(array.isEmpty()){//isEmpty It is used to judge whether it is not empty collection Interface method ,ArrayList Realized
list Interface ,list Interface inherits collection Interface System.out.println(" No contact information , Please add a contact before viewing "); } else{
System.out.println(array);}// View all entered information , No rewriting toString Time :
//[controller.Student@6e0be858] }
In the method of deleting contact information
public static void deletePeople(ArrayList<People> array){
if(array.isEmpty()){ System.out.println(" No contact information , Please add contact information before operation "); } else{
Scanner sc=new Scanner(System.in); System.out.println(" Please enter the name of the contact you want to delete "); String
num=sc.nextLine(); int sum=0; for(int i=0;i<array.size();i++) { People stu =
array.get(i); // System.out.println(stu);//stu Includes all letters of an element contact (name,age...)
if(stu.getName().equals(num)) { array.remove(i);sum++;// Define a variable to determine whether it contains the element
System.out.println(" Deleted successfully "); } } if(sum==0){
System.out.println(" Delete failed , The contact information does not exist "); } } }
In defining the method of modifying contact information
public static void revisePeople(ArrayList<People> array) { if
(array.isEmpty()) { System.out.println(" No contact information ! Please operate after adding "); } else { Scanner
sc1 = new Scanner(System.in); System.out.println(" Please enter the name of the contact you want to modify "); String name1
= sc1.nextLine(); int sum = 0; for (int j = 0; j < array.size(); j++) { People
s2 = array.get(j); if (s2.getName().equals(name1)) { sum++; } } if (sum == 0) {
System.out.println(" No matching names , Re-enter "); }else{ Scanner sc = new Scanner(System.in);
System.out.println(" Please enter a new name "); String name = sc.nextLine();
System.out.println(" Please enter a new phone code "); String num = sc.nextLine();
System.out.println(" Please enter your relationship "); String sid = sc.nextLine();
System.out.println(" Please enter a new age "); String age = sc.nextLine();
System.out.println(" Please enter a new address "); String address = sc.nextLine(); People s1 = new
People(); s1.setAddress(address); s1.setAge(age); s1.setName(name);
s1.setSid(sid); s1.setNum(num); //
System.out.println(s1);//s1 It is also an element that contains all the information of the contact , So follow-up use array.set(i,s1) Equivalent exchange for (int i
= 0; i < array.size(); i++) { People s = array.get(i); if
(s.getName().equals(name1)) { array.set(i, s1);// Modify him
System.out.println(" Modified successfully "); break; } } } } }
Overview of final effect :

 

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage MySQL Basics Commonly used sentence ( Add / delete / modify query )C Language of a Gobang game implementation QT5.9 Use of learning notes QSqlQuery Method of Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control java Polymorphic array of web Front end signature plug-in _signature_pad Plug in implements electronic signature function centos7 install RabbitMqspringboot use redis Experiment 4 Automated test tools - software test