写出Java语言创建一个学生信息管理系统的代码
时间: 2023-09-29 15:11:01 浏览: 75
用java写的学生信息管理系统
以下是一个简单的Java语言创建学生信息管理系统的示例代码:
Student.java
```java
public class Student {
private String name;
private int id;
private double mathScore;
private double englishScore;
private double computerScore;
public Student(String name, int id, double mathScore, double englishScore, double computerScore) {
this.name = name;
this.id = id;
this.mathScore = mathScore;
this.englishScore = englishScore;
this.computerScore = computerScore;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public double getMathScore() {
return mathScore;
}
public double getEnglishScore() {
return englishScore;
}
public double getComputerScore() {
return computerScore;
}
public double getTotalScore() {
return mathScore + englishScore + computerScore;
}
public double getAverageScore() {
return getTotalScore() / 3;
}
}
```
StudentManager.java
```java
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class StudentManager {
private List<Student> studentList;
private String fileName;
public StudentManager(String fileName) {
this.fileName = fileName;
studentList = new ArrayList<>();
loadFromFile();
}
public void addStudent(String name, int id, double mathScore, double englishScore, double computerScore) {
Student student = new Student(name, id, mathScore, englishScore, computerScore);
studentList.add(student);
saveToFile();
}
public void deleteStudent(int id) {
for (Student student : studentList) {
if (student.getId() == id) {
studentList.remove(student);
saveToFile();
return;
}
}
}
public void updateStudent(int id, String name, double mathScore, double englishScore, double computerScore) {
for (Student student : studentList) {
if (student.getId() == id) {
studentList.remove(student);
Student newStudent = new Student(name, id, mathScore, englishScore, computerScore);
studentList.add(newStudent);
saveToFile();
return;
}
}
}
public List<Student> queryStudentByName(String name) {
List<Student> result = new ArrayList<>();
for (Student student : studentList) {
if (student.getName().equals(name)) {
result.add(student);
}
}
return result;
}
public List<Student> queryStudentByScore() {
Collections.sort(studentList, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return Double.compare(o2.getTotalScore(), o1.getTotalScore());
}
});
return studentList;
}
public void printStudents() {
for (Student student : studentList) {
System.out.println(student.getId() + " " + student.getName() + " " + student.getMathScore() + " " + student.getEnglishScore() + " " + student.getComputerScore() + " " + student.getTotalScore() + " " + student.getAverageScore());
}
}
private void loadFromFile() {
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 5) {
String name = parts[0];
int id = Integer.parseInt(parts[1]);
double mathScore = Double.parseDouble(parts[2]);
double englishScore = Double.parseDouble(parts[3]);
double computerScore = Double.parseDouble(parts[4]);
Student student = new Student(name, id, mathScore, englishScore, computerScore);
studentList.add(student);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void saveToFile() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
for (Student student : studentList) {
String line = student.getName() + "," + student.getId() + "," + student.getMathScore() + "," + student.getEnglishScore() + "," + student.getComputerScore();
writer.write(line);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
Main.java
```java
import java.util.List;
import java.util.Scanner;
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("请选择操作");
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("0. 退出");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入学号:");
int id = scanner.nextInt();
System.out.print("请输入数学成绩:");
double mathScore = scanner.nextDouble();
System.out.print("请输入英语成绩:");
double englishScore = scanner.nextDouble();
System.out.print("请输入计算机成绩:");
double computerScore = scanner.nextDouble();
manager.addStudent(name, id, mathScore, englishScore, computerScore);
break;
case 2:
System.out.print("请输入学号:");
id = scanner.nextInt();
manager.deleteStudent(id);
break;
case 3:
System.out.print("请输入学号:");
id = scanner.nextInt();
scanner.nextLine();
System.out.print("请输入姓名:");
name = scanner.nextLine();
System.out.print("请输入数学成绩:");
mathScore = scanner.nextDouble();
System.out.print("请输入英语成绩:");
englishScore = scanner.nextDouble();
System.out.print("请输入计算机成绩:");
computerScore = scanner.nextDouble();
manager.updateStudent(id, name, mathScore, englishScore, computerScore);
break;
case 4:
System.out.print("请输入姓名:");
name = scanner.nextLine();
List<Student> students = manager.queryStudentByName(name);
for (Student student : students) {
System.out.println(student.getId() + " " + student.getName() + " " + student.getMathScore() + " " + student.getEnglishScore() + " " + student.getComputerScore() + " " + student.getTotalScore() + " " + student.getAverageScore());
}
break;
case 5:
students = manager.queryStudentByScore();
for (Student student : students) {
System.out.println(student.getId() + " " + student.getName() + " " + student.getMathScore() + " " + student.getEnglishScore() + " " + student.getComputerScore() + " " + student.getTotalScore() + " " + student.getAverageScore());
}
break;
case 6:
manager.printStudents();
break;
case 0:
return;
default:
System.out.println("输入错误,请重新输入");
break;
}
}
}
}
```
这个示例代码实现了基本的学生信息管理系统功能,包括添加、删除、修改学生信息,查询学生信息,计算学生的总成绩、平均成绩、排名等,将学生信息存储到文件中。您可以根据自己的需求进行修改和扩展。
阅读全文