设计一个学生通信录,学生通迅录的数据信息构成内容可自行设计(如:姓名、家庭地址、邮政编码、电话号码等等),通信录数据类型定义为结构体类型。 1.主要功能: (1) 能建立、修改和增删学生通讯录 (2) 能够按多种方式进行查询 2.要求:界面友好,易于操作。用Java写出代码
时间: 2024-04-29 21:26:48 浏览: 73
非常感谢您的提问!以下是我给您提供的代码:
```
import java.util.*;
public class StudentCommunicationList {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
List<Student> students = new ArrayList<Student>();
while (true) {
System.out.println("请选择操作:\n1.建立\n2.修改\n3.增加\n4.删除\n5.查询\n6.退出");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("请输入学生信息,格式为:姓名 家庭地址 邮政编码 电话号码");
Student student1 = new Student(input.next(), input.next(), input.next(), input.next());
students.add(student1);
System.out.println("学生信息建立成功!");
break;
case 2:
System.out.println("请输入要修改的学生姓名:");
String name1 = input.next();
boolean found1 = false;
for (Student student2 : students) {
if (student2.name.equals(name1)) {
System.out.println("请输入修改后的学生信息,格式为:姓名 家庭地址 邮政编码 电话号码");
student2.name = input.next();
student2.address = input.next();
student2.zipCode = input.next();
student2.phoneNumber = input.next();
found1 = true;
System.out.println("学生信息修改成功!");
}
}
if (!found1) {
System.out.println("查无此人!");
}
break;
case 3:
System.out.println("请输入要增加的学生信息,格式为:姓名 家庭地址 邮政编码 电话号码");
Student student3 = new Student(input.next(), input.next(), input.next(), input.next());
students.add(student3);
System.out.println("学生信息增加成功!");
break;
case 4:
System.out.println("请输入要删除的学生姓名:");
String name2 = input.next();
boolean found2 = false;
for (Student student4 : students) {
if (student4.name.equals(name2)) {
students.remove(student4);
found2 = true;
System.out.println("学生信息删除成功!");
break;
}
}
if (!found2) {
System.out.println("查无此人!");
}
break;
case 5:
System.out.println("请选择查询方式:\n1.按姓名查询\n2.按家庭地址查询\n3.按邮政编码查询\n4.按电话号码查询");
int choice2 = input.nextInt();
switch (choice2) {
case 1:
System.out.println("请输入要查询的学生姓名:");
String name3 = input.next();
boolean found3 = false;
for (Student student5 : students) {
if (student5.name.equals(name3)) {
System.out.println(student5);
found3 = true;
break;
}
}
if (!found3) {
System.out.println("查无此人!");
}
break;
case 2:
System.out.println("请输入要查询的学生家庭地址:");
String address1 = input.next();
boolean found4 = false;
for (Student student6 : students) {
if (student6.address.equals(address1)) {
System.out.println(student6);
found4 = true;
}
}
if (!found4) {
System.out.println("查无此人!");
}
break;
case 3:
System.out.println("请输入要查询的学生邮政编码:");
String zipCode1 = input.next();
boolean found5 = false;
for (Student student7 : students) {
if (student7.zipCode.equals(zipCode1)) {
System.out.println(student7);
found5 = true;
}
}
if (!found5) {
System.out.println("查无此人!");
}
break;
case 4:
System.out.println("请输入要查询的学生电话号码:");
String phoneNumber1 = input.next();
boolean found6 = false;
for (Student student8 : students) {
if (student8.phoneNumber.equals(phoneNumber1)) {
System.out.println(student8);
found6 = true;
}
}
if (!found6) {
System.out.println("查无此人!");
}
break;
default:
System.out.println("输入有误!");
break;
}
break;
case 6:
System.exit(0);
default:
System.out.println("输入有误!");
break;
}
}
}
}
class Student {
String name;
String address;
String zipCode;
String phoneNumber;
public Student(String name, String address, String zipCode, String phoneNumber) {
this.name = name;
this.address = address;
this.zipCode = zipCode;
this.phoneNumber = phoneNumber;
}
@Override
public String toString() {
return "姓名:" + name + " 家庭地址:" + address + " 邮政编码:" + zipCode + " 电话号码:" + phoneNumber;
}
}
```
这里我们使用了Java中的List和Struct来实现学生通信录的建立、修改、增加、删除和查询等操作。对于查询还支持了多种查询方式,方便用户使用。如果您有任何其他问题,请随时与我交流哦~
阅读全文