Java薪资计算案例怎么开发

wen java案例 28

本文目录导读:

Java薪资计算案例怎么开发

  1. 完整的Java薪资计算系统
  2. 项目结构建议
  3. 扩展功能建议

我将帮助你开发一个Java薪资计算案例,这个案例将涵盖基本的薪资计算逻辑,包括基本工资、加班费、扣除项等。

完整的Java薪资计算系统

员工类(基础模型)

// Employee.java
public class Employee {
    private int id;
    private String name;
    private String department;
    private double basicSalary;  // 基本工资
    private double hourlyRate;   // 时薪
    private double overtimeHours; // 加班小时数
    private double absentDays;   // 缺勤天数
    private double bonus;        // 奖金
    private double insurance;    // 保险扣款
    private double tax;          // 税额
    public Employee() {}
    public Employee(int id, String name, String department, double basicSalary) {
        this.id = id;
        this.name = name;
        this.department = department;
        this.basicSalary = basicSalary;
        this.hourlyRate = basicSalary / 21.75 / 8; // 按标准工作日计算时薪
    }
    // Getters and Setters
    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 getDepartment() { return department; }
    public void setDepartment(String department) { this.department = department; }
    public double getBasicSalary() { return basicSalary; }
    public void setBasicSalary(double basicSalary) {
        this.basicSalary = basicSalary;
        this.hourlyRate = basicSalary / 21.75 / 8;
    }
    public double getHourlyRate() { return hourlyRate; }
    public double getOvertimeHours() { return overtimeHours; }
    public void setOvertimeHours(double overtimeHours) { 
        this.overtimeHours = overtimeHours; 
    }
    public double getAbsentDays() { return absentDays; }
    public void setAbsentDays(double absentDays) { this.absentDays = absentDays; }
    public double getBonus() { return bonus; }
    public void setBonus(double bonus) { this.bonus = bonus; }
    public double getInsurance() { return insurance; }
    public void setInsurance(double insurance) { this.insurance = insurance; }
    public double getTax() { return tax; }
    public void setTax(double tax) { this.tax = tax; }
    @Override
    public String toString() {
        return String.format("员工ID: %d, 姓名: %s, 部门: %s, 基本工资: %.2f", 
            id, name, department, basicSalary);
    }
}

薪资计算器类

// SalaryCalculator.java
import java.time.LocalDate;
public class SalaryCalculator {
    // 税率等级
    private static final double TAX_THRESHOLD = 5000; // 起征点
    private static final double SOCIAL_SECURITY_RATE = 0.105; // 社保比例
    /**
     * 计算最终薪资
     */
    public SalaryResult calculateSalary(Employee employee) {
        SalaryResult result = new SalaryResult();
        result.setEmployee(employee);
        // 1. 计算应发工资
        double grossSalary = calculateGrossSalary(employee);
        result.setGrossSalary(grossSalary);
        // 2. 计算加班费
        double overtimePay = calculateOvertimePay(employee);
        result.setOvertimePay(overtimePay);
        // 3. 计算缺勤扣款
        double absentDeduction = calculateAbsentDeduction(employee);
        result.setAbsentDeduction(absentDeduction);
        // 4. 计算社保扣款
        double socialSecurity = calculateSocialSecurity(employee, grossSalary);
        result.setSocialSecurity(socialSecurity);
        // 5. 计算个人所得税
        double tax = calculateTax(grossSalary - socialSecurity);
        result.setTax(tax);
        // 6. 计算实发工资
        double netSalary = grossSalary + overtimePay - absentDeduction - socialSecurity - tax;
        result.setNetSalary(netSalary);
        return result;
    }
    /**
     * 计算应发工资(基本工资 + 奖金)
     */
    private double calculateGrossSalary(Employee employee) {
        return employee.getBasicSalary() + employee.getBonus();
    }
    /**
     * 计算加班费
     * 工作日加班1.5倍,周末加班2倍,节假日加班3倍
     */
    private double calculateOvertimePay(Employee employee) {
        double overtimeHours = employee.getOvertimeHours();
        double hourlyRate = employee.getHourlyRate();
        // 简化计算:假设所有加班都是工作日1.5倍
        return overtimeHours * hourlyRate * 1.5;
    }
    /**
     * 计算缺勤扣款
     */
    private double calculateAbsentDeduction(Employee employee) {
        double dailyRate = employee.getBasicSalary() / 21.75; // 日工资
        return employee.getAbsentDays() * dailyRate;
    }
    /**
     * 计算社保扣款(养老、医疗、失业等)
     */
    private double calculateSocialSecurity(Employee employee, double grossSalary) {
        double socialSecurity = grossSalary * SOCIAL_SECURITY_RATE;
        // 社保基数上下限(简化处理)
        double minBase = 3000;
        double maxBase = 20000;
        if (socialSecurity < minBase * SOCIAL_SECURITY_RATE) {
            socialSecurity = minBase * SOCIAL_SECURITY_RATE;
        } else if (socialSecurity > maxBase * SOCIAL_SECURITY_RATE) {
            socialSecurity = maxBase * SOCIAL_SECURITY_RATE;
        }
        return Math.round(socialSecurity * 100.0) / 100.0;
    }
    /**
     * 计算个人所得税(使用累进税率)
     */
    private double calculateTax(double taxableIncome) {
        double tax = 0;
        double taxableAmount = taxableIncome - TAX_THRESHOLD;
        if (taxableAmount <= 0) {
            return 0;
        }
        // 累进税率计算
        if (taxableAmount <= 3000) {
            tax = taxableAmount * 0.03;
        } else if (taxableAmount <= 12000) {
            tax = taxableAmount * 0.1 - 210;
        } else if (taxableAmount <= 25000) {
            tax = taxableAmount * 0.2 - 1410;
        } else if (taxableAmount <= 35000) {
            tax = taxableAmount * 0.25 - 2660;
        } else if (taxableAmount <= 55000) {
            tax = taxableAmount * 0.3 - 4410;
        } else if (taxableAmount <= 80000) {
            tax = taxableAmount * 0.35 - 7160;
        } else {
            tax = taxableAmount * 0.45 - 15160;
        }
        return Math.round(tax * 100.0) / 100.0;
    }
}

