学生管理系统 学生Student(属性封装){ id:Long唯一 学号:String,唯一 姓名:String 电话:String,唯一 年龄:Integer 性别:Integer、Boolean 爱好:String[] 班级:Class } 班级Class(属性封装){ id:Long唯一 班级名:String } Student、Class增删改查(使用接口、创建接口实现类)(合理使用异常处理) 学生 增(id自增) 删(根据id删除) 改(根据id修改) 查(根据学号查询、姓名模糊查询、查询全部) 班级 增(id自增) 删(根据id删除) 改(根据id修改) 查(根据班级名模糊查询) 导出文档(两个文档 学号+姓名+学生信息 例:22013213+张三+学生信息 学号+姓名+班级信息 例:22013213+张三+班级信息 )
时间: 2024-01-18 12:04:22 浏览: 78
好的,以下是Java编写的学生管理系统,包括学生和班级的增删改查以及导出文档功能。在实现过程中,我们使用了接口和实现类,并合理使用了异常处理。
首先是学生类的代码:
```java
public class Student {
private Long id; // 唯一标识
private String studentId; // 学号
private String name; // 姓名
private String phone; // 电话
private Integer age; // 年龄
private Integer gender; // 性别,0表示女,1表示男
private String[] hobbies; // 爱好
private Class clazz; // 所属班级
// 构造方法
public Student(String studentId, String name, String phone, Integer age, Integer gender, String[] hobbies, Class clazz) {
this.studentId = studentId;
this.name = name;
this.phone = phone;
this.age = age;
this.gender = gender;
this.hobbies = hobbies;
this.clazz = clazz;
}
// 属性的get和set方法
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String[] getHobbies() {
return hobbies;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
public Class getClazz() {
return clazz;
}
public void setClazz(Class clazz) {
this.clazz = clazz;
}
}
```
接下来是班级类的代码:
```java
public class Class {
private Long id; // 唯一标识
private String name; // 班级名
// 构造方法
public Class(String name) {
this.name = name;
}
// 属性的get和set方法
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
```
然后是学生管理系统的接口,包括学生和班级的增删改查以及导出文档功能:
```java
import java.util.List;
public interface StudentManagementSystem {
// 学生增加
void addStudent(Student student) throws Exception;
// 学生删除
void deleteStudent(Long id) throws Exception;
// 学生修改
void updateStudent(Long id, Student newStudent) throws Exception;
// 根据学号查询学生
Student findStudentByStudentId(String studentId) throws Exception;
// 根据姓名模糊查询学生
List<Student> findStudentsByName(String name) throws Exception;
// 查询所有学生
List<Student> findAllStudents() throws Exception;
// 班级增加
void addClass(Class clazz) throws Exception;
// 班级删除
void deleteClass(Long id) throws Exception;
// 班级修改
void updateClass(Long id, Class newClass) throws Exception;
// 根据班级名模糊查询班级
List<Class> findClassesByName(String name) throws Exception;
// 导出学生信息文档
void exportStudentInfo(String filePath) throws Exception;
// 导出班级信息文档
void exportClassInfo(String filePath) throws Exception;
}
```
最后是学生管理系统的实现类,包括学生和班级的增删改查以及导出文档功能的具体实现:
```java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StudentManagementSystemImpl implements StudentManagementSystem {
// 保存学生信息的列表
private List<Student> studentList = new ArrayList<>();
// 保存班级信息的列表
private List<Class> classList = new ArrayList<>();
// 学生id到学生对象的映射
private Map<Long, Student> studentIdToStudent = new HashMap<>();
// 学号到学生对象的映射
private Map<String, Student> studentIdToStudentObject = new HashMap<>();
// 班级id到班级对象的映射
private Map<Long, Class> classIdToClass = new HashMap<>();
// 学生增加
@Override
public void addStudent(Student student) throws Exception {
if (studentIdToStudentObject.containsKey(student.getStudentId())) {
throw new Exception("该学号已存在!");
}
student.setId(generateStudentId());
studentList.add(student);
studentIdToStudent.put(student.getId(), student);
studentIdToStudentObject.put(student.getStudentId(), student);
}
// 学生删除
@Override
public void deleteStudent(Long id) throws Exception {
if (!studentIdToStudent.containsKey(id)) {
throw new Exception("该学生不存在!");
}
Student student = studentIdToStudent.get(id);
studentList.remove(student);
studentIdToStudent.remove(id);
studentIdToStudentObject.remove(student.getStudentId());
}
// 学生修改
@Override
public void updateStudent(Long id, Student newStudent) throws Exception {
if (!studentIdToStudent.containsKey(id)) {
throw new Exception("该学生不存在!");
}
Student oldStudent = studentIdToStudent.get(id);
if (!newStudent.getStudentId().equals(oldStudent.getStudentId())) {
if (studentIdToStudentObject.containsKey(newStudent.getStudentId())) {
throw new Exception("该学号已存在!");
}
studentIdToStudentObject.remove(oldStudent.getStudentId());
studentIdToStudentObject.put(newStudent.getStudentId(), oldStudent);
}
oldStudent.setStudentId(newStudent.getStudentId());
oldStudent.setName(newStudent.getName());
oldStudent.setPhone(newStudent.getPhone());
oldStudent.setAge(newStudent.getAge());
oldStudent.setGender(newStudent.getGender());
oldStudent.setHobbies(newStudent.getHobbies());
oldStudent.setClazz(newStudent.getClazz());
}
// 根据学号查询学生
@Override
public Student findStudentByStudentId(String studentId) throws Exception {
if (!studentIdToStudentObject.containsKey(studentId)) {
throw new Exception("该学号对应的学生不存在!");
}
return studentIdToStudentObject.get(studentId);
}
// 根据姓名模糊查询学生
@Override
public List<Student> findStudentsByName(String name) throws Exception {
List<Student> result = new ArrayList<>();
for (Student student : studentList) {
if (student.getName().contains(name)) {
result.add(student);
}
}
return result;
}
// 查询所有学生
@Override
public List<Student> findAllStudents() throws Exception {
return studentList;
}
// 班级增加
@Override
public void addClass(Class clazz) throws Exception {
clazz.setId(generateClassId());
classList.add(clazz);
classIdToClass.put(clazz.getId(), clazz);
}
// 班级删除
@Override
public void deleteClass(Long id) throws Exception {
if (!classIdToClass.containsKey(id)) {
throw new Exception("该班级不存在!");
}
Class clazz = classIdToClass.get(id);
classList.remove(clazz);
classIdToClass.remove(id);
}
// 班级修改
@Override
public void updateClass(Long id, Class newClass) throws Exception {
if (!classIdToClass.containsKey(id)) {
throw new Exception("该班级不存在!");
}
Class oldClass = classIdToClass.get(id);
oldClass.setName(newClass.getName());
}
// 根据班级名模糊查询班级
@Override
public List<Class> findClassesByName(String name) throws Exception {
List<Class> result = new ArrayList<>();
for (Class clazz : classList) {
if (clazz.getName().contains(name)) {
result.add(clazz);
}
}
return result;
}
// 导出学生信息文档
@Override
public void exportStudentInfo(String filePath) throws Exception {
File file = new File(filePath);
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for (Student student : studentList) {
writer.write(student.getStudentId() + "+" + student.getName() + "+");
writer.write("学号:" + student.getStudentId() + ",姓名:" + student.getName() + ",电话:" + student.getPhone());
writer.newLine();
}
writer.close();
}
// 导出班级信息文档
@Override
public void exportClassInfo(String filePath) throws Exception {
File file = new File(filePath);
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for (Class clazz : classList) {
writer.write(clazz.getName() + "+班级信息");
writer.newLine();
}
writer.close();
}
// 生成学生id
private Long generateStudentId() {
if (studentList.size() == 0) {
return 1L;
} else {
return studentList.get(studentList.size() - 1).getId() + 1;
}
}
// 生成班级id
private Long generateClassId() {
if (classList.size() == 0) {
return 1L;
} else {
return classList.get(classList.size() - 1).getId() + 1;
}
}
}
```
在实现类中,我们使用了列表、映射、文件写入等多种数据结构和I/O操作,来实现学生和班级的增删改查以及导出文档功能。
以上就是Java编写的学生管理系统的全部代码。
阅读全文