java6. 完成一个通讯录,要求实现如下功能: (1)添加联系人(联系人:编号,姓名,手机号,QQ,邮箱地址)添加时需要检查手机号和邮箱地址格式是否正确,若不正确,不允许添加 (2)联系人查询(输入姓名或电话查询) (3)显示联系人列表 (4)根据编号删除指定编号的联系人
时间: 2024-04-16 17:23:52 浏览: 74
基于java 实现 通讯录 及联系人的增删减
下面是一个示例的通讯录程序的实现代码:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Contact {
private int id;
private String name;
private String phoneNumber;
private String qq;
private String email;
public Contact(int id, String name, String phoneNumber, String qq, String email) {
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber;
this.qq = qq;
this.email = email;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getQq() {
return qq;
}
public String getEmail() {
return email;
}
}
class AddressBook {
private List<Contact> contacts;
public AddressBook() {
contacts = new ArrayList<>();
}
public void addContact(Contact contact) {
if (validatePhoneNumber(contact.getPhoneNumber()) && validateEmail(contact.getEmail())) {
contacts.add(contact);
System.out.println("联系人添加成功!");
} else {
System.out.println("手机号或邮箱地址格式不正确,联系人添加失败!");
}
}
public List<Contact> searchContact(String keyword) {
List<Contact> searchResult = new ArrayList<>();
for (Contact contact : contacts) {
if (contact.getName().equalsIgnoreCase(keyword) || contact.getPhoneNumber().equals(keyword)) {
searchResult.add(contact);
}
}
return searchResult;
}
public void displayContacts() {
if (contacts.isEmpty()) {
System.out.println("通讯录为空!");
} else {
System.out.println("联系人列表:");
for (Contact contact : contacts) {
System.out.println("编号:" + contact.getId());
System.out.println("姓名:" + contact.getName());
System.out.println("手机号:" + contact.getPhoneNumber());
System.out.println("QQ:" + contact.getQq());
System.out.println("邮箱地址:" + contact.getEmail());
System.out.println("--------------------");
}
}
}
public void deleteContact(int id) {
int index = -1;
for (int i = 0; i < contacts.size(); i++) {
if (contacts.get(i).getId() == id) {
index = i;
break;
}
}
if (index != -1) {
contacts.remove(index);
System.out.println("联系人删除成功!");
} else {
System.out.println("未找到对应编号的联系人!");
}
}
private boolean validatePhoneNumber(String phoneNumber) {
// 验证手机号格式是否正确
String regex = "^1[3456789]\\d{9}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(phoneNumber);
return matcher.matches();
}
private boolean validateEmail(String email) {
// 验证邮箱地址格式是否正确
String regex = "^[A-Za-z0-9]+([._\\\\-]*[A-Za-z0-9])*@([A-Za-z0-9]+[-\\\\.])+[A-Za-z]{2,6}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
}
public class Main {
public static void main(String[] args) {
AddressBook addressBook = new AddressBook();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加联系人");
System.out.println("2. 联系人查询");
System.out.println("3. 显示联系人列表");
System.out.println("4. 根据编号删除联系人");
System.out.println("0. 退出");
int choice = scanner.nextInt();
scanner.nextLine(); // 读取换行符
switch (choice) {
case 1:
System.out.println("请输入联系人信息:");
System.out.print("编号:");
int id = scanner.nextInt();
scanner.nextLine(); // 读取换行符
System.out.print("姓名:");
String name = scanner.nextLine();
System.out.print("手机号:");
String phoneNumber = scanner.nextLine();
System.out.print("QQ:");
String qq = scanner.nextLine();
System.out.print("邮箱地址:");
String email = scanner.nextLine();
Contact contact = new Contact(id, name, phoneNumber, qq, email);
addressBook.addContact(contact);
break;
case 2:
System.out.println("请输入查询关键词(姓名或电话):");
String keyword = scanner.nextLine();
List<Contact> searchResult = addressBook.searchContact(keyword);
if (searchResult.isEmpty()) {
System.out.println("未找到匹配的联系人!");
} else {
System.out.println("查询结果:");
for (Contact result : searchResult) {
System.out.println("编号:" + result.getId());
System.out.println("姓名:" + result.getName());
System.out.println("手机号:" + result.getPhoneNumber());
System.out.println("QQ:" + result.getQq());
System.out.println("邮箱地址:" + result.getEmail());
System.out.println("--------------------");
}
}
break;
case 3:
addressBook.displayContacts();
break;
case 4:
System.out.println("请输入要删除的联系人编号:");
int contactId = scanner.nextInt();
addressBook.deleteContact(contactId);
break;
case 0:
System.out.println("程序已退出!");
return;
default:
System.out.println("无效的操作!");
}
}
}
}
```
你可以根据需要进行修改和完善,以满足你的具体要求。注意在添加联系人时,会检查手机号和邮箱地址的格式是否正确,如果不正确,则不允许添加联系人。
阅读全文