Java基础—通讯录管理系统
题目:
- 完成一个通讯录,需求:
(1)添加联系人(联系人:编号,姓名,手机号,QQ,邮箱地址)添加时需要检查手机号和邮箱地址格式是否正确,若不正确,不允许添加
(2)联系人查询(输入姓名或电话查询)
(3)显示联系人列表
(4)根据编号删除指定编号的联系人
代码分析:
之前写过类似的管理系统,不过是使用数组进行数据存储,这次的通讯录管理系统通过动态数组
ArrayList进行数据存储。其中代码实现的原理和之前所写相似。在此不再赘述。
判断手机号邮箱地址格式是否格式正确使用了正则表达式进行判断,如果输入错误则输出提示语句,并重新输入正确格式,递归实现。
其中修改手机号的方法和删除用户类似,顺带写了一下,没有进行实现,感兴趣的朋友可以自己进行实现测试一下。
代码实现:
用户类:
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;}@Overridepublic String toString() {return "User{" +"no=" + no +", name='" + name + '\'' +", phone='" + phone + '\'' +", QQ='" + QQ + '\'' +", email='" + email + '\'' +'}';}
}
用户管理类:
public class UserMange {static ArrayList 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);}
}
测试类:
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();}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
