我需要java代码设计一个简单的学校人事管理系统,管理教师和学生信息。实现教师与学生基本信息的添加、删除、修改、查询。其中编号、姓名、性别、出生日期为共有的,教师包括部门、职称、工资;学生包括高考分数、专业field、班级等。
时间: 2023-10-20 15:26:25 浏览: 81
好的,以下是一个简单的学校人事管理系统的 Java 代码:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class SchoolManagementSystem {
public static void main(String[] args) {
ArrayList<Teacher> teachers = new ArrayList<>();
ArrayList<Student> students = new ArrayList<>();
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("7. 修改学生信息");
System.out.println("8. 查询学生信息");
System.out.println("0. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addTeacher(teachers, scanner);
break;
case 2:
deleteTeacher(teachers, scanner);
break;
case 3:
updateTeacher(teachers, scanner);
break;
case 4:
queryTeacher(teachers, scanner);
break;
case 5:
addStudent(students, scanner);
break;
case 6:
deleteStudent(students, scanner);
break;
case 7:
updateStudent(students, scanner);
break;
case 8:
queryStudent(students, scanner);
break;
case 0:
System.exit(0);
default:
System.out.println("无效的选择,请重新输入!");
}
}
}
private static void addTeacher(ArrayList<Teacher> teachers, Scanner scanner) {
System.out.println("请输入教师编号:");
int id = scanner.nextInt();
System.out.println("请输入教师姓名:");
String name = scanner.next();
System.out.println("请输入教师性别:");
String gender = scanner.next();
System.out.println("请输入教师出生日期:");
String birthdate = scanner.next();
System.out.println("请输入教师部门:");
String department = scanner.next();
System.out.println("请输入教师职称:");
String title = scanner.next();
System.out.println("请输入教师工资:");
double salary = scanner.nextDouble();
Teacher teacher = new Teacher(id, name, gender, birthdate, department, title, salary);
teachers.add(teacher);
System.out.println("教师添加成功!");
}
private static void deleteTeacher(ArrayList<Teacher> teachers, Scanner scanner) {
System.out.println("请输入要删除的教师编号:");
int id = scanner.nextInt();
for (Teacher teacher : teachers) {
if (teacher.getId() == id) {
teachers.remove(teacher);
System.out.println("教师删除成功!");
return;
}
}
System.out.println("未找到指定编号的教师!");
}
private static void updateTeacher(ArrayList<Teacher> teachers, Scanner scanner) {
System.out.println("请输入要修改的教师编号:");
int id = scanner.nextInt();
for (Teacher teacher : teachers) {
if (teacher.getId() == id) {
System.out.println("请输入教师姓名(留空表示不修改):");
String name = scanner.nextLine().trim();
if (!name.isEmpty()) {
teacher.setName(name);
}
System.out.println("请输入教师性别(留空表示不修改):");
String gender = scanner.nextLine().trim();
if (!gender.isEmpty()) {
teacher.setGender(gender);
}
System.out.println("请输入教师出生日期(留空表示不修改):");
String birthdate = scanner.nextLine().trim();
if (!birthdate.isEmpty()) {
teacher.setBirthdate(birthdate);
}
System.out.println("请输入教师部门(留空表示不修改):");
String department = scanner.nextLine().trim();
if (!department.isEmpty()) {
teacher.setDepartment(department);
}
System.out.println("请输入教师职称(留空表示不修改):");
String title = scanner.nextLine().trim();
if (!title.isEmpty()) {
teacher.setTitle(title);
}
System.out.println("请输入教师工资(留空表示不修改):");
String salaryStr = scanner.nextLine().trim();
if (!salaryStr.isEmpty()) {
double salary = Double.parseDouble(salaryStr);
teacher.setSalary(salary);
}
System.out.println("教师信息修改成功!");
return;
}
}
System.out.println("未找到指定编号的教师!");
}
private static void queryTeacher(ArrayList<Teacher> teachers, Scanner scanner) {
System.out.println("请输入要查询的教师编号:");
int id = scanner.nextInt();
for (Teacher teacher : teachers) {
if (teacher.getId() == id) {
System.out.println("教师编号:" + teacher.getId());
System.out.println("教师姓名:" + teacher.getName());
System.out.println("教师性别:" + teacher.getGender());
System.out.println("教师出生日期:" + teacher.getBirthdate());
System.out.println("教师部门:" + teacher.getDepartment());
System.out.println("教师职称:" + teacher.getTitle());
System.out.println("教师工资:" + teacher.getSalary());
return;
}
}
System.out.println("未找到指定编号的教师!");
}
private static void addStudent(ArrayList<Student> students, Scanner scanner) {
System.out.println("请输入学生编号:");
int id = scanner.nextInt();
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生性别:");
String gender = scanner.next();
System.out.println("请输入学生出生日期:");
String birthdate = scanner.next();
System.out.println("请输入学生高考分数:");
int score = scanner.nextInt();
System.out.println("请输入学生专业field:");
String field = scanner.next();
System.out.println("请输入学生班级:");
String clazz = scanner.next();
Student student = new Student(id, name, gender, birthdate, score, field, clazz);
students.add(student);
System.out.println("学生添加成功!");
}
private static void deleteStudent(ArrayList<Student> students, Scanner scanner) {
System.out.println("请输入要删除的学生编号:");
int id = scanner.nextInt();
for (Student student : students) {
if (student.getId() == id) {
students.remove(student);
System.out.println("学生删除成功!");
return;
}
}
System.out.println("未找到指定编号的学生!");
}
private static void updateStudent(ArrayList<Student> students, Scanner scanner) {
System.out.println("请输入要修改的学生编号:");
int id = scanner.nextInt();
for (Student student : students) {
if (student.getId() == id) {
System.out.println("请输入学生姓名(留空表示不修改):");
String name = scanner.nextLine().trim();
if (!name.isEmpty()) {
student.setName(name);
}
System.out.println("请输入学生性别(留空表示不修改):");
String gender = scanner.nextLine().trim();
if (!gender.isEmpty()) {
student.setGender(gender);
}
System.out.println("请输入学生出生日期(留空表示不修改):");
String birthdate = scanner.nextLine().trim();
if (!birthdate.isEmpty()) {
student.setBirthdate(birthdate);
}
System.out.println("请输入学生高考分数(留空表示不修改):");
String scoreStr = scanner.nextLine().trim();
if (!scoreStr.isEmpty()) {
int score = Integer.parseInt(scoreStr);
student.setScore(score);
}
System.out.println("请输入学生专业field(留空表示不修改):");
String field = scanner.nextLine().trim();
if (!field.isEmpty()) {
student.setField(field);
}
System.out.println("请输入学生班级(留空表示不修改):");
String clazz = scanner.nextLine().trim();
if (!clazz.isEmpty()) {
student.setClazz(clazz);
}
System.out.println("学生信息修改成功!");
return;
}
}
System.out.println("未找到指定编号的学生!");
}
private static void queryStudent(ArrayList<Student> students, Scanner scanner) {
System.out.println("请输入要查询的学生编号:");
int id = scanner.nextInt();
for (Student student : students) {
if (student.getId() == id) {
System.out.println("学生编号:" + student.getId());
System.out.println("学生姓名:" + student.getName());
System.out.println("学生性别:" + student.getGender());
System.out.println("学生出生日期:" + student.getBirthdate());
System.out.println("学生高考分数:" + student.getScore());
System.out.println("学生专业field:" + student.getField());
System.out.println("学生班级:" + student.getClazz());
return;
}
}
System.out.println("未找到指定编号的学生!");
}
}
class Teacher {
private int id;
private String name;
private String gender;
private String birthdate;
private String department;
private String title;
private double salary;
public Teacher(int id, String name, String gender, String birthdate, String department, String title, double salary) {
this.id = id;
this.name = name;
this.gender = gender;
this.birthdate = birthdate;
this.department = department;
this.title = title;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int 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 getBirthdate() {
return birthdate;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
class Student {
private int id;
private String name;
private String gender;
private String birthdate;
private int score;
private String field;
private String clazz;
public Student(int id, String name, String gender, String birthdate, int score, String field, String clazz) {
this.id = id;
this.name = name;
this.gender = gender;
this.birthdate = birthdate;
this.score = score;
this.field = field;
this.clazz = clazz;
}
public int getId() {
return id;
}
public void setId(int 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 getBirthdate() {
return birthdate;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
}
```
在代码中,我们定义了两个类 `Teacher` 和 `Student` 来表示教师和学生。每个类都有一些属性(编号、姓名、性别、出生日期等)和对应的 getter 和 setter 方法。我们使用两个 `ArrayList` 对象来存储教师和学生信息,并在 `main` 方法中实现了添加、删除、修改和查询教师和学生信息的功能。
阅读全文