电话簿管理程序java
时间: 2023-12-05 08:41:35 浏览: 49
以下是一个简单的电话簿管理程序Java的示例,包括添加联系人、查找联系人、按电话号码查找联系人、按姓名排序联系人等功能:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class PhoneBook {
private ArrayList<Person> list = new ArrayList<Person>();
// 添加联系人
public void addPerson() {
Scanner input = new Scanner(System.in);
System.out.print("请输入姓名:"); String name = input.nextLine();
System.out.print("请输入电话号码:"); String telNum = input.nextLine();
System.out.print("请输入地址:");
String address = input.nextLine();
Person person = new Person(name, telNum, address);
list.add(person);
System.out.println("添加成功!");
}
// 查找联系人
public void searchPerson() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要查找的姓名:");
String name = input.nextLine();
boolean b = true; // 默认找不到
for (Person person : list) {
if (name.equals(person.getName())) { // 能进行到这里说明找到
System.out.println(person.toString());
b = false;
}
}
if (b) {
System.out.println("找不到对应信息!");
}
}
// 按电话号码查找联系人
public void searchByTelNum() {
Scanner input = new Scanner(System.in);
System.out.print("请输入要查找的电话号码:");
String searchTelNum = input.nextLine();
boolean b = true; // 默认找不到
for (Person person : list) {
if (searchTelNum.equals(person.getTelNum())) { // 能进行到这里说明找到
System.out.println(person.toString());
b = false;
}
}
if (b) {
System.out.println("找不到对应信息!");
}
}
// 按姓名排序联系人
public void orderName() {
Collections.sort(this.list, new OrderByName());
for (int i = 0; i < list.size(); i++) {
this.list.get(i).setId(i + 1);
}
System.out.println("排序成功");
}
// 主菜单
public void mainMenu() {
Scanner input = 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.print("请选择:");
int choice = input.nextInt();
switch (choice) {
case 1:
addPerson();
break;
case 2:
searchPerson();
break;
case 3:
searchByTelNum();
break;
case 4:
orderName();
break;
case 5:
System.out.println("感谢使用电话簿管理程序,再见!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
public static void main(String[] args) {
PhoneBook phoneBook = new PhoneBook();
phoneBook.mainMenu();
}
}
class Person {
private int id;
private String name;
private String telNum;
private String address;
public Person(String name, String telNum, String address) {
this.name = name;
this.telNum = telNum;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public String getTelNum() {
return telNum;
}
public String getAddress() {
return address;
}
public String toString() {
return "编号:" + id + ",姓名:" + name + ",电话号码:" + telNum + ",地址:" + address;
}
}
class OrderByName implements java.util.Comparator<Person> {
public int compare(Person p1, Person p2) {
return p1.getName().compareTo(p2.getName());
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)