薪资结果类

// SalaryResult.java
public class SalaryResult {
    private Employee employee;
    private double grossSalary;       // 应发工资
    private double overtimePay;       // 加班费
    private double absentDeduction;   // 缺勤扣款
    private double socialSecurity;    // 社保扣款
    private double tax;              // 个人所得税
    private double netSalary;        // 实发工资
    // Getters and Setters
    public Employee getEmployee() { return employee; }
    public void setEmployee(Employee employee) { this.employee = employee; }
    public double getGrossSalary() { return grossSalary; }
    public void setGrossSalary(double grossSalary) { this.grossSalary = grossSalary; }
    public double getOvertimePay() { return overtimePay; }
    public void setOvertimePay(double overtimePay) { this.overtimePay = overtimePay; }
    public double getAbsentDeduction() { return absentDeduction; }
    public void setAbsentDeduction(double absentDeduction) { this.absentDeduction = absentDeduction; }
    public double getSocialSecurity() { return socialSecurity; }
    public void setSocialSecurity(double socialSecurity) { this.socialSecurity = socialSecurity; }
    public double getTax() { return tax; }
    public void setTax(double tax) { this.tax = tax; }
    public double getNetSalary() { return netSalary; }
    public void setNetSalary(double netSalary) { this.netSalary = netSalary; }
    public void printSalarySlip() {
        System.out.println("=================================");
        System.out.println("          工资单");
        System.out.println("=================================");
        System.out.println("员工: " + employee.getName());
        System.out.println("部门: " + employee.getDepartment());
        System.out.println("---------------------------------");
        System.out.printf("基本工资:      %.2f\n", employee.getBasicSalary());
        System.out.printf("奖金:          %.2f\n", employee.getBonus());
        System.out.printf("加班费:        %.2f\n", overtimePay);
        System.out.printf("缺勤扣款:     -%.2f\n", absentDeduction);
        System.out.println("---------------------------------");
        System.out.printf("社保扣款:     -%.2f\n", socialSecurity);
        System.out.printf("个人所得税:   -%.2f\n", tax);
        System.out.println("---------------------------------");
        System.out.printf("实发工资:      %.2f\n", netSalary);
        System.out.println("=================================");
    }
}

计算器UI界面(可选)

