1、命令行运行
2、能建立、修改和增删通讯录
3、能导入导出通讯录
4、能按多种方式进行查询
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.io.File;
public class ContactBook {
// 定义通讯录联系人类
static class Contact {
String name; // 联系人姓名
String phoneNumber; // 联系人电话号码
String gender;//联系人性别
String email;//联系人邮箱
public Contact(String name, String phoneNumber, String gender, String email) {
this.name = name;
this.phoneNumber = phoneNumber;
this.gender = gender;
this.email = email;
}
public String toString() {
return "姓名:" + name + "\t电话号码:" + phoneNumber + "\t性别" + gender + "\t邮箱" + email;
}
}
public static void main(String[] args) throws IOException {
ArrayList<Contact> contacts = new ArrayList<>(); // 存放所有联系人信息的ArrayList
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("5. 显示所有联系人");
System.out.println("6. 导入通讯录");
System.out.println("7. 导出通讯录");
System.out.println("0. 退出程序");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addContact(contacts, scanner);
break;
case 2:
modifyContact(contacts, scanner);
break;
case 3:
deleteContact(contacts, scanner);
break;
case 4:
searchContacts(contacts, scanner);
break;
case 5:
showAllContacts(contacts);
break;
case 6:
importContacts(contacts, scanner);
break;
case 7:
exportContacts(contacts, scanner);
break;
case 0:
System.out.println("程序已退出!");
return;
default:
System.out.println("输入错误,请重新输入!");
}
}
}
// 添加联系人
public static void addContact(ArrayList<Contact> contacts, Scanner scanner) {
System.out.print("请输入联系人姓名:");
String name = scanner.next();
System.out.print("请输入联系人电话号码:");
String phoneNumber = scanner.next();
System.out.print("请输入联系人性别:");
String gender = scanner.next();
System.out.print("请输入联系人邮箱:");
String email = scanner.next();
Contact contact = new Contact(name, phoneNumber,gender,email);
contacts.add(contact); // 将新的联系人添加到ArrayList中
System.out.println("添加成功!");
}
// 修改联系人
public static void modifyContact(ArrayList<Contact> contacts, Scanner scanner) {
if (contacts.size() == 0) {
System.out.println("通讯录为空,无法修改!");
return;
}
System.out.print("请输入要修改的联系人姓名:");
String name = scanner.next();
for (int i = 0; i < contacts.size(); i++) {
if (name.equals(contacts.get(i).name)) { // 找到要修改的联系人
System.out.print("请输入新的电话号码:");
String phoneNumber = scanner.next();
System.out.print("请输入新的性别:");
String gender = scanner.next();
System.out.print("请输入新的邮箱:");
String email = scanner.next();
Contact newContact = new Contact(name, phoneNumber,gender,email); // 创建一个新的联系人对象
contacts.set(i, newContact); // 替换原来的联系人对象
System.out.println("修改成功!");
return;
}
}
System.out.println("未找到要修改的联系人!");
}
// 删除联系人
public static void deleteContact(ArrayList<Contact> contacts, Scanner scanner) {
if (contacts.size() == 0) {
System.out.println("通讯录为空,无法删除!");
return;
}
System.out.print("请输入要删除的联系人姓名:");
String name = scanner.next();
for (int i = 0; i < contacts.size(); i++) {
if (name.equals(contacts.get(i).name)) { // 找到要删除的联系人
contacts.remove(i); // 删除该联系人
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到要删除的联系人!");
}
// 查询联系人
public static void searchContacts(ArrayList<Contact> contacts, Scanner scanner) {
if (contacts.size() == 0) {
System.out.println("通讯录为空,无法查询!");
return;
}
while (true) {
System.out.println("请选择查询方式:");
System.out.println("1. 按姓名查询");
System.out.println("2. 按电话号码查询");
System.out.println("3. 按邮箱查询");
int choice = scanner.nextInt();
switch (choice) {
case 1:
searchByName(contacts, scanner);
return;
case 2:
searchByPhoneNumber(contacts, scanner);
return;
case 3:
searchByemail(contacts, scanner);
return;
default:
System.out.println("输入错误,请重新输入!");
}
}
}
// 按姓名查询联系人
public static void searchByName(ArrayList<Contact> contacts, Scanner scanner) {
System.out.print("请输入要查询的联系人姓名:");
String name = scanner.next();
for (int i = 0; i < contacts.size(); i++) {
if (name.equals(contacts.get(i).name)) { // 找到要查询的联系人
System.out.println(contacts.get(i));
return;
}
}
System.out.println("未找到该联系人!");
}
// 按电话号码查询联系人
public static void searchByPhoneNumber(ArrayList<Contact> contacts, Scanner scanner) {
System.out.print("请输入要查询的电话号码:");
String phoneNumber = scanner.next();
for (int i = 0; i < contacts.size(); i++) {
if (phoneNumber.equals(contacts.get(i).phoneNumber)) { // 找到要查询的联系人
System.out.println(contacts.get(i));
return;
}
}
System.out.println("未找到该联系人!");
}
// 按邮箱查询联系人
public static void searchByemail(ArrayList<Contact> contacts, Scanner scanner) {
System.out.print("请输入要查询的联系人邮箱:");
String email = scanner.next();
for (int i = 0; i < contacts.size(); i++) {
if (email.equals(contacts.get(i).email)) { // 找到要查询的联系人
System.out.println(contacts.get(i));
return;
}
}
System.out.println("未找到该联系人!");
}
// 显示所有联系人
public static void showAllContacts(ArrayList<Contact> contacts) {
if (contacts.size() == 0) {
System.out.println("通讯录为空!");
return;
}
for (Contact contact : contacts) {
System.out.println(contact);
}
}
// 导入通讯录
public static void importContacts(ArrayList<Contact> contacts, Scanner scanner) throws IOException {
System.out.print("请输入导入文件的路径:");
String filePath = scanner.next();
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();//如果文件不存在,则创建新文件
}
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) { // 逐行读取文件内容
String[] parts = line.split(","); // 将一行内容按逗号分隔成四部分,分别为姓名 电话号码 性别 邮箱
String name = parts[0];
String phoneNumber = parts[1];
String gender = parts[2];
String email = parts[3];
Contact contact = new Contact(name, phoneNumber,gender,email);
contacts.add(contact); // 将新的联系人添加到ArrayList中
System.out.print(name + "\t" + phoneNumber + "\t" + gender + "\t" + email);
}
reader.close();
System.out.println("导入成功!");
}
// 导出通讯录
public static void exportContacts(ArrayList<Contact> contacts, Scanner scanner) throws IOException {
if (contacts.size() == 0) {
System.out.println("通讯录为空,无法导出!");
return;
}
System.out.print("请输入导出文件的路径:");
String filePath = scanner.next();
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
for (Contact contact : contacts) {
writer.write(contact.name + "," + contact.phoneNumber + "," + contact.gender + "," + contact.email); // 将联系人信息按逗号分隔写入文件中
writer.newLine(); // 换行
}
writer.close();
System.out.println("导出成功!");
}
}