java设计一个简单的学校人事管理系统,管理教师和学生信息。实现教师与学生基本信息的添加、删除、修改、查询。其中编号、姓名、性别、出生日期为共有的,教师包括部门、职称、工资;学生包括高考分数、专业field、班级等。
时间: 2023-07-04 13:03:19 浏览: 98
以下是一个简单的学校人事管理系统的Java代码示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class SchoolManagementSystem {
ArrayList<Teacher> teachers = new ArrayList<Teacher>();
ArrayList<Student> students = new ArrayList<Student>();
Scanner scanner = new Scanner(System.in);
// 添加教师
public void addTeacher() {
System.out.println("请输入教师编号:");
int id = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入教师姓名:");
String name = scanner.nextLine();
System.out.println("请输入教师性别:");
String gender = scanner.nextLine();
System.out.println("请输入教师出生日期:");
String birthDate = scanner.nextLine();
System.out.println("请输入教师部门:");
String department = scanner.nextLine();
System.out.println("请输入教师职称:");
String title = scanner.nextLine();
System.out.println("请输入教师工资:");
double salary = scanner.nextDouble();
Teacher teacher = new Teacher(id, name, gender, birthDate, department, title, salary);
teachers.add(teacher);
System.out.println("教师添加成功!");
}
// 添加学生
public void addStudent() {
System.out.println("请输入学生编号:");
int id = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生性别:");
String gender = scanner.nextLine();
System.out.println("请输入学生出生日期:");
String birthDate = scanner.nextLine();
System.out.println("请输入学生高考分数:");
double score = scanner.nextDouble();
scanner.nextLine();
System.out.println("请输入学生专业:");
String field = scanner.nextLine();
System.out.println("请输入学生班级:");
String className = scanner.nextLine();
Student student = new Student(id, name, gender, birthDate, score, field, className);
students.add(student);
System.out.println("学生添加成功!");
}
// 删除教师
public void deleteTeacher() {
System.out.println("请输入要删除的教师编号:");
int id = scanner.nextInt();
for (int i = 0; i < teachers.size(); i++) {
if (teachers.get(i).getId() == id) {
teachers.remove(i);
System.out.println("教师删除成功!");
return;
}
}
System.out.println("找不到编号为" + id + "的教师!");
}
// 删除学生
public void deleteStudent() {
System.out.println("请输入要删除的学生编号:");
int id = scanner.nextInt();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() == id) {
students.remove(i);
System.out.println("学生删除成功!");
return;
}
}
System.out.println("找不到编号为" + id + "的学生!");
}
// 修改教师信息
public void updateTeacher() {
System.out.println("请输入要修改的教师编号:");
int id = scanner.nextInt();
for (int i = 0; i < teachers.size(); i++) {
if (teachers.get(i).getId() == id) {
scanner.nextLine();
System.out.println("请输入教师姓名(不修改请直接回车):");
String name = scanner.nextLine();
if (!name.equals("")) {
teachers.get(i).setName(name);
}
System.out.println("请输入教师性别(不修改请直接回车):");
String gender = scanner.nextLine();
if (!gender.equals("")) {
teachers.get(i).setGender(gender);
}
System.out.println("请输入教师出生日期(不修改请直接回车):");
String birthDate = scanner.nextLine();
if (!birthDate.equals("")) {
teachers.get(i).setBirthDate(birthDate);
}
System.out.println("请输入教师部门(不修改请直接回车):");
String department = scanner.nextLine();
if (!department.equals("")) {
teachers.get(i).setDepartment(department);
}
System.out.println("请输入教师职称(不修改请直接回车):");
String title = scanner.nextLine();
if (!title.equals("")) {
teachers.get(i).setTitle(title);
}
System.out.println("请输入教师工资(不修改请直接回车):");
String salaryStr = scanner.nextLine();
if (!salaryStr.equals("")) {
double salary = Double.parseDouble(salaryStr);
teachers.get(i).setSalary(salary);
}
System.out.println("教师信息修改成功!");
return;
}
}
System.out.println("找不到编号为" + id + "的教师!");
}
// 修改学生信息
public void updateStudent() {
System.out.println("请输入要修改的学生编号:");
int id = scanner.nextInt();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() == id) {
scanner.nextLine();
System.out.println("请输入学生姓名(不修改请直接回车):");
String name = scanner.nextLine();
if (!name.equals("")) {
students.get(i).setName(name);
}
System.out.println("请输入学生性别(不修改请直接回车):");
String gender = scanner.nextLine();
if (!gender.equals("")) {
students.get(i).setGender(gender);
}
System.out.println("请输入学生出生日期(不修改请直接回车):");
String birthDate = scanner.nextLine();
if (!birthDate.equals("")) {
students.get(i).setBirthDate(birthDate);
}
System.out.println("请输入学生高考分数(不修改请直接回车):");
String scoreStr = scanner.nextLine();
if (!scoreStr.equals("")) {
double score = Double.parseDouble(scoreStr);
students.get(i).setScore(score);
}
System.out.println("请输入学生专业(不修改请直接回车):");
String field = scanner.nextLine();
if (!field.equals("")) {
students.get(i).setField(field);
}
System.out.println("请输入学生班级(不修改请直接回车):");
String className = scanner.nextLine();
if (!className.equals("")) {
students.get(i).setClassName(className);
}
System.out.println("学生信息修改成功!");
return;
}
}
System.out.println("找不到编号为" + id + "的学生!");
}
// 查询教师信息
public void queryTeacher() {
System.out.println("请输入要查询的教师编号:");
int id = scanner.nextInt();
for (Teacher teacher : teachers) {
if (teacher.getId() == id) {
System.out.println(teacher);
return;
}
}
System.out.println("找不到编号为" + id + "的教师!");
}
// 查询学生信息
public void queryStudent() {
System.out.println("请输入要查询的学生编号:");
int id = scanner.nextInt();
for (Student student : students) {
if (student.getId() == id) {
System.out.println(student);
return;
}
}
System.out.println("找不到编号为" + id + "的学生!");
}
// 显示菜单
public void showMenu() {
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. 退出系统");
}
// 运行系统
public void run() {
while (true) {
showMenu();
int choice = scanner.nextInt();
switch (choice) {
case 1:
addTeacher();
break;
case 2:
addStudent();
break;
case 3:
deleteTeacher();
break;
case 4:
deleteStudent();
break;
case 5:
updateTeacher();
break;
case 6:
updateStudent();
break;
case 7:
queryTeacher();
break;
case 8:
queryStudent();
break;
case 0:
System.out.println("谢谢使用!");
return;
default:
System.out.println("无效的选择!");
}
}
}
public static void main(String[] args) {
SchoolManagementSystem system = new SchoolManagementSystem();
system.run();
}
}
class Person {
private int id;
private String name;
private String gender;
private String birthDate;
public Person(int id, String name, String gender, String birthDate) {
this.id = id;
this.name = name;
this.gender = gender;
this.birthDate = birthDate;
}
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;
}
@Override
public String toString() {
return "编号:" + id +
",姓名:" + name +
",性别:" + gender +
",出生日期:" + birthDate;
}
}
class Teacher extends Person {
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) {
super(id, name, gender, birthDate);
this.department = department;
this.title = title;
this.salary = salary;
}
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;
}
@Override
public String toString() {
return super.toString() +
",部门:" + department +
",职称:" + title +
",工资:" + salary;
}
}
class Student extends Person {
private double score;
private String field;
private String className;
public Student(int id, String name, String gender, String birthDate, double score, String field, String className) {
super(id, name, gender, birthDate);
this.score = score;
this.field = field;
this.className = className;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return super.toString() +
",高考分数:" + score +
",专业:" + field +
",班级:" + className;
}
}
```
运行程序后,可以根据提示进行教师和学生信息的添加、删除、修改、查询。
阅读全文