用Java编写一个程序,模拟学生管理系统。可以实现对学生信息的添加、删除、修改和查询功能,要求使用List集合存储自定义的对象集合,并熟练使用List集合的中常用方法实现相关的操作并且使用Iterator迭代器
时间: 2024-05-09 13:16:00 浏览: 103
好的,我明白了你的问题。下面是一个基本的Java程序,用于模拟学生管理系统,实现了添加、删除、修改和查询学生信息的基本功能。该程序使用List集合存储自定义的Student对象集合,并使用Iterator迭代器实现相关的操作。
```
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class StudentManagementSystem {
private List<Student> studentList;
public StudentManagementSystem() {
studentList = new ArrayList<Student>();
}
public static void main(String[] args) {
StudentManagementSystem sms = new StudentManagementSystem();
sms.run();
}
public void run() {
int choice;
Scanner scanner = new Scanner(System.in);
do {
System.out.println("学生管理系统:");
System.out.println("1. 添加学生");
System.out.println("2. 删除学生");
System.out.println("3. 修改学生");
System.out.println("4. 查询学生");
System.out.println("0. 退出");
System.out.print("请输入您的选择: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent(scanner);
break;
case 2:
deleteStudent(scanner);
break;
case 3:
modifyStudent(scanner);
break;
case 4:
queryStudent(scanner);
break;
case 0:
System.out.println("退出程序!");
break;
default:
System.out.println("无效选择,请重新选择!");
}
} while (choice != 0);
scanner.close();
}
public void addStudent(Scanner scanner) {
String name, id;
int age;
System.out.print("请输入学生姓名: ");
name = scanner.next();
System.out.print("请输入学生学号: ");
id = scanner.next();
System.out.print("请输入学生年龄: ");
age = scanner.nextInt();
// 创建一个新的Student对象并添加到列表中
Student student = new Student(name, id, age);
studentList.add(student);
System.out.println("添加成功!");
}
public void deleteStudent(Scanner scanner) {
String id;
System.out.print("请输入要删除学生的学号: ");
id = scanner.next();
Iterator<Student> it = studentList.iterator();
while (it.hasNext()) {
Student student = it.next();
if (student.getId().equals(id)) {
it.remove();
System.out.println("删除成功!");
return;
}
}
System.out.println("没有找到该学生!");
}
public void modifyStudent(Scanner scanner) {
String id, name;
int age;
System.out.print("请输入要修改学生的学号: ");
id = scanner.next();
Iterator<Student> it = studentList.iterator();
while (it.hasNext()) {
Student student = it.next();
if (student.getId().equals(id)) {
System.out.print("请输入新的学生姓名: ");
name = scanner.next();
student.setName(name);
System.out.print("请输入新的学生年龄: ");
age = scanner.nextInt();
student.setAge(age);
System.out.println("修改成功!");
return;
}
}
System.out.println("没有找到该学生!");
}
public void queryStudent(Scanner scanner) {
String id;
System.out.print("请输入要查询学生的学号: ");
id = scanner.next();
Iterator<Student> it = studentList.iterator();
while (it.hasNext()) {
Student student = it.next();
if (student.getId().equals(id)) {
System.out.println(student);
return;
}
}
System.out.println("没有找到该学生!");
}
}
class Student {
private String name;
private String id;
private int age;
public Student(String name, String id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "学生信息: " + name + ", " + id + ", " + age;
}
}
```
请注意,这只是一个基本的实现,可能不适用于所有情况,并且可能需要进行改进和优化。如果您需要更复杂的学生管理系统,请根据您的具体需求进行开发。
阅读全文