通讯录管理系统设计用JAVA实现以下代码 1)录入成员信息。基本信息:姓名、现就读大学名称及班级名、专业、家庭住址、家庭联系电话、邮编、本人联系电话等。2)删除一个成员信息。3)修改成员信息并保存 4)能够按姓名对成员信息进行检索并将检索结果显示在屏幕上。 5)能够按专业对成员信息进行检索并将检索结果显示在屏幕上。6)显示所有成员信息。 7)要求利用菜单的形式进行管理。
时间: 2024-03-30 10:36:37 浏览: 34
好的,以下是通讯录管理系统的JAVA代码,包含了您提到的所有功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class AddressBookManagementSystem {
private ArrayList<Member> members = new ArrayList<>();
public static void main(String[] args) {
AddressBookManagementSystem system = new AddressBookManagementSystem();
system.run();
}
public void run() {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("1. 添加成员信息");
System.out.println("2. 删除成员信息");
System.out.println("3. 修改成员信息");
System.out.println("4. 按姓名检索成员信息");
System.out.println("5. 按专业检索成员信息");
System.out.println("6. 显示所有成员信息");
System.out.println("7. 退出");
System.out.print("请选择操作:");
choice = scanner.nextInt();
switch (choice) {
case 1:
addMember();
break;
case 2:
deleteMember();
break;
case 3:
modifyMember();
break;
case 4:
searchByName();
break;
case 5:
searchByMajor();
break;
case 6:
displayAllMembers();
break;
case 7:
break;
default:
System.out.println("无效操作,请重新选择。");
}
} while (choice != 7);
}
private void addMember() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入现就读大学名称及班级名:");
String className = scanner.nextLine();
System.out.print("请输入专业:");
String major = scanner.nextLine();
System.out.print("请输入家庭住址:");
String address = scanner.nextLine();
System.out.print("请输入家庭联系电话:");
String homePhone = scanner.nextLine();
System.out.print("请输入邮编:");
String postalCode = scanner.nextLine();
System.out.print("请输入本人联系电话:");
String phone = scanner.nextLine();
Member member = new Member(name, className, major, address, homePhone, postalCode, phone);
members.add(member);
System.out.println("成员信息已添加。");
}
private void deleteMember() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的成员姓名:");
String name = scanner.nextLine();
boolean isDeleted = false;
for (Member member : members) {
if (member.getName().equals(name)) {
members.remove(member);
isDeleted = true;
break;
}
}
if (isDeleted) {
System.out.println("成员信息已删除。");
} else {
System.out.println("没有找到该成员信息。");
}
}
private void modifyMember() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要修改的成员姓名:");
String name = scanner.nextLine();
boolean isModified = false;
for (Member member : members) {
if (member.getName().equals(name)) {
System.out.print("请输入新的现就读大学名称及班级名:");
String className = scanner.nextLine();
System.out.print("请输入新的专业:");
String major = scanner.nextLine();
System.out.print("请输入新的家庭住址:");
String address = scanner.nextLine();
System.out.print("请输入新的家庭联系电话:");
String homePhone = scanner.nextLine();
System.out.print("请输入新的邮编:");
String postalCode = scanner.nextLine();
System.out.print("请输入新的本人联系电话:");
String phone = scanner.nextLine();
member.setClassName(className);
member.setMajor(major);
member.setAddress(address);
member.setHomePhone(homePhone);
member.setPostalCode(postalCode);
member.setPhone(phone);
isModified = true;
break;
}
}
if (isModified) {
System.out.println("成员信息已修改。");
} else {
System.out.println("没有找到该成员信息。");
}
}
private void searchByName() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查找的成员姓名:");
String name = scanner.nextLine();
boolean isFound = false;
for (Member member : members) {
if (member.getName().equals(name)) {
System.out.println(member);
isFound = true;
}
}
if (!isFound) {
System.out.println("没有找到该成员信息。");
}
}
private void searchByMajor() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查找的成员专业:");
String major = scanner.nextLine();
boolean isFound = false;
for (Member member : members) {
if (member.getMajor().equals(major)) {
System.out.println(member);
isFound = true;
}
}
if (!isFound) {
System.out.println("没有找到该成员信息。");
}
}
private void displayAllMembers() {
for (Member member : members) {
System.out.println(member);
}
}
}
class Member {
private String name;
private String className;
private String major;
private String address;
private String homePhone;
private String postalCode;
private String phone;
public Member(String name, String className, String major, String address, String homePhone, String postalCode, String phone) {
this.name = name;
this.className = className;
this.major = major;
this.address = address;
this.homePhone = homePhone;
this.postalCode = postalCode;
this.phone = phone;
}
public String getName() {
return name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getHomePhone() {
return homePhone;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "姓名:" + name + "\n" +
"现就读大学名称及班级名:" + className + "\n" +
"专业:" + major + "\n" +
"家庭住址:" + address + "\n" +
"家庭联系电话:" + homePhone + "\n" +
"邮编:" + postalCode + "\n" +
"本人联系电话:" + phone + "\n";
}
}
```
您可以将以上代码保存为一个名为 `AddressBookManagementSystem.java` 的文件,然后在命令行中使用 `javac AddressBookManagementSystem.java` 命令编译它,再使用 `java AddressBookManagementSystem` 命令运行它。
希望这个代码可以帮助到您!
阅读全文