java实现员工管理系统
时间: 2023-09-22 07:10:55 浏览: 32
下面是一个简单的员工管理系统的 Java 实现,包含了添加、删除、查询和修改员工信息的功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String toString() {
return "ID: " + id + ", Name: " + name + ", Age: " + age + ", Salary: " + salary;
}
}
public class EmployeeManagementSystem {
private ArrayList<Employee> employees;
public EmployeeManagementSystem() {
employees = new ArrayList<Employee>();
}
public void addEmployee(Employee employee) {
employees.add(employee);
}
public void removeEmployee(int id) {
for (Employee e: employees) {
if (e.getId() == id) {
employees.remove(e);
break;
}
}
}
public Employee findEmployee(int id) {
for (Employee e: employees) {
if (e.getId() == id) {
return e;
}
}
return null;
}
public void updateEmployeeSalary(int id, double salary) {
for (Employee e: employees) {
if (e.getId() == id) {
e.setSalary(salary);
break;
}
}
}
public void printAllEmployees() {
for (Employee e: employees) {
System.out.println(e);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
EmployeeManagementSystem ems = new EmployeeManagementSystem();
while (true) {
System.out.println("1. Add employee");
System.out.println("2. Remove employee");
System.out.println("3. Find employee");
System.out.println("4. Update employee salary");
System.out.println("5. Print all employees");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter employee ID: ");
int id = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter employee name: ");
String name = scanner.nextLine();
System.out.print("Enter employee age: ");
int age = scanner.nextInt();
System.out.print("Enter employee salary: ");
double salary = scanner.nextDouble();
ems.addEmployee(new Employee(id, name, age, salary));
break;
case 2:
System.out.print("Enter employee ID: ");
id = scanner.nextInt();
ems.removeEmployee(id);
break;
case 3:
System.out.print("Enter employee ID: ");
id = scanner.nextInt();
Employee e = ems.findEmployee(id);
if (e != null) {
System.out.println(e);
} else {
System.out.println("Employee not found.");
}
break;
case 4:
System.out.print("Enter employee ID: ");
id = scanner.nextInt();
System.out.print("Enter new salary: ");
salary = scanner.nextDouble();
ems.updateEmployeeSalary(id, salary);
break;
case 5:
ems.printAllEmployees();
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Invalid choice.");
break;
}
}
}
}
```
该程序使用了 ArrayList 来存储所有员工的信息,包含了添加、删除、查询和修改员工信息的功能,并且提供了一个简单的命令行界面供用户操作。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)