JAVA课程设计通讯录
时间: 2023-12-03 18:04:12 浏览: 89
Java课程设计 通讯录的制作
4星 · 用户满意度95%
以下是一个简单的Java通讯录设计示例,它包括添加联系人、删除联系人、查找联系人和显示所有联系人等功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class AddressBook {
private ArrayList<Contact> contacts;
public AddressBook() {
contacts = new ArrayList<Contact>();
}
public void addContact(Contact contact) {
contacts.add(contact);
}
public void removeContact(Contact contact) {
contacts.remove(contact);
}
public Contact findContact(String name) {
for (Contact contact : contacts) {
if (contact.getName().equals(name)) {
return contact;
}
}
return null;
}
public void displayContacts() {
for (Contact contact : contacts) {
System.out.println(contact);
}
}
public static void main(String[] args) {
AddressBook addressBook = new AddressBook();
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.退出");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.println("请输入联系人姓名:");
String name = scanner.nextLine();
System.out.println("请输入联系人电话:");
String phone = scanner.nextLine();
Contact contact = new Contact(name, phone);
addressBook.addContact(contact);
System.out.println("联系人添加成功!");
break;
case 2:
System.out.println("请输入联系人姓名:");
String name2 = scanner.nextLine();
Contact contact2 = addressBook.findContact(name2);
if (contact2 != null) {
addressBook.removeContact(contact2);
System.out.println("联系人删除成功!");
} else {
System.out.println("联系人不存在!");
}
break;
case 3:
System.out.println("请输入联系人姓名:");
String name3 = scanner.nextLine();
Contact contact3 = addressBook.findContact(name3);
if (contact3 != null) {
System.out.println(contact3);
} else {
System.out.println("联系人不存在!");
}
break;
case 4:
addressBook.displayContacts();
break;
case 5:
System.exit(0);
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
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 String toString() {
return "姓名:" + name + ",电话:" + phone;
}
}
```
阅读全文