控制台工资管理系统

bash 复制代码
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

// 员工类
class Employee {
    private String id;
    private String name;
    private String department;
    private double salary;

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

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDepartment() {
        return department;
    }

    public double getSalary() {
        return salary;
    }

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

    public void setDepartment(String department) {
        this.department = department;
    }

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

    @Override
    public String toString() {
        return "👤 员工信息 {" +
                "工号='" + id + '\'' +
                ", 姓名='" + name + '\'' +
                ", 部门='" + department + '\'' +
                ", 工资=¥" + salary +
                '}';
    }
}

// 工资管理系统类
public class SalaryManagementSystem {
    private static List<Employee> employees = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        login(); // 登录
        boolean exit = false;
        while (!exit) {
            displayMenu();
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline left-over

            switch (choice) {
                case 1:
                    addEmployee();
                    break;
                case 2:
                    deleteEmployee();
                    break;
                case 3:
                    updateEmployee();
                    break;
                case 4:
                    queryEmployee();
                    break;
                case 5:
                    showStatistics();
                    break;
                case 6:
                    exit = true;
                    break;
                case 7:
                    sortEmployeesBySalary();
                    break;
                // 显示所有员工信息
                case 8:
                    displayAllEmployees();
                    break;
                default:
                    System.out.println("❌ 请输入有效的选项!");
                    break;
            }
        }
        System.out.println("👋 感谢使用工资管理系统!");
    }
    private static void sortEmployeesBySalary() {
        if (employees.isEmpty()) {
            System.out.println("⚠️ 当前没有员工信息!");
            return;
        }

        employees.sort(Comparator.comparingDouble(Employee::getSalary));

        System.out.println("✨ 员工信息按工资升序排序:");
        for (Employee emp : employees) {
            System.out.println(emp);
        }
    }
    // 显示所有员工信息
    private static void displayAllEmployees() {
        if (employees.isEmpty()) {
            System.out.println("⚠️ 当前没有员工信息!");
            return;
        }

        System.out.println("✨ 所有员工信息:");
        for (Employee emp : employees) {
            System.out.println(emp);
        }
    }
    // 登录功能,这里简单模拟,可以根据实际情况扩展
    private static void login() {
        System.out.println("=== 欢迎使用工资管理系统 ===");
        while (true) {
            System.out.print("🔑 请输入用户名:");
            String username = scanner.nextLine();
            System.out.print("🔑 请输入密码:");
            String password = scanner.nextLine();

            // 简单模拟用户名和密码
            if (username.equals("admin") && password.equals("admin")) {
                System.out.println("✅ 登录成功!");
                break;
            } else {
                System.out.println("❌ 用户名或密码错误,请重试!");
            }
        }
    }

    // 显示菜单
    private static void displayMenu() {
        System.out.println("\n请选择操作:");
        System.out.println("1. ➕ 增加员工信息");
        System.out.println("2. ➖ 删除员工信息");
        System.out.println("3. ✏️ 修改员工信息");
        System.out.println("4. 🔍 查询员工信息");
        System.out.println("5. 📊 查看统计信息");
        System.out.println("6. 退出系统");
        System.out.println("7. 🔄 按工资排序员工信息");
        System.out.println("8. 📜 显示所有员工信息");
        System.out.print("请输入数字选择操作:");
    }

    // 添加员工信息
    private static void addEmployee() {
        System.out.print("请输入员工工号:");
        String id = scanner.nextLine();
        System.out.print("请输入员工姓名:");
        String name = scanner.nextLine();
        System.out.print("请输入员工部门:");
        String department = scanner.nextLine();
        System.out.print("请输入员工工资:");
        double salary = scanner.nextDouble();
        scanner.nextLine(); // Consume newline left-over

        Employee newEmployee = new Employee(id, name, department, salary);
        employees.add(newEmployee);
        System.out.println("✅ 员工信息添加成功!");
    }

    // 删除员工信息
    private static void deleteEmployee() {
        System.out.print("请输入要删除的员工工号:");
        String id = scanner.nextLine();

        boolean found = false;
        for (Employee emp : employees) {
            if (emp.getId().equals(id)) {
                employees.remove(emp);
                System.out.println("✅ 员工信息删除成功!");
                found = true;
                break;
            }
        }

        if (!found) {
            System.out.println("❌ 未找到该员工!");
        }
    }

    // 修改员工信息
    private static void updateEmployee() {
        System.out.print("请输入要修改的员工工号:");
        String id = scanner.nextLine();

        boolean found = false;
        for (Employee emp : employees) {
            if (emp.getId().equals(id)) {
                System.out.print("请输入新的员工姓名:");
                String newName = scanner.nextLine();
                System.out.print("请输入新的员工部门:");
                String newDept = scanner.nextLine();
                System.out.print("请输入新的员工工资:");
                double newSalary = scanner.nextDouble();
                scanner.nextLine(); // Consume newline left-over

                emp.setName(newName);
                emp.setDepartment(newDept);
                emp.setSalary(newSalary);
                System.out.println("✅ 员工信息修改成功!");
                found = true;
                break;
            }
        }

        if (!found) {
            System.out.println("❌ 未找到该员工!");
        }
    }

    // 查询员工信息
    private static void queryEmployee() {
        System.out.print("请输入要查询的员工工号:");
        String id = scanner.nextLine();

        boolean found = false;
        for (Employee emp : employees) {
            if (emp.getId().equals(id)) {
                System.out.println(emp);
                found = true;
                break;
            }
        }

        if (!found) {
            System.out.println("❌ 未找到该员工!");
        }
    }

    // 查看统计信息
    private static void showStatistics() {
        if (employees.isEmpty()) {
            System.out.println("⚠️ 当前没有员工信息!");
            return;
        }

        double totalSalary = 0;
        double maxSalary = Double.MIN_VALUE;
        double minSalary = Double.MAX_VALUE;

        for (Employee emp : employees) {
            double salary = emp.getSalary();
            totalSalary += salary;
            if (salary > maxSalary) {
                maxSalary = salary;
            }
            if (salary < minSalary) {
                minSalary = salary;
            }
        }

        double averageSalary = totalSalary / employees.size();
        System.out.println("📊 统计信息:");
        System.out.println("总员工人数:" + employees.size());
        System.out.println("平均工资:¥" + averageSalary);
        System.out.println("最高工资:¥" + maxSalary);
        System.out.println("最低工资:¥" + minSalary);
    }
}
相关推荐
Swift社区3 小时前
在 Swift 中实现字符串分割问题:以字典中的单词构造句子
开发语言·ios·swift
没头脑的ht3 小时前
Swift内存访问冲突
开发语言·ios·swift
没头脑的ht3 小时前
Swift闭包的本质
开发语言·ios·swift
wjs20243 小时前
Swift 数组
开发语言
吾日三省吾码4 小时前
JVM 性能调优
java
stm 学习ing4 小时前
FPGA 第十讲 避免latch的产生
c语言·开发语言·单片机·嵌入式硬件·fpga开发·fpga
湫ccc5 小时前
《Python基础》之字符串格式化输出
开发语言·python
弗拉唐5 小时前
springBoot,mp,ssm整合案例
java·spring boot·mybatis
oi776 小时前
使用itextpdf进行pdf模版填充中文文本时部分字不显示问题
java·服务器