员工管理系统Java代码实现
时间: 2023-12-07 18:04:11 浏览: 50
以下是一个基本的员工管理系统的Java代码实现,其中包括了员工信息的增删改查等功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class EmployeeManagementSystem {
static ArrayList<Employee> employees = new ArrayList<Employee>();
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("请选择操作:1.添加员工 2.删除员工 3.修改员工信息 4.查询员工信息 5.退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addEmployee();
break;
case 2:
deleteEmployee();
break;
case 3:
updateEmployee();
break;
case 4:
queryEmployee();
break;
case 5:
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
// 添加员工
public static void addEmployee() {
System.out.println("请输入员工编号:");
int id = scanner.nextInt();
System.out.println("请输入员工姓名:");
String name = scanner.next();
System.out.println("请输入员工性别:");
String gender = scanner.next();
System.out.println("请输入员工年龄:");
int age = scanner.nextInt();
System.out.println("请输入员工职位:");
String position = scanner.next();
Employee employee = new Employee(id, name, gender, age, position);
employees.add(employee);
System.out.println("添加成功!");
}
// 删除员工
public static void deleteEmployee() {
System.out.println("请输入要删除的员工编号:");
int id = scanner.nextInt();
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).getId() == id) {
employees.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("没有找到该员工!");
}
// 修改员工信息
public static void updateEmployee() {
System.out.println("请输入要修改的员工编号:");
int id = scanner.nextInt();
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).getId() == id) {
System.out.println("请输入员工姓名:");
String name = scanner.next();
System.out.println("请输入员工性别:");
String gender = scanner.next();
System.out.println("请输入员工年龄:");
int age = scanner.nextInt();
System.out.println("请输入员工职位:");
String position = scanner.next();
Employee employee = new Employee(id, name, gender, age, position);
employees.set(i, employee);
System.out.println("修改成功!");
return;
}
}
System.out.println("没有找到该员工!");
}
// 查询员工信息
public static void queryEmployee() {
System.out.println("请输入要查询的员工编号:");
int id = scanner.nextInt();
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).getId() == id) {
System.out.println(employees.get(i));
return;
}
}
System.out.println("没有找到该员工!");
}
}
class Employee {
private int id;
private String name;
private String gender;
private int age;
private String position;
public Employee(int id, String name, String gender, int age, String position) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.position = position;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
@Override
public String toString() {
return "员工编号:" + id + ",员工姓名:" + name + ",员工性别:" + gender + ",员工年龄:" + age + ",员工职位:" + position;
}
}
```
运行程序后,可以依次选择进行添加员工、删除员工、修改员工信息、查询员工信息等操作。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)