2、利用文件构建用户电话号码本。(需实现增、删、查(按指定属性排序并查询)、改、显示的功能)。Java
时间: 2023-12-21 22:03:13 浏览: 95
在Java中实现增、删、改、查功能
可以使用Java中的文件操作API来实现用户电话号码本的增、删、查、改和显示功能,以下是一个简单的实现:
```java
import java.io.*;
import java.util.*;
public class PhoneNumberBook {
private static final String FILENAME = "phonebook.txt"; // 文件名
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice = 0;
do {
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("0. 退出");
System.out.print("请输入选项:");
choice = scanner.nextInt();
switch (choice) {
case 1:
addContact();
break;
case 2:
deleteContact();
break;
case 3:
searchContact();
break;
case 4:
modifyContact();
break;
case 5:
displayContacts();
break;
case 0:
System.out.println("谢谢使用!");
break;
default:
System.out.println("无效选项,请重新输入!");
break;
}
} while (choice != 0);
}
// 添加联系人
private static void addContact() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入电话号码:");
String phone = scanner.nextLine();
Contact contact = new Contact(name, phone);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILENAME, true))) {
writer.write(contact.toString());
writer.newLine();
System.out.println("添加联系人成功!");
} catch (IOException e) {
System.out.println("添加联系人失败:" + e.getMessage());
}
}
// 删除联系人
private static void deleteContact() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的联系人姓名:");
String name = scanner.nextLine();
List<Contact> contacts = readContacts();
boolean found = false;
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILENAME))) {
for (Contact contact : contacts) {
if (contact.getName().equals(name)) {
found = true;
} else {
writer.write(contact.toString());
writer.newLine();
}
}
if (found) {
System.out.println("删除联系人成功!");
} else {
System.out.println("未找到该联系人!");
}
} catch (IOException e) {
System.out.println("删除联系人失败:" + e.getMessage());
}
}
// 查询联系人
private static void searchContact() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查询的联系人姓名:");
String name = scanner.nextLine();
List<Contact> contacts = readContacts();
List<Contact> results = new ArrayList<>();
for (Contact contact : contacts) {
if (contact.getName().contains(name)) {
results.add(contact);
}
}
if (results.isEmpty()) {
System.out.println("未找到匹配的联系人!");
} else {
System.out.println("查询结果如下:");
sortByProperty(results);
displayContacts(results);
}
}
// 修改联系人
private static void modifyContact() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要修改的联系人姓名:");
String name = scanner.nextLine();
List<Contact> contacts = readContacts();
boolean found = false;
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILENAME))) {
for (Contact contact : contacts) {
if (contact.getName().equals(name)) {
System.out.print("请输入新的电话号码:");
String phone = scanner.nextLine();
contact.setPhone(phone);
writer.write(contact.toString());
writer.newLine();
found = true;
} else {
writer.write(contact.toString());
writer.newLine();
}
}
if (found) {
System.out.println("修改联系人成功!");
} else {
System.out.println("未找到该联系人!");
}
} catch (IOException e) {
System.out.println("修改联系人失败:" + e.getMessage());
}
}
// 显示联系人
private static void displayContacts() {
List<Contact> contacts = readContacts();
if (contacts.isEmpty()) {
System.out.println("电话号码本为空!");
} else {
sortByProperty(contacts);
displayContacts(contacts);
}
}
// 读取联系人信息
private static List<Contact> readContacts() {
List<Contact> contacts = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(FILENAME))) {
String line = null;
while ((line = reader.readLine()) != null) {
Contact contact = Contact.fromString(line);
if (contact != null) {
contacts.add(contact);
}
}
} catch (IOException e) {
System.out.println("读取文件失败:" + e.getMessage());
}
return contacts;
}
// 按指定属性排序并显示联系人
private static void sortByProperty(List<Contact> contacts) {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择排序属性:");
System.out.println("1. 姓名");
System.out.println("2. 电话号码");
System.out.print("请输入选项:");
int choice = scanner.nextInt();
Comparator<Contact> comparator = null;
switch (choice) {
case 1:
comparator = Comparator.comparing(Contact::getName);
break;
case 2:
comparator = Comparator.comparing(Contact::getPhone);
break;
default:
System.out.println("无效选项,使用默认排序规则:按姓名排序。");
comparator = Comparator.comparing(Contact::getName);
break;
}
contacts.sort(comparator);
}
// 显示联系人
private static void displayContacts(List<Contact> contacts) {
System.out.println("姓名\t\t电话号码");
for (Contact contact : contacts) {
System.out.println(contact.getName() + "\t\t" + contact.getPhone());
}
}
// 联系人类
private static class Contact {
private String name;
private String phone;
public Contact(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return name + "," + phone;
}
public static Contact fromString(String str) {
String[] parts = str.split(",");
if (parts.length == 2) {
return new Contact(parts[0], parts[1]);
} else {
return null;
}
}
}
}
```
在程序中,我们使用了一个`Contact`类来表示每个联系人,其中包含姓名和电话号码两个属性。程序启动后,用户可以选择添加、删除、查询、修改和显示联系人信息。联系人信息保存在一个名为`phonebook.txt`的文本文件中,每行表示一个联系人,格式为`姓名,电话号码`。在实现查询和排序时,我们使用了Java集合框架中的`List`和`Comparator`来方便地对联系人进行排序和过滤。
阅读全文