Java数组实现员工管理系统:增、删、改、查功能
以下是基于数组存储的员工管理系统的Java代码,实现了增加、查找、修改和删除员工的功能。输出语句为中文,方便用户理解。
import java.util.Scanner;
public class EmployeeManagementSystem {
private static final int MAX_EMPLOYEES = 100; // 最大员工数
private static Employee[] employees = new Employee[MAX_EMPLOYEES]; // 员工数组
private static int employeeCount = 0; // 当前员工数
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean exit = false; // 是否退出系统
while (!exit) {
System.out.println('请选择操作:');
System.out.println('1. 增加员工');
System.out.println('2. 查找员工');
System.out.println('3. 修改员工');
System.out.println('4. 删除员工');
System.out.println('5. 退出系统');
int option = scanner.nextInt();
switch (option) {
case 1:
addEmployee(scanner);
break;
case 2:
findEmployee(scanner);
break;
case 3:
updateEmployee(scanner);
break;
case 4:
deleteEmployee(scanner);
break;
case 5:
exit = true;
break;
default:
System.out.println('无效操作,请重新选择。');
break;
}
}
}
// 增加员工
private static void addEmployee(Scanner scanner) {
if (employeeCount >= MAX_EMPLOYEES) {
System.out.println('员工数量已达上限,无法继续增加。');
return;
}
System.out.println('请输入员工姓名:');
String name = scanner.next();
System.out.println('请输入员工工号:');
int id = scanner.nextInt();
System.out.println('请输入员工职位:');
String position = scanner.next();
System.out.println('请输入员工工资:');
double salary = scanner.nextDouble();
Employee employee = new Employee(name, id, position, salary);
employees[employeeCount] = employee;
employeeCount++;
System.out.println('员工添加成功。');
}
// 查找员工
private static void findEmployee(Scanner scanner) {
System.out.println('请输入要查找的员工工号:');
int id = scanner.nextInt();
for (int i = 0; i < employeeCount; i++) {
Employee employee = employees[i];
if (employee.getId() == id) {
System.out.println('员工姓名:' + employee.getName());
System.out.println('员工工号:' + employee.getId());
System.out.println('员工职位:' + employee.getPosition());
System.out.println('员工工资:' + employee.getSalary());
return;
}
}
System.out.println('未找到该员工。');
}
// 修改员工
private static void updateEmployee(Scanner scanner) {
System.out.println('请输入要修改的员工工号:');
int id = scanner.nextInt();
for (int i = 0; i < employeeCount; i++) {
Employee employee = employees[i];
if (employee.getId() == id) {
System.out.println('请输入员工姓名(原值:' + employee.getName() + '):');
String name = scanner.next();
System.out.println('请输入员工职位(原值:' + employee.getPosition() + '):');
String position = scanner.next();
System.out.println('请输入员工工资(原值:' + employee.getSalary() + '):');
double salary = scanner.nextDouble();
employee.setName(name);
employee.setPosition(position);
employee.setSalary(salary);
System.out.println('员工修改成功。');
return;
}
}
System.out.println('未找到该员工。');
}
// 删除员工
private static void deleteEmployee(Scanner scanner) {
System.out.println('请输入要删除的员工工号:');
int id = scanner.nextInt();
for (int i = 0; i < employeeCount; i++) {
if (employees[i].getId() == id) {
for (int j = i + 1; j < employeeCount; j++) {
employees[j - 1] = employees[j];
}
employees[employeeCount - 1] = null;
employeeCount--;
System.out.println('员工删除成功。');
return;
}
}
System.out.println('未找到该员工。');
}
}
class Employee {
private String name; // 姓名
private int id; // 工号
private String position; // 职位
private double salary; // 工资
public Employee(String name, int id, String position, double salary) {
this.name = name;
this.id = id;
this.position = position;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
本代码使用了类和对象的概念,将员工信息封装在Employee类中,定义了增加、查找、修改和删除员工的方法,并且使用了数组存储员工数据。通过命令行输入,实现了简单的交互式操作。
原文地址: http://www.cveoy.top/t/topic/osSM 著作权归作者所有。请勿转载和采集!