Java 通讯录管理系统
时间: 2023-09-21 20:11:28 浏览: 71
Java通讯录管理系统可以有以下功能:
1. 添加联系人:用户可以添加联系人的姓名、电话、电子邮件等信息。
2. 查找联系人:用户可以通过关键字查找已添加的联系人。
3. 修改联系人信息:用户可以修改已添加联系人的信息。
4. 删除联系人:用户可以删除已添加的联系人。
5. 显示联系人列表:用户可以查看已添加的所有联系人。
6. 数据存储:程序需要能够将添加的联系人数据持久化存储。
下面是一个简单的Java通讯录管理系统的代码示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class AddressBook {
private ArrayList<Contact> contacts = new ArrayList<>();
public void addContact(Contact contact) {
contacts.add(contact);
System.out.println("Contact added successfully!");
}
public Contact findContact(String keyword) {
for (Contact contact : contacts) {
if (contact.getName().contains(keyword) ||
contact.getPhone().contains(keyword) ||
contact.getEmail().contains(keyword)) {
return contact;
}
}
return null;
}
public void updateContact(Contact contact) {
int index = contacts.indexOf(contact);
if (index >= 0) {
contacts.set(index, contact);
System.out.println("Contact updated successfully!");
} else {
System.out.println("Contact not found!");
}
}
public void deleteContact(Contact contact) {
if (contacts.remove(contact)) {
System.out.println("Contact deleted successfully!");
} else {
System.out.println("Contact not found!");
}
}
public void displayContacts() {
System.out.println("Contact list:");
for (Contact contact : contacts) {
System.out.println(contact);
}
}
public void saveContacts() {
// TODO: implement saving contacts to a file
}
public void loadContacts() {
// TODO: implement loading contacts from a file
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
AddressBook addressBook = new AddressBook();
while (true) {
System.out.println("Please select an option:");
System.out.println("1. Add contact");
System.out.println("2. Find contact");
System.out.println("3. Update contact");
System.out.println("4. Delete contact");
System.out.println("5. Display contacts");
System.out.println("6. Exit");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.println("Enter contact name:");
String name = scanner.nextLine();
System.out.println("Enter contact phone:");
String phone = scanner.nextLine();
System.out.println("Enter contact email:");
String email = scanner.nextLine();
Contact contact = new Contact(name, phone, email);
addressBook.addContact(contact);
break;
case 2:
System.out.println("Enter search keyword:");
String keyword = scanner.nextLine();
Contact foundContact = addressBook.findContact(keyword);
if (foundContact != null) {
System.out.println("Contact found:");
System.out.println(foundContact);
} else {
System.out.println("Contact not found!");
}
break;
case 3:
System.out.println("Enter contact name:");
String updateName = scanner.nextLine();
System.out.println("Enter contact phone:");
String updatePhone = scanner.nextLine();
System.out.println("Enter contact email:");
String updateEmail = scanner.nextLine();
Contact updateContact = new Contact(updateName, updatePhone, updateEmail);
addressBook.updateContact(updateContact);
break;
case 4:
System.out.println("Enter contact name:");
String deleteName = scanner.nextLine();
System.out.println("Enter contact phone:");
String deletePhone = scanner.nextLine();
System.out.println("Enter contact email:");
String deleteEmail = scanner.nextLine();
Contact deleteContact = new Contact(deleteName, deletePhone, deleteEmail);
addressBook.deleteContact(deleteContact);
break;
case 5:
addressBook.displayContacts();
break;
case 6:
addressBook.saveContacts();
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
break;
}
}
}
}
```
联系人类:
```java
public class Contact {
private String name;
private String phone;
private String email;
public Contact(String name, String phone, String email) {
this.name = name;
this.phone = phone;
this.email = email;
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Name: " + name + ", Phone: " + phone + ", Email: " + email;
}
}
```
阅读全文