上述通讯录代码里的Java核心代码有哪些地方,导包语句和空方法不用写,以文件名作为小标题,不允许有空行
时间: 2023-06-14 22:08:06 浏览: 93
java通讯录
Contact.java
```java
public class Contact {
private String name;
private String phoneNumber;
// 构造方法
public Contact(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
// 获取姓名
public String getName() {
return name;
}
// 获取电话号码
public String getPhoneNumber() {
return phoneNumber;
}
// 修改电话号码
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
```
ContactList.java
```java
import java.util.ArrayList;
public class ContactList {
private ArrayList<Contact> contacts;
// 构造方法
public ContactList() {
this.contacts = new ArrayList<Contact>();
}
// 添加联系人
public void addContact(Contact contact) {
contacts.add(contact);
}
// 删除联系人
public void removeContact(Contact contact) {
contacts.remove(contact);
}
// 修改联系人信息
public void modifyContact(Contact oldContact, Contact newContact) {
int index = contacts.indexOf(oldContact);
if (index != -1) {
contacts.set(index, newContact);
}
}
// 搜索联系人
public Contact searchContact(String name) {
for (Contact contact : contacts) {
if (contact.getName().equals(name)) {
return contact;
}
}
return null;
}
// 获取联系人列表
public ArrayList<Contact> getContacts() {
return contacts;
}
}
```
Main.java
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ContactList contactList = new ContactList();
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. 退出程序");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1: // 添加联系人
System.out.println("请输入联系人姓名:");
String name = scanner.nextLine();
System.out.println("请输入联系人电话号码:");
String phoneNumber = scanner.nextLine();
Contact contact = new Contact(name, phoneNumber);
contactList.addContact(contact);
System.out.println("联系人添加成功!");
break;
case 2: // 删除联系人
System.out.println("请输入联系人姓名:");
name = scanner.nextLine();
contact = contactList.searchContact(name);
if (contact != null) {
contactList.removeContact(contact);
System.out.println("联系人删除成功!");
} else {
System.out.println("联系人不存在!");
}
break;
case 3: // 修改联系人信息
System.out.println("请输入联系人姓名:");
name = scanner.nextLine();
contact = contactList.searchContact(name);
if (contact != null) {
System.out.println("请输入新的联系人姓名:");
String newName = scanner.nextLine();
System.out.println("请输入新的联系人电话号码:");
String newPhoneNumber = scanner.nextLine();
Contact newContact = new Contact(newName, newPhoneNumber);
contactList.modifyContact(contact, newContact);
System.out.println("联系人信息修改成功!");
} else {
System.out.println("联系人不存在!");
}
break;
case 4: // 搜索联系人
System.out.println("请输入联系人姓名:");
name = scanner.nextLine();
contact = contactList.searchContact(name);
if (contact != null) {
System.out.println("联系人姓名:" + contact.getName());
System.out.println("联系人电话号码:" + contact.getPhoneNumber());
} else {
System.out.println("联系人不存在!");
}
break;
case 5: // 显示联系人列表
System.out.println("联系人列表:");
for (Contact c : contactList.getContacts()) {
System.out.println("联系人姓名:" + c.getName());
System.out.println("联系人电话号码:" + c.getPhoneNumber());
}
break;
case 6: // 退出程序
System.out.println("程序已退出!");
System.exit(0);
break;
default:
System.out.println("无效的操作!");
break;
}
}
}
}
```
阅读全文