// SalaryCalculatorUI.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SalaryCalculatorUI extends JFrame {
    private JTextField nameField, departmentField, basicField;
    private JTextField overtimeField, absentField, bonusField;
    private JTextArea resultArea;
    private SalaryCalculator calculator;
    public SalaryCalculatorUI() {
        calculator = new SalaryCalculator();
        initializeUI();
    }
    private void initializeUI() {
        setTitle("薪资计算系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout(10, 10));
        // 输入面板
        JPanel inputPanel = new JPanel(new GridLayout(7, 2, 5, 5));
        inputPanel.setBorder(BorderFactory.createTitledBorder("员工信息输入"));
        inputPanel.add(new JLabel("姓名:"));
        nameField = new JTextField();
        inputPanel.add(nameField);
        inputPanel.add(new JLabel("部门:"));
        departmentField = new JTextField();
        inputPanel.add(departmentField);
        inputPanel.add(new JLabel("基本工资:"));
        basicField = new JTextField();
        inputPanel.add(basicField);
        inputPanel.add(new JLabel("加班小时数:"));
        overtimeField = new JTextField("0");
        inputPanel.add(overtimeField);
        inputPanel.add(new JLabel("缺勤天数:"));
        absentField = new JTextField("0");
        inputPanel.add(absentField);
        inputPanel.add(new JLabel("奖金:"));
        bonusField = new JTextField("0");
        inputPanel.add(bonusField);
        JButton calculateBtn = new JButton("计算薪资");
        JButton clearBtn = new JButton("清空");
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(calculateBtn);
        buttonPanel.add(clearBtn);
        // 结果显示
        resultArea = new JTextArea(15, 40);
        resultArea.setEditable(false);
        resultArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
        JScrollPane scrollPane = new JScrollPane(resultArea);
        scrollPane.setBorder(BorderFactory.createTitledBorder("薪资明细"));
        // 添加事件
        calculateBtn.addActionListener(e -> calculate());
        clearBtn.addActionListener(e -> clearFields());
        // 布局
        add(inputPanel, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.CENTER);
        add(scrollPane, BorderLayout.SOUTH);
        pack();
        setLocationRelativeTo(null);
    }
    private void calculate() {
        try {
            Employee emp = new Employee();
            emp.setName(nameField.getText());
            emp.setDepartment(departmentField.getText());
            emp.setBasicSalary(Double.parseDouble(basicField.getText()));
            emp.setOvertimeHours(Double.parseDouble(overtimeField.getText()));
            emp.setAbsentDays(Double.parseDouble(absentField.getText()));
            emp.setBonus(Double.parseDouble(bonusField.getText()));
            SalaryResult result = calculator.calculateSalary(emp);
            // 显示结果
            StringBuilder sb = new StringBuilder();
            sb.append("=================================\n");
            sb.append("          工资单\n");
            sb.append("=================================\n");
            sb.append(String.format("员工: %s\n", emp.getName()));
            sb.append(String.format("部门: %s\n", emp.getDepartment()));
            sb.append("---------------------------------\n");
            sb.append(String.format("基本工资:      %.2f\n", emp.getBasicSalary()));
            sb.append(String.format("奖金:          %.2f\n", emp.getBonus()));
            sb.append(String.format("加班费:        %.2f\n", result.getOvertimePay()));
            sb.append(String.format("缺勤扣款:     -%.2f\n", result.getAbsentDeduction()));
            sb.append("---------------------------------\n");
            sb.append(String.format("社保扣款:     -%.2f\n", result.getSocialSecurity()));
            sb.append(String.format("个人所得税:   -%.2f\n", result.getTax()));
            sb.append("---------------------------------\n");
            sb.append(String.format("实发工资:      %.2f\n", result.getNetSalary()));
            sb.append("=================================");
            resultArea.setText(sb.toString());
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "请输入有效的数字!", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
    private void clearFields() {
        nameField.setText("");
        departmentField.setText("");
        basicField.setText("");
        overtimeField.setText("0");
        absentField.setText("0");
        bonusField.setText("0");
        resultArea.setText("");
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new SalaryCalculatorUI().setVisible(true);
        });
    }
}

主程序入口

// Main.java
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        SalaryCalculator calculator = new SalaryCalculator();
        System.out.println("=== 薪资计算系统 ===");
        System.out.println("请选择模式:");
        System.out.println("1. 命令行模式");
        System.out.println("2. 图形界面模式");
        System.out.print("请输入选择 (1或2): ");
        int choice = scanner.nextInt();
        if (choice == 1) {
            runCommandLine(scanner);
        } else if (choice == 2) {
            runGUI();
        } else {
            System.out.println("无效选择!");
        }
        scanner.close();
    }
    private static void runCommandLine(Scanner scanner) {
        System.out.print("请输入员工姓名: ");
        String name = scanner.next();
        System.out.print("请输入部门: ");
        String department = scanner.next();
        System.out.print("请输入基本工资: ");
        double basicSalary = scanner.nextDouble();
        Employee emp = new Employee(1, name, department, basicSalary);
        System.out.print("请输入加班小时数: ");
        emp.setOvertimeHours(scanner.nextDouble());
        System.out.print("请输入缺勤天数: ");
        emp.setAbsentDays(scanner.nextDouble());
        System.out.print("请输入奖金: ");
        emp.setBonus(scanner.nextDouble());
        SalaryCalculator calculator = new SalaryCalculator();
        SalaryResult result = calculator.calculateSalary(emp);
        result.printSalarySlip();
    }
    private static void runGUI() {
        SalaryCalculatorUI.main(null);
    }
}

项目结构建议

salary-calculator/
├── src/
│   ├── Employee.java
│   ├── SalaryCalculator.java
│   ├── SalaryResult.java
│   ├── SalaryCalculatorUI.java
│   └── Main.java
├── lib/
└── README.md

扩展功能建议

  1. 数据库存储:使用JDBC连接数据库,存储员工信息和薪资记录
  2. 批量计算:支持多员工批量计算
  3. 报表导出:导出为Excel或PDF格式
  4. 公司配置:支持自定义税率、社保比例等参数
  5. 年度汇总:统计年度薪资总和

这个案例涵盖了面向对象设计、算法实现和基本的UI开发,适合作为学习Java开发的项目,你可以根据需要添加更多功能。

抱歉,评论功能暂时关闭!