Java implementation of simple address book management system

Time:2021-12-2

This example shares the specific code of Java address book management system for your reference. The specific contents are as follows

Title:

1. Complete an address book. Requirements:

(1) When adding a contact (contact: number, name, mobile phone number, QQ, email address), check whether the mobile phone number and email address format are correct. If not, it is not allowed to add

(2) Contact inquiry (enter name or telephone inquiry)

(3) Show contact list

(4) Delete the contact with the specified number according to the number

Code analysis:

A similar management system has been written before, but it uses array for data storage. This time, the address book management system uses dynamic array

ArrayList for data storage. The principle of code implementation is similar to that written before. It will not be repeated here.

Judge whether the format of mobile phone number and e-mail address is correct. Use regular expression to judge. If input error, output prompt statement, re-enter the correct format and implement recursively.

The method of modifying the mobile phone number is similar to deleting users. I wrote it in passing, but it was not implemented. Interested friends can test it by themselves.

Code implementation:

User class:


package com.softeem.j2106.work;
 
/**
 * @author admin
 * 2021/7/26
 */
public class User {
    private int no;
    private String name;
    private String phone;
    private String QQ;
    private String email;
 
    public User() {
    }
 
    public User(int no, String name, String phone, String QQ, String email) {
        this.no = no;
        this.name = name;
        this.phone = phone;
        this.QQ = QQ;
        this.email = email;
    }
 
    public int getNo() {
        return no;
    }
 
    public void setNo(int no) {
        this.no = no;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getPhone() {
        return phone;
    }
 
    public void setPhone(String phone) {
        this.phone = phone;
    }
 
    public String getQQ() {
        return QQ;
    }
 
    public void setQQ(String QQ) {
        this.QQ = QQ;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    @Override
    public String toString() {
        return "User{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                ", QQ='" + QQ + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

User management class:

public class UserMange {
    static ArrayList<User> s = new ArrayList<>();
 
    public boolean addUser(User user){
        return s.add(user);
    }
 
    public ArrayList showInfo(){
        return s;
    }
    public User searchByName(String name){
        for (User user : s) {
            if (Objects.equals(name,user.getName()) ||Objects.equals(name,user.getPhone())){
                return user;
            }
        }
        return null;
    }
 
    public boolean updatePhone(int no,String phone){
        User user = null;
        for(User u:s) {
            if(no == u.getNo()) {
                u.setPhone(phone);
                break;
            }
        }
        if(user == null) {
            System.out.println ("the user does not exist");
            return false;
        }
        System. Out. Println ("modification succeeded!");
        return true;
    }
 
    public boolean delUser(int no){
        User user = null;
        for(User u:s) {
            if(no == u.getNo()) {
                user = u;
                break;
            }
        }
        if(user == null) {
            System.out.println ("the user does not exist");
            return false;
        }
        return s.remove(user);
    }
}

Test class:

public class Test2 {
 
    static UserMange user = new UserMange();
    static Scanner sc = new Scanner(System.in);
 
    public static void start(){
        System. Out. Println ("======== Softeem address book management system ======");
        System.out.println ("[1] add contact");
        System. Out. Println ("[2] contact query");
        System.out.println ("[3] display contact list");
        System. Out. Println ("[4] delete the contact with the specified number according to the number");
        System.out.println("=============================");
        int i = sc.nextInt();
        switch (i){
            case 1:
                add();
                start();
                break;
            case 2:
                System. Out. Println ("[1] query by contact name / [2] query by contact phone");
                int a = sc.nextInt();
                findbyName(a);
                start();
                break;
            case 3:
                show();
                start();
                break;
            case 4:
                del();
                start();
                break;
            case 0:
                System. Out. Println ("thank you for using, goodbye!");
                System.exit(0);
                break;
            default:
                System. Out. Println ("please enter the correct instruction!");
                start();
                break;
        }
    }
    public static void add(){
        System.out.println ("please enter contact number:");
        int a = sc.nextInt();
        System.out.println ("please enter contact name:");
        String b = sc.next();
        System.out.println ("please enter the contact mobile phone number:");
        String c = sc.next();
        judgePhone(c);
        System. Out. Println ("please enter the contact QQ:");
        String  d = sc.next();
        System.out.println ("please enter the email address of the contact:");
        String e = sc.next();
        judgeEmail(e);
        User x = new User(a,b,c,d,e);
        if(user.addUser(x)){
            System. Out. Println ("added successfully!");
        }
    }
 
    public static void judgePhone(String phone){
 
        if (phone.matches("1[34589][0-9]{9}")){
 
        }else {
            System.out.println ("the mobile phone number is entered incorrectly, please re-enter it");
            String  v = sc.next();
            judgePhone(v);
        }
    }
 
    public static void judgeEmail(String email){
 
        if (email.matches("[A-Za-z0-9][email protected][a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)")){
 
        }else {
            System.out.println ("the mailbox format is entered incorrectly, please re-enter");
            String v = sc.next();
            judgeEmail(v);
        }
    }
 
    public static void findbyName(int a){
        if (a==1){
            System.out.println ("please enter the contact name");
        }else {
            System.out.println ("please enter the contact phone");
        }
        String name = sc.next();
        User user = Test2.user.searchByName(name);
        System.out.println(user);
    }
 
    public static void  show(){
        ArrayList list = user.showInfo();
        for (Object o : list) {
            System.out.println(o);
        }
    }
 
    public static void del(){
        System.out.println ("please enter the number");
        int no = sc.nextInt();
        if(user.delUser(no)){
            System.out.println ("deletion succeeded");
        }
    }
 
    public static void main(String[] args) {
        start();
    }
}

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.