帮我写一个简单的人事管理信息系统的java代码,能运行的
时间: 2024-03-21 10:41:59 浏览: 59
java编写的人事管理系统
好的,这是一个简单的人事管理信息系统的Java代码,它包括员工信息的添加、修改、删除和查询功能。请注意,这只是一个示例代码,可能需要根据具体需求进行修改。
```java
import java.util.ArrayList;
import java.util.Scanner;
class Employee {
private String name;
private int age;
private String gender;
private String department;
public Employee(String name, int age, String gender, String department) {
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
return "Name: " + name + ", Age: " + age + ", Gender: " + gender + ", Department: " + department;
}
}
class EmployeeManagementSystem {
private ArrayList<Employee> employees = new ArrayList<>();
public void addEmployee() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter employee name: ");
String name = scanner.next();
System.out.print("Enter employee age: ");
int age = scanner.nextInt();
System.out.print("Enter employee gender: ");
String gender = scanner.next();
System.out.print("Enter employee department: ");
String department = scanner.next();
Employee employee = new Employee(name, age, gender, department);
employees.add(employee);
System.out.println("Employee added successfully.");
}
public void updateEmployee() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter employee name to update: ");
String name = scanner.next();
boolean found = false;
for (Employee employee : employees) {
if (employee.getName().equals(name)) {
System.out.print("Enter new employee age: ");
int age = scanner.nextInt();
System.out.print("Enter new employee gender: ");
String gender = scanner.next();
System.out.print("Enter new employee department: ");
String department = scanner.next();
employee.setAge(age);
employee.setGender(gender);
employee.setDepartment(department);
System.out.println("Employee updated successfully.");
found = true;
break;
}
}
if (!found) {
System.out.println("Employee not found.");
}
}
public void deleteEmployee() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter employee name to delete: ");
String name = scanner.next();
boolean found = false;
for (Employee employee : employees) {
if (employee.getName().equals(name)) {
employees.remove(employee);
System.out.println("Employee deleted successfully.");
found = true;
break;
}
}
if (!found) {
System.out.println("Employee not found.");
}
}
public void searchEmployee() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter employee name to search: ");
String name = scanner.next();
boolean found = false;
for (Employee employee : employees) {
if (employee.getName().equals(name)) {
System.out.println(employee);
found = true;
break;
}
}
if (!found) {
System.out.println("Employee not found.");
}
}
public void listEmployees() {
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
public class Main {
public static void main(String[] args) {
EmployeeManagementSystem ems = new EmployeeManagementSystem();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println();
System.out.println("1. Add employee");
System.out.println("2. Update employee");
System.out.println("3. Delete employee");
System.out.println("4. Search employee");
System.out.println("5. List employees");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
ems.addEmployee();
break;
case 2:
ems.updateEmployee();
break;
case 3:
ems.deleteEmployee();
break;
case 4:
ems.searchEmployee();
break;
case 5:
ems.listEmployees();
break;
case 0:
System.exit(0);
default:
System.out.println("Invalid choice.");
}
}
}
}
```
这个程序包括一个 `Employee` 类来表示员工信息,以及一个 `EmployeeManagementSystem` 类来管理所有员工信息。在 `main` 函数中,我们创建了一个 `EmployeeManagementSystem` 对象,并提供了添加、修改、删除、查找和列出员工信息的选项。
阅读全文