用java设计一个简单的学校人事管理系统,管理教师和学生信息。实现教师与学生基本信息的添加、删除、修改、查询。其中编号、姓名、性别、出生日期为共有的,教师包括部门、职称、工资;学生包括高考分数、专业field、班级等。
时间: 2023-10-20 10:06:07 浏览: 214
基于JAVA的学生信息管理系统.rar
5星 · 资源好评率100%
好的,以下是一个简单的学校人事管理系统的Java代码:
```java
import java.util.ArrayList;
import java.util.Scanner;
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 String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getBirthdate() {
return birthdate;
}
public void setName(String name) {
this.name = name;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Gender: " + gender + ", Birthdate: " + 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 String getTitle() {
return title;
}
public double getSalary() {
return salary;
}
public void setDepartment(String department) {
this.department = department;
}
public void setTitle(String title) {
this.title = title;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return super.toString() + ", Department: " + department + ", Title: " + title + ", Salary: " + salary;
}
}
class Student extends Person {
private double entranceExamScore;
private String field;
private String className;
public Student(int id, String name, String gender, String birthdate, double entranceExamScore, String field, String className) {
super(id, name, gender, birthdate);
this.entranceExamScore = entranceExamScore;
this.field = field;
this.className = className;
}
public double getEntranceExamScore() {
return entranceExamScore;
}
public String getField() {
return field;
}
public String getClassName() {
return className;
}
public void setEntranceExamScore(double entranceExamScore) {
this.entranceExamScore = entranceExamScore;
}
public void setField(String field) {
this.field = field;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return super.toString() + ", Entrance Exam Score: " + entranceExamScore + ", Field: " + field + ", Class Name: " + className;
}
}
public class SchoolManagementSystem {
private static ArrayList<Teacher> teachers = new ArrayList<>();
private static ArrayList<Student> students = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
private static void addTeacher() {
System.out.println("Enter teacher information:");
System.out.print("ID: ");
int id = scanner.nextInt();
scanner.nextLine();
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Gender: ");
String gender = scanner.nextLine();
System.out.print("Birthdate: ");
String birthdate = scanner.nextLine();
System.out.print("Department: ");
String department = scanner.nextLine();
System.out.print("Title: ");
String title = scanner.nextLine();
System.out.print("Salary: ");
double salary = scanner.nextDouble();
Teacher teacher = new Teacher(id, name, gender, birthdate, department, title, salary);
teachers.add(teacher);
System.out.println("Teacher added successfully!");
}
private static void addStudent() {
System.out.println("Enter student information:");
System.out.print("ID: ");
int id = scanner.nextInt();
scanner.nextLine();
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Gender: ");
String gender = scanner.nextLine();
System.out.print("Birthdate: ");
String birthdate = scanner.nextLine();
System.out.print("Entrance Exam Score: ");
double entranceExamScore = scanner.nextDouble();
scanner.nextLine();
System.out.print("Field: ");
String field = scanner.nextLine();
System.out.print("Class Name: ");
String className = scanner.nextLine();
Student student = new Student(id, name, gender, birthdate, entranceExamScore, field, className);
students.add(student);
System.out.println("Student added successfully!");
}
private static void deleteTeacher() {
System.out.print("Enter teacher ID to delete: ");
int id = scanner.nextInt();
boolean isDeleted = false;
for (Teacher teacher : teachers) {
if (teacher.getId() == id) {
teachers.remove(teacher);
isDeleted = true;
break;
}
}
if (isDeleted) {
System.out.println("Teacher deleted successfully!");
} else {
System.out.println("Teacher not found!");
}
}
private static void deleteStudent() {
System.out.print("Enter student ID to delete: ");
int id = scanner.nextInt();
boolean isDeleted = false;
for (Student student : students) {
if (student.getId() == id) {
students.remove(student);
isDeleted = true;
break;
}
}
if (isDeleted) {
System.out.println("Student deleted successfully!");
} else {
System.out.println("Student not found!");
}
}
private static void modifyTeacher() {
System.out.print("Enter teacher ID to modify: ");
int id = scanner.nextInt();
boolean isModified = false;
for (Teacher teacher : teachers) {
if (teacher.getId() == id) {
System.out.println("Enter new teacher information:");
scanner.nextLine();
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Gender: ");
String gender = scanner.nextLine();
System.out.print("Birthdate: ");
String birthdate = scanner.nextLine();
System.out.print("Department: ");
String department = scanner.nextLine();
System.out.print("Title: ");
String title = scanner.nextLine();
System.out.print("Salary: ");
double salary = scanner.nextDouble();
teacher.setName(name);
teacher.setGender(gender);
teacher.setBirthdate(birthdate);
teacher.setDepartment(department);
teacher.setTitle(title);
teacher.setSalary(salary);
isModified = true;
break;
}
}
if (isModified) {
System.out.println("Teacher modified successfully!");
} else {
System.out.println("Teacher not found!");
}
}
private static void modifyStudent() {
System.out.print("Enter student ID to modify: ");
int id = scanner.nextInt();
boolean isModified = false;
for (Student student : students) {
if (student.getId() == id) {
System.out.println("Enter new student information:");
scanner.nextLine();
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Gender: ");
String gender = scanner.nextLine();
System.out.print("Birthdate: ");
String birthdate = scanner.nextLine();
System.out.print("Entrance Exam Score: ");
double entranceExamScore = scanner.nextDouble();
scanner.nextLine();
System.out.print("Field: ");
String field = scanner.nextLine();
System.out.print("Class Name: ");
String className = scanner.nextLine();
student.setName(name);
student.setGender(gender);
student.setBirthdate(birthdate);
student.setEntranceExamScore(entranceExamScore);
student.setField(field);
student.setClassName(className);
isModified = true;
break;
}
}
if (isModified) {
System.out.println("Student modified successfully!");
} else {
System.out.println("Student not found!");
}
}
private static void searchTeacher() {
System.out.print("Enter teacher ID to search: ");
int id = scanner.nextInt();
boolean isFound = false;
for (Teacher teacher : teachers) {
if (teacher.getId() == id) {
System.out.println(teacher);
isFound = true;
break;
}
}
if (!isFound) {
System.out.println("Teacher not found!");
}
}
private static void searchStudent() {
System.out.print("Enter student ID to search: ");
int id = scanner.nextInt();
boolean isFound = false;
for (Student student : students) {
if (student.getId() == id) {
System.out.println(student);
isFound = true;
break;
}
}
if (!isFound) {
System.out.println("Student not found!");
}
}
private static void displayAllTeachers() {
for (Teacher teacher : teachers) {
System.out.println(teacher);
}
}
private static void displayAllStudents() {
for (Student student : students) {
System.out.println(student);
}
}
public static void main(String[] args) {
System.out.println("Welcome to School Management System!");
while (true) {
System.out.println("1. Add Teacher\n2. Add Student\n3. Delete Teacher\n4. Delete Student\n5. Modify Teacher\n6. Modify Student\n7. Search Teacher\n8. Search Student\n9. Display All Teachers\n10. Display All Students\n11. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addTeacher();
break;
case 2:
addStudent();
break;
case 3:
deleteTeacher();
break;
case 4:
deleteStudent();
break;
case 5:
modifyTeacher();
break;
case 6:
modifyStudent();
break;
case 7:
searchTeacher();
break;
case 8:
searchStudent();
break;
case 9:
displayAllTeachers();
break;
case 10:
displayAllStudents();
break;
case 11:
System.out.println("Thank you for using School Management System!");
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
break;
}
}
}
}
```
这个系统使用了面向对象的编程风格,定义了一个 `Person` 类作为教师和学生的基类,然后分别定义了 `Teacher` 和 `Student` 类。在 `SchoolManagementSystem` 类中,我们使用了一个 `ArrayList` 来存储教师和学生的信息,并实现了添加、删除、修改、查询和显示所有教师和学生的功能。用户可以通过在控制台输入不同的选项来执行不同的操作。
阅读全文