java设计一个学生信息管理系统,能够实现学生信息(学号、姓名、性别、民族、年龄、年级、班级、专 业、电话、E-mail)的增加、删除、修改、查询以及更新功能。要将学生信息及其变动情况存储在 out.txt中。
时间: 2023-09-25 15:12:17 浏览: 64
好的,这是一个比较典型的 Java 项目,需要设计类、接口和文件存储等功能。下面是一个简单的实现方案:
1. 学生信息类:Student
```java
public class Student {
private String id; // 学号
private String name; // 姓名
private String gender; // 性别
private String nation; // 民族
private int age; // 年龄
private int grade; // 年级
private int clazz; // 班级
private String major; // 专业
private String phone; // 电话
private String email; // 邮箱
// 构造方法
public Student(String id, String name, String gender, String nation, int age, int grade, int clazz, String major, String phone, String email) {
this.id = id;
this.name = name;
this.gender = gender;
this.nation = nation;
this.age = age;
this.grade = grade;
this.clazz = clazz;
this.major = major;
this.phone = phone;
this.email = email;
}
// getter和setter方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public int getClazz() {
return clazz;
}
public void setClazz(int clazz) {
this.clazz = clazz;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// 重写toString方法,用于输出信息
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", nation='" + nation + '\'' +
", age=" + age +
", grade=" + grade +
", clazz=" + clazz +
", major='" + major + '\'' +
", phone='" + phone + '\'' +
", email='" + email + '\'' +
'}';
}
}
```
2. 学生信息管理接口:StudentManager
```java
import java.util.List;
public interface StudentManager {
/**
* 增加学生信息
* @param student
*/
void addStudent(Student student);
/**
* 删除学生信息
* @param id
*/
void deleteStudent(String id);
/**
* 修改学生信息
* @param student
*/
void updateStudent(Student student);
/**
* 查询学生信息
* @param id
* @return
*/
Student queryStudent(String id);
/**
* 获取所有学生信息
* @return
*/
List<Student> getAllStudents();
/**
* 更新学生信息到文件
*/
void updateToFile();
}
```
3. 学生信息管理实现类:StudentManagerImpl
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class StudentManagerImpl implements StudentManager {
private List<Student> studentList = new ArrayList<>();
private final String FILE_NAME = "out.txt";
// 读取文件中的学生信息
public StudentManagerImpl() {
try {
BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME));
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(",");
Student student = new Student(data[0], data[1], data[2], data[3], Integer.parseInt(data[4]), Integer.parseInt(data[5]), Integer.parseInt(data[6]), data[7], data[8], data[9]);
studentList.add(student);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void addStudent(Student student) {
studentList.add(student);
updateToFile();
}
@Override
public void deleteStudent(String id) {
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).getId().equals(id)) {
studentList.remove(i);
break;
}
}
updateToFile();
}
@Override
public void updateStudent(Student student) {
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).getId().equals(student.getId())) {
studentList.set(i, student);
break;
}
}
updateToFile();
}
@Override
public Student queryStudent(String id) {
for (Student student : studentList) {
if (student.getId().equals(id)) {
return student;
}
}
return null;
}
@Override
public List<Student> getAllStudents() {
return studentList;
}
@Override
public void updateToFile() {
try {
FileWriter writer = new FileWriter(FILE_NAME);
for (Student student : studentList) {
writer.write(student.getId() + "," + student.getName() + "," + student.getGender() + "," + student.getNation() + "," + student.getAge() + "," + student.getGrade() + "," + student.getClazz() + "," + student.getMajor() + "," + student.getPhone() + "," + student.getEmail() + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
4. 主程序:StudentManagementSystem
```java
import java.util.List;
import java.util.Scanner;
public class StudentManagementSystem {
private static StudentManagerImpl studentManager = new StudentManagerImpl();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. 添加学生信息");
System.out.println("2. 删除学生信息");
System.out.println("3. 修改学生信息");
System.out.println("4. 查询学生信息");
System.out.println("5. 显示所有学生信息");
System.out.println("0. 退出程序");
int option = scanner.nextInt();
switch (option) {
case 1:
addStudent(scanner);
break;
case 2:
deleteStudent(scanner);
break;
case 3:
updateStudent(scanner);
break;
case 4:
queryStudent(scanner);
break;
case 5:
showAllStudents();
break;
case 0:
System.exit(0);
default:
System.out.println("无效的选项,请重新输入");
}
}
}
private static void addStudent(Scanner scanner) {
System.out.println("请输入学生信息(学号,姓名,性别,民族,年龄,年级,班级,专业,电话,E-mail):");
String id = scanner.next();
String name = scanner.next();
String gender = scanner.next();
String nation = scanner.next();
int age = scanner.nextInt();
int grade = scanner.nextInt();
int clazz = scanner.nextInt();
String major = scanner.next();
String phone = scanner.next();
String email = scanner.next();
Student student = new Student(id, name, gender, nation, age, grade, clazz, major, phone, email);
studentManager.addStudent(student);
System.out.println("添加成功!");
}
private static void deleteStudent(Scanner scanner) {
System.out.println("请输入学生学号:");
String id = scanner.next();
studentManager.deleteStudent(id);
System.out.println("删除成功!");
}
private static void updateStudent(Scanner scanner) {
System.out.println("请输入学生信息(学号,姓名,性别,民族,年龄,年级,班级,专业,电话,E-mail):");
String id = scanner.next();
String name = scanner.next();
String gender = scanner.next();
String nation = scanner.next();
int age = scanner.nextInt();
int grade = scanner.nextInt();
int clazz = scanner.nextInt();
String major = scanner.next();
String phone = scanner.next();
String email = scanner.next();
Student student = new Student(id, name, gender, nation, age, grade, clazz, major, phone, email);
studentManager.updateStudent(student);
System.out.println("修改成功!");
}
private static void queryStudent(Scanner scanner) {
System.out.println("请输入学生学号:");
String id = scanner.next();
Student student = studentManager.queryStudent(id);
if (student != null) {
System.out.println(student);
} else {
System.out.println("未找到该学生!");
}
}
private static void showAllStudents() {
List<Student> studentList = studentManager.getAllStudents();
for (Student student : studentList) {
System.out.println(student);
}
}
}
```
这样就实现了一个简单的学生信息管理系统,并将学生信息存储在文件 out.txt 中。需要注意的是,这里没有进行输入验证和异常处理等操作,实际应用中需要进行更加严格的处理。
阅读全文