实现通讯录增删改查的功能
1.创建通讯录类,输入所需要的基本属性
package com.softeem.work1;
public class TongXunLu {
private int id;
private String name;
private String phone;
private int qq;
private String email;
//无参构造器 有参构造器 setter,getter方法 toString方法 省略
}
2.创建方法类,写出需要的方法
package com.softeem.work1;
import java.util.ArrayList;
import java.util.Scanner;
public class TestTXL {
ArrayList<TongXunLu> list = new ArrayList<TongXunLu>();
Scanner sc = new Scanner(System.in);
//添加用户
public void addLXR() {
while (true) {
System.out.print("输入1添加用户,输入其他返回");
int ab = sc.nextInt();
if (ab == 1) {
System.out.print("输入手机号");
String phone = sc.next();
//通过正则表达式来判断是否是正确的手机号,手机号格式不正确则结束这个程序
if (phone.matches("^1\\d{10}$")) {
System.out.print("输入邮箱");
String email = sc.next();
//判断邮箱格式
if (email.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*")) {
System.out.print("输入编号");
int id = sc.nextInt();
System.out.print("输入姓名");
String name = sc.next();
System.out.print("输入QQ");
int qq = sc.nextInt();
list.add(new TongXunLu(id, name, phone, qq, email));
System.out.println("添加成功");
} else {
System.out.println("邮箱地址不正确");
break;
}
} else {
System.out.println("手机号格式不正确");
break;
}
} else {
break;
}
}
}
//查找
public int chaxun() {
System.out.println("输入姓名或手机号查找");
String s = sc.next();
//通过正则表达式来通过手机号查询,如果不是手机号的格式则都按名字来查询
if (s.matches("^1\\d{10}$")) {
for (int i = 0; i < list.size(); i++) {
//遍历后查找相同电话号码的人
if (list.get(i).getPhone().equals(s)) {
//找到后返回这个人的全部信息
return i;
}
}
} else {
for (int i = 0; i < list.size(); i++) {
//遍历后查找相同名字的人
if (list.get(i).getName().equals(s)) {
//找到后返回这个人的全部信息
return i;
}
}
}
return -1;
}
//删除
public void shanchu() {
System.out.println("输入你需要删除的信息的编号(按填写的编号删除)");
int y = sc.nextInt();
for (int i = 0; i < list.size(); i++) {
//遍历寻找相同id的人
if (list.get(i).getId() == y) {
//找到后删除这个人的所有信息
list.remove(i);
System.out.println("删除成功");
return;
}
}
System.out.println("查无此人");
}
//输出所有
public void chaXunALL() {
for (TongXunLu TXL : list) {
System.out.println(TXL);
}
}
public void wasd() {
while (true) {
System.out.println("输入1添加,输入2查询联系人,输入3删除,输入4查询所有输入其他结束");
int x = sc.nextInt();
switch (x) {
case 1:
addLXR();
System.out.println("========================");
break;
case 2:
int i = chaxun();
//运行后查找信息,找到后输出他的信息,找不到则输出"查无此人"
if (i != -1) {
System.out.println(list.get(i));
} else {
System.out.println("查无此人");
}
System.out.println("========================");
break;
case 3:
shanchu();
System.out.println("========================");
break;
case 4 :chaXunALL();
break;
default:
System.out.println("程序结束");
return;
}
}
}
//添加信息方便操作
public void m() {
list.add(new TongXunLu(1001, "aa", "12345678910", 123456, "123123@aa.com"));
list.add(new TongXunLu(1002, "bb", "12213123312", 123456, "121121@aa.com"));
list.add(new TongXunLu(1003, "cc", "12398123843", 123456, "122122@aa.com"));
list.add(new TongXunLu(1004, "张三", "15698123843", 123456, "12341324@aa.com"));
}
}
3.测试类
package com.softeem.work1;
public class Test {
public static void main(String[] args) {
TestTXL t = new TestTXL();
t.m();
t.wasd();
}
}
添加
查找
查询所有
删除