控制台工资管理系统

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);
    }
}
相关推荐
一直学习永不止步几秒前
LeetCode题练习与总结:赎金信--383
java·数据结构·算法·leetcode·字符串·哈希表·计数
兆。几秒前
掌握 PyQt5:从零开始的桌面应用开发
开发语言·爬虫·python·qt
尘浮生3 分钟前
Java项目实战II基于Spring Boot的光影视频平台(开发文档+数据库+源码)
java·开发语言·数据库·spring boot·后端·maven·intellij-idea
尚学教辅学习资料11 分钟前
基于SpringBoot的医药管理系统+LW示例参考
java·spring boot·后端·java毕业设计·医药管理
明月看潮生23 分钟前
青少年编程与数学 02-003 Go语言网络编程 15课题、Go语言URL编程
开发语言·网络·青少年编程·golang·编程与数学
雷神乐乐27 分钟前
File.separator与File.separatorChar的区别
java·路径分隔符
小刘|32 分钟前
《Java 实现希尔排序:原理剖析与代码详解》
java·算法·排序算法
南宫理的日知录34 分钟前
99、Python并发编程:多线程的问题、临界资源以及同步机制
开发语言·python·学习·编程学习
逊嘘1 小时前
【Java语言】抽象类与接口
java·开发语言·jvm