Java实现简单通讯录管理系统

时间:2022-11-28 14:02:43

本文实例为大家分享了Java实现通讯录管理系统的具体代码,供大家参考,具体内容如下

题目:

1、完成一个通讯录,需求:

(1)添加联系人(联系人:编号,姓名,手机号,QQ,邮箱地址)添加时需要检查手机号和邮箱地址格式是否正确,若不正确,不允许添加

(2)联系人查询(输入姓名或电话查询)

(3)显示联系人列表

(4)根据编号删除指定编号的联系人

代码分析:

之前写过类似的管理系统,不过是使用数组进行数据存储,这次的通讯录管理系统通过动态数组

ArrayList进行数据存储。其中代码实现的原理和之前所写相似。在此不再赘述。

判断手机号邮箱地址格式是否格式正确使用了正则表达式进行判断,如果输入错误则输出提示语句,并重新输入正确格式,递归实现。

其中修改手机号的方法和删除用户类似,顺带写了一下,没有进行实现,感兴趣的朋友可以自己进行实现测试一下。

代码实现:

用户类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 + '\'' +
                '}';
    }
}

用户管理类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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("该用户不存在");
            return false;
        }
        System.out.println("修改成功!");
        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("该用户不存在");
            return false;
        }
        return s.remove(user);
    }
}

测试类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
public class Test2 {
 
    static UserMange user = new UserMange();
    static Scanner sc = new Scanner(System.in);
 
    public static void start(){
        System.out.println("=======SOFTEEM通讯录管理系统=====");
        System.out.println("【1】添加联系人");
        System.out.println("【2】联系人查询");
        System.out.println("【3】显示联系人列表");
        System.out.println("【4】根据编号删除指定编号的联系人");
        System.out.println("=============================");
        int i = sc.nextInt();
        switch (i){
            case 1:
                add();
                start();
                break;
            case 2:
                System.out.println("【1】通过联系人姓名查询/【2】通过联系人电话查询");
                int a = sc.nextInt();
                findbyName(a);
                start();
                break;
            case 3:
                show();
                start();
                break;
            case 4:
                del();
                start();
                break;
            case 0:
                System.out.println("谢谢使用,再见!");
                System.exit(0);
                break;
            default:
                System.out.println("请输入正确的指令!");
                start();
                break;
        }
    }
    public static void add(){
        System.out.println("请输入联系人编号:");
        int a = sc.nextInt();
        System.out.println("请输入联系人姓名:");
        String b = sc.next();
        System.out.println("请输入联系人手机号:");
        String c = sc.next();
        judgePhone(c);
        System.out.println("请输入联系人QQ:");
        String  d = sc.next();
        System.out.println("请输入联系人邮箱地址:");
        String e = sc.next();
        judgeEmail(e);
        User x = new User(a,b,c,d,e);
        if(user.addUser(x)){
            System.out.println("添加成功!");
        }
    }
 
    public static void judgePhone(String phone){
 
        if (phone.matches("1[34589][0-9]{9}")){
 
        }else {
            System.out.println("手机号输入有误,请重新输入");
            String  v = sc.next();
            judgePhone(v);
        }
    }
 
    public static void judgeEmail(String email){
 
        if (email.matches("[A-Za-z0-9]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)")){
 
        }else {
            System.out.println("邮箱格式输入有误,请重新输入");
            String v = sc.next();
            judgeEmail(v);
        }
    }
 
    public static void findbyName(int a){
        if (a==1){
            System.out.println("请输入联系人姓名");
        }else {
            System.out.println("请输入联系人电话");
        }
        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("请输入编号");
        int no = sc.nextInt();
        if(user.delUser(no)){
            System.out.println("删除成功");
        }
    }
 
    public static void main(String[] args) {
        start();
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_44873394/article/details/119152704