请基于Java写出一个简单的人事管理系统
时间: 2023-11-19 14:06:25 浏览: 91
好的,以下是一个简单的人事管理系统的Java代码示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
class Employee {
private int id;
private String name;
private int age;
private String position;
private double salary;
public Employee(int id, String name, int age, String position, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.position = position;
this.salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getPosition() {
return position;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String toString() {
return "ID: " + id + "\nName: " + name + "\nAge: " + age + "\nPosition: " + position + "\nSalary: " + salary;
}
}
public class HRSystem {
private ArrayList<Employee> employees;
public HRSystem() {
employees = new ArrayList<>();
}
public void addEmployee(Employee e) {
employees.add(e);
}
public void removeEmployee(int id) {
for (Employee e : employees) {
if (e.getId() == id) {
employees.remove(e);
break;
}
}
}
public void displayAllEmployees() {
for (Employee e : employees) {
System.out.println(e.toString());
}
}
public void updateEmployeeSalary(int id, double newSalary) {
for (Employee e : employees) {
if (e.getId() == id) {
e.setSalary(newSalary);
break;
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
HRSystem hrSystem = new HRSystem();
while (true) {
System.out.println("1. Add Employee");
System.out.println("2. Remove Employee");
System.out.println("3. Display All Employees");
System.out.println("4. Update Employee Salary");
System.out.println("5. Exit");
System.out.print("\nEnter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter employee ID: ");
int id = scanner.nextInt();
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 position: ");
String position = scanner.next();
System.out.print("Enter employee salary: ");
double salary = scanner.nextDouble();
Employee e = new Employee(id, name, age, position, salary);
hrSystem.addEmployee(e);
System.out.println("\nEmployee added successfully!\n");
break;
case 2:
System.out.print("Enter employee ID: ");
int removeId = scanner.nextInt();
hrSystem.removeEmployee(removeId);
System.out.println("\nEmployee removed successfully!\n");
break;
case 3:
hrSystem.displayAllEmployees();
break;
case 4:
System.out.print("Enter employee ID: ");
int updateId = scanner.nextInt();
System.out.print("Enter new salary: ");
double newSalary = scanner.nextDouble();
hrSystem.updateEmployeeSalary(updateId, newSalary);
System.out.println("\nEmployee salary updated successfully!\n");
break;
case 5:
System.out.println("\nExiting HR System...");
System.exit(0);
default:
System.out.println("\nInvalid choice. Please try again.\n");
}
}
}
}
```
这个人事管理系统可以添加、删除、显示和更新员工信息,包括ID、姓名、年龄、职位和工资。用户可以通过菜单选择操作,所有员工信息被存储在一个ArrayList中。
阅读全文