以下是一个简单的员工管理系统的 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 numOfEmployees = 0;
    private static final String ADMIN_USERNAME = 'admin';
    private static final String ADMIN_PASSWORD = 'password';

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String username, password;
        System.out.print('Enter username: ');
        username = scanner.nextLine();
        System.out.print('Enter password: ');
        password = scanner.nextLine();

        if (login(username, password)) {
            while (true) {
                System.out.println('1. Add employee');
                System.out.println('2. Delete employee');
                System.out.println('3. Update employee');
                System.out.println('4. View employee');
                System.out.println('5. Exit');
                System.out.print('Enter your choice: ');
                int choice = scanner.nextInt();

                switch (choice) {
                    case 1:
                        addEmployee();
                        break;
                    case 2:
                        deleteEmployee();
                        break;
                    case 3:
                        updateEmployee();
                        break;
                    case 4:
                        viewEmployee();
                        break;
                    case 5:
                        System.exit(0);
                    default:
                        System.out.println('Invalid choice.');
                }
            }
        } else {
            System.out.println('Invalid username or password.');
        }
    }

    private static boolean login(String username, String password) {
        return username.equals(ADMIN_USERNAME) && password.equals(ADMIN_PASSWORD);
    }

    private static void addEmployee() {
        Scanner scanner = new Scanner(System.in);
        if (numOfEmployees >= MAX_EMPLOYEES) {
            System.out.println('Cannot add more employees.');
            return;
        }
        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 salary: ');
        double salary = scanner.nextDouble();
        employees[numOfEmployees++] = new Employee(id, name, salary);
        System.out.println('Employee added successfully.');
    }

    private static void deleteEmployee() {
        Scanner scanner = new Scanner(System.in);
        System.out.print('Enter employee ID: ');
        int id = scanner.nextInt();
        int index = findEmployeeIndex(id);
        if (index == -1) {
            System.out.println('Employee not found.');
            return;
        }
        for (int i = index; i < numOfEmployees - 1; i++) {
            employees[i] = employees[i + 1];
        }
        numOfEmployees--;
        System.out.println('Employee deleted successfully.');
    }

    private static void updateEmployee() {
        Scanner scanner = new Scanner(System.in);
        System.out.print('Enter employee ID: ');
        int id = scanner.nextInt();
        int index = findEmployeeIndex(id);
        if (index == -1) {
            System.out.println('Employee not found.');
            return;
        }
        System.out.print('Enter new employee name: ');
        String name = scanner.nextLine();
        employees[index].setName(name);
        System.out.print('Enter new employee salary: ');
        double salary = scanner.nextDouble();
        employees[index].setSalary(salary);
        System.out.println('Employee updated successfully.');
    }

    private static void viewEmployee() {
        Scanner scanner = new Scanner(System.in);
        System.out.print('Enter employee ID: ');
        int id = scanner.nextInt();
        int index = findEmployeeIndex(id);
        if (index == -1) {
            System.out.println('Employee not found.');
            return;
        }
        System.out.println('ID: ' + employees[index].getId());
        System.out.println('Name: ' + employees[index].getName());
        System.out.println('Salary: ' + employees[index].getSalary());
    }

    private static int findEmployeeIndex(int id) {
        for (int i = 0; i < numOfEmployees; i++) {
            if (employees[i].getId() == id) {
                return i;
            }
        }
        return -1;
    }
}

class Employee {
    private int id;
    private String name;
    private double salary;

    public Employee(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

注意,此代码仅作为示例,实际使用时可能需要增加更多的输入验证和错误处理。

Java 员工管理系统:增删改查功能实现

原文地址: https://www.cveoy.top/t/topic/osI8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录