学生管理系统的代码
时间: 2023-08-03 21:07:22 浏览: 89
好的,下面给您提供一个基于 Java 面向对象编程思想实现的简单学生管理系统的代码,其中使用文件存储学生信息。这只是一个简单的示例,您可以根据自己的需求和实际情况进行修改和完善。
```java
import java.io.*;
import java.util.*;
// 定义学生类
class Student {
private String id;
private String name;
private String gender;
private int age;
public Student(String id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",性别:" + gender + ",年龄:" + age;
}
}
// 定义学生管理类
class StudentManager {
private List<Student> students;
private String filename;
public StudentManager(String filename) {
this.filename = filename;
students = new ArrayList<>();
load();
}
// 加载学生信息
private void load() {
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
Student student = new Student(data[0], data[1], data[2], Integer.parseInt(data[3]));
students.add(student);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 保存学生信息
private void save() {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
for (Student student : students) {
bw.write(student.getId() + "," + student.getName() + "," + student.getGender() + "," + student.getAge());
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 添加学生信息
public void add(Student student) {
students.add(student);
save();
}
// 删除学生信息
public void remove(String id) {
Iterator<Student> it = students.iterator();
while (it.hasNext()) {
Student student = it.next();
if (student.getId().equals(id)) {
it.remove();
break;
}
}
save();
}
// 修改学生信息
public void update(String id, String name, String gender, int age) {
for (Student student : students) {
if (student.getId().equals(id)) {
student = new Student(id, name, gender, age);
break;
}
}
save();
}
// 查询学生信息
public List<Student> query(String id, String name, String gender, int age) {
List<Student> result = new ArrayList<>();
for (Student student : students) {
if ((id == null || student.getId().equals(id)) &&
(name == null || student.getName().equals(name)) &&
(gender == null || student.getGender().equals(gender)) &&
(age == -1 || student.getAge() == age)) {
result.add(student);
}
}
return result;
}
// 显示学生信息
public void display(List<Student> students) {
if (students.size() == 0) {
System.out.println("没有找到符合条件的学生!");
} else {
for (Student student : students) {
System.out.println(student.toString());
}
}
}
}
// 定义主程序类
public class Main {
public static void main(String[] args) {
StudentManager manager = new StudentManager("students.txt");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:1.添加学生 2.删除学生 3.修改学生 4.查询学生 5.退出");
int option = scanner.nextInt();
switch (option) {
case 1:
System.out.println("请输入学号:");
String id = scanner.next();
System.out.println("请输入姓名:");
String name = scanner.next();
System.out.println("请输入性别:");
String gender = scanner.next();
System.out.println("请输入年龄:");
int age = scanner.nextInt();
Student student = new Student(id, name, gender, age);
manager.add(student);
System.out.println("添加成功!");
break;
case 2:
System.out.println("请输入学号:");
id = scanner.next();
manager.remove(id);
System.out.println("删除成功!");
break;
case 3:
System.out.println("请输入学号:");
id = scanner.next();
System.out.println("请输入姓名:");
name = scanner.next();
System.out.println("请输入性别:");
gender = scanner.next();
System.out.println("请输入年龄:");
age = scanner.nextInt();
manager.update(id, name, gender, age);
System.out.println("修改成功!");
break;
case 4:
System.out.println("请输入学号(不输入则不限制):");
id = scanner.next();
System.out.println("请输入姓名(不输入则不限制):");
name = scanner.next();
System.out.println("请输入性别(不输入则不限制):");
gender = scanner.next();
System.out.println("请输入年龄(不输入则不限制):");
age = scanner.nextInt();
List<Student> students = manager.query(id.equals("") ? null : id, name.equals("") ? null : name, gender.equals("") ? null : gender, age == 0 ? -1 : age);
manager.display(students);
break;
case 5:
System.exit(0);
break;
default:
System.out.println("输入错误,请重新选择!");
}
}
}
}
```
以上代码只是一个简单的示例,可能存在不足和需要改进的地方,仅供参考。
阅读全文