使用list集合模拟学员数据库 先进行学员信息初试化 包括功能1.添加学员功能 2.查询全部学员 3.根据学员编号查询 4.根据学员班级名查询 5.根据学员状态查询 6.退出系统java完整代码
时间: 2024-09-08 17:00:50 浏览: 45
在Java中,可以使用`ArrayList`集合来模拟学员数据库,其中每个学员的信息可以通过一个自定义的类(例如`Student`类)来表示。以下是一个简单的示例代码,实现了您要求的功能:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Student {
private String id;
private String name;
private String classId;
private String status;
public Student(String id, String name, String classId, String status) {
this.id = id;
this.name = name;
this.classId = classId;
this.status = status;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getClassId() {
return classId;
}
public String getStatus() {
return status;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", classId='" + classId + '\'' +
", status='" + status + '\'' +
'}';
}
}
public class StudentDatabaseSimulator {
private static List<Student> studentList = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
initializeDatabase();
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.print("请选择操作:");
choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
queryAllStudents();
break;
case 3:
queryStudentById();
break;
case 4:
queryStudentsByClassId();
break;
case 5:
queryStudentsByStatus();
break;
case 6:
System.out.println("退出系统");
break;
default:
System.out.println("无效的操作,请重新选择。");
}
} while (choice != 6);
}
private static void initializeDatabase() {
studentList.add(new Student("001", "张三", "1班", "在校"));
studentList.add(new Student("002", "李四", "2班", "毕业"));
studentList.add(new Student("003", "王五", "1班", "在校"));
// 可以添加更多的学员信息
}
private static void addStudent() {
System.out.print("请输入学员编号:");
String id = scanner.next();
System.out.print("请输入学员姓名:");
String name = scanner.next();
System.out.print("请输入学员班级名:");
String classId = scanner.next();
System.out.print("请输入学员状态:");
String status = scanner.next();
studentList.add(new Student(id, name, classId, status));
System.out.println("添加成功!");
}
private static void queryAllStudents() {
for (Student student : studentList) {
System.out.println(student);
}
}
private static void queryStudentById() {
System.out.print("请输入学员编号:");
String id = scanner.next();
for (Student student : studentList) {
if (student.getId().equals(id)) {
System.out.println(student);
return;
}
}
System.out.println("未找到该学员!");
}
private static void queryStudentsByClassId() {
System.out.print("请输入学员班级名:");
String classId = scanner.next();
for (Student student : studentList) {
if (student.getClassId().equals(classId)) {
System.out.println(student);
}
}
}
private static void queryStudentsByStatus() {
System.out.print("请输入学员状态:");
String status = scanner.next();
for (Student student : studentList) {
if (student.getStatus().equals(status)) {
System.out.println(student);
}
}
}
}
```
这段代码定义了一个`Student`类来存储学员信息,并使用`StudentDatabaseSimulator`类中的`studentList`来模拟数据库存储所有学员对象。程序提供了添加学员、查询全部学员、根据学员编号查询、根据学员班级名查询、根据学员状态查询以及退出系统的功能。
阅读全文