同学通讯录:具有数据插入、修改、删除、显示、查询和统计功能的电话簿管理程序。用Java实现。
时间: 2023-12-20 22:05:32 浏览: 118
电话簿管理小程序(具有加入、删除、显示和查询联系人电话号码的功能)
好的,这是一个比较典型的Java项目,下面是一些实现思路和代码示例供您参考:
1. 定义一个联系人类,包括姓名、电话号码、电子邮件等属性,以及相应的 getter 和 setter 方法。
```java
public class Contact {
private String name;
private String phoneNumber;
private String email;
public Contact(String name, String phoneNumber, String email) {
this.name = name;
this.phoneNumber = phoneNumber;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
```
2. 定义一个通讯录类,包括联系人列表、添加联系人、删除联系人、修改联系人、查询联系人等方法。
```java
import java.util.ArrayList;
public class AddressBook {
private ArrayList<Contact> contacts;
public AddressBook() {
contacts = new ArrayList<>();
}
public void addContact(Contact contact) {
contacts.add(contact);
}
public void removeContact(Contact contact) {
contacts.remove(contact);
}
public void updateContact(Contact oldContact, Contact newContact) {
int index = contacts.indexOf(oldContact);
if (index >= 0) {
contacts.set(index, newContact);
}
}
public Contact findContactByName(String name) {
for (Contact contact : contacts) {
if (contact.getName().equals(name)) {
return contact;
}
}
return null;
}
public ArrayList<Contact> getAllContacts() {
return contacts;
}
public int countContacts() {
return contacts.size();
}
}
```
3. 编写一个基本的用户界面,允许用户输入指令并执行相应的操作。
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
AddressBook addressBook = new AddressBook();
Scanner scanner = new Scanner(System.in);
boolean quit = false;
while (!quit) {
System.out.println("Enter command: ");
String command = scanner.nextLine();
switch (command) {
case "add":
System.out.println("Enter name: ");
String name = scanner.nextLine();
System.out.println("Enter phone number: ");
String phoneNumber = scanner.nextLine();
System.out.println("Enter email: ");
String email = scanner.nextLine();
Contact contact = new Contact(name, phoneNumber, email);
addressBook.addContact(contact);
break;
case "remove":
System.out.println("Enter name: ");
String nameToRemove = scanner.nextLine();
Contact contactToRemove = addressBook.findContactByName(nameToRemove);
if (contactToRemove != null) {
addressBook.removeContact(contactToRemove);
} else {
System.out.println("Contact not found.");
}
break;
case "update":
System.out.println("Enter name to update: ");
String oldName = scanner.nextLine();
Contact oldContact = addressBook.findContactByName(oldName);
if (oldContact != null) {
System.out.println("Enter new name: ");
String newName = scanner.nextLine();
System.out.println("Enter new phone number: ");
String newPhoneNumber = scanner.nextLine();
System.out.println("Enter new email: ");
String newEmail = scanner.nextLine();
Contact newContact = new Contact(newName, newPhoneNumber, newEmail);
addressBook.updateContact(oldContact, newContact);
} else {
System.out.println("Contact not found.");
}
break;
case "list":
for (Contact c : addressBook.getAllContacts()) {
System.out.println(c.getName() + " " + c.getPhoneNumber() + " " + c.getEmail());
}
break;
case "count":
System.out.println("Number of contacts: " + addressBook.countContacts());
break;
case "quit":
quit = true;
break;
default:
System.out.println("Invalid command.");
break;
}
}
}
}
```
这个程序还有很多可以改进的地方,比如可以将数据存储到文件中,添加更多的查询条件等等。但是以上代码应该可以满足您的基本需求。
阅读全文