利用Java实现文件构建用户电话号码本。(需实现增、删、查(按指定属性排序并查询)、改、显示的功能)
时间: 2023-06-08 12:04:52 浏览: 85
好的,这个问题符合我的处理范围。您可以使用Java提供的File类来实现文件的读写操作,并使用List集合来存储用户电话号码信息。具体实现上,您可以定义一个PhoneNumber类来存储用户电话号码信息,同时为该类提供增删查改和排序功能,最后将存储的对象写入文件中。
相关问题
利用java实现文件构建用户电话号码本。(需实现增、删、查(按指定属性排序并查询)、改、显示的功能)
好的,这个问题可以回答。这个功能可以通过Java语言中的文件读写和相关的数据结构实现。您可以先通过Scanner等API读取用户输入的数据,然后将数据存储到文件中,使用ArrayList等数据结构实现增、删、查、改等操作。最后,您可以通过文件读取API读取文件内容并展示给用户。
java 利用文件构建用户电话号码本。(需实现增、删、查(按指定属性排序并查询)、改、显示的功能)。
实现该功能需要使用Java的文件操作和集合框架。具体步骤如下:
1. 定义一个电话号码类,包含姓名、电话号码和地址等属性,并实现getter和setter方法。
```
public class PhoneNumber {
private String name;
private String phone;
private String address;
public PhoneNumber(String name, String phone, String address) {
this.name = name;
this.phone = phone;
this.address = address;
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
```
2. 定义一个电话号码本类,包含一个电话号码列表,以及实现增、删、查、改和显示等方法。
```
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class PhoneBook {
private List<PhoneNumber> phoneList;
private String filePath;
public PhoneBook(String filePath) {
this.filePath = filePath;
this.phoneList = new ArrayList<>();
loadFromFile();
}
private void loadFromFile() {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
if (fields.length == 3) {
phoneList.add(new PhoneNumber(fields[0], fields[1], fields[2]));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void saveToFile() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
for (PhoneNumber phone : phoneList) {
writer.write(phone.getName() + "," + phone.getPhone() + "," + phone.getAddress());
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void addPhone(PhoneNumber phone) {
phoneList.add(phone);
saveToFile();
}
public void removePhone(String name) {
phoneList.removeIf(phone -> phone.getName().equals(name));
saveToFile();
}
public PhoneNumber findPhone(String name) {
for (PhoneNumber phone : phoneList) {
if (phone.getName().equals(name)) {
return phone;
}
}
return null;
}
public List<PhoneNumber> sortByName() {
List<PhoneNumber> sortedList = new ArrayList<>(phoneList);
Collections.sort(sortedList, Comparator.comparing(PhoneNumber::getName));
return sortedList;
}
public List<PhoneNumber> sortByPhone() {
List<PhoneNumber> sortedList = new ArrayList<>(phoneList);
Collections.sort(sortedList, Comparator.comparing(PhoneNumber::getPhone));
return sortedList;
}
public void updatePhone(String name, String phone, String address) {
for (PhoneNumber p : phoneList) {
if (p.getName().equals(name)) {
p.setPhone(phone);
p.setAddress(address);
break;
}
}
saveToFile();
}
public void display() {
for (PhoneNumber phone : phoneList) {
System.out.println(phone.getName() + "," + phone.getPhone() + "," + phone.getAddress());
}
}
}
```
3. 在主函数中创建电话号码本对象,并通过控制台输入实现增、删、查、改和显示等操作。
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
PhoneBook phoneBook = new PhoneBook("phonebook.txt");
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();
scanner.nextLine(); // consume newline
switch (choice) {
case 1:
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入电话号码:");
String phone = scanner.nextLine();
System.out.print("请输入地址:");
String address = scanner.nextLine();
phoneBook.addPhone(new PhoneNumber(name, phone, address));
System.out.println("电话号码已添加。");
break;
case 2:
System.out.print("请输入姓名:");
name = scanner.nextLine();
phoneBook.removePhone(name);
System.out.println("电话号码已删除。");
break;
case 3:
System.out.print("请输入姓名:");
name = scanner.nextLine();
PhoneNumber p = phoneBook.findPhone(name);
if (p != null) {
System.out.println(p.getName() + "," + p.getPhone() + "," + p.getAddress());
} else {
System.out.println("电话号码不存在。");
}
break;
case 4:
System.out.print("请输入姓名:");
name = scanner.nextLine();
System.out.print("请输入电话号码:");
phone = scanner.nextLine();
System.out.print("请输入地址:");
address = scanner.nextLine();
phoneBook.updatePhone(name, phone, address);
System.out.println("电话号码已更新。");
break;
case 5:
for (PhoneNumber p1 : phoneBook.sortByName()) {
System.out.println(p1.getName() + "," + p1.getPhone() + "," + p1.getAddress());
}
break;
case 6:
for (PhoneNumber p1 : phoneBook.sortByPhone()) {
System.out.println(p1.getName() + "," + p1.getPhone() + "," + p1.getAddress());
}
break;
case 7:
phoneBook.display();
break;
case 0:
return;
default:
System.out.println("无效操作。");
break;
}
}
}
}
```
注意:上述代码中的电话号码本使用了文本文件"phonebook.txt"存储电话号码信息,每行格式为"name,phone,address"。如果文件不存在,程序会自动创建一个新的文件。如果需要修改文件路径或格式,需要修改相应的代码。
阅读全文