题意整理

题目要求编写个人所得税的计算程序,并且提供了公式
应纳税额 = (工资薪金所得 - 扣除数) x 适用税率 - 速算扣除数
根据该公式和表格,可以联想到使用if-else if进行判断

思路梳理

  1. 根据题意,新建三个employee对象,并且使用有参构造进行赋值,再将其放入集合中,这里为了简化代码一并进行了操作:
        employees.add(new Employee("小明", 2500));
        employees.add(new Employee("小军", 8000));
        employees.add(new Employee("小红", 100000));
  1. 编写所得税计算方法,根据(总收入-3500)为全月应缴税额进行判断

  2. 使用增强for进行打印,可以简化代码

完整代码如下

import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Employee> employees = new ArrayList<>();

        //write your code here......
        employees.add(new Employee("小明", 2500));
        employees.add(new Employee("小军", 8000));
        employees.add(new Employee("小红", 100000));
        
        for (Employee employee : employees) {
            System.out.println(employee.getName() + "应该缴纳的个人所得税是:" + calTax(employee.getSalary()));
        }

    }
    
    public static double calTax(double salary) {
        double tax = 0;
        double taxMoney = salary - 3500;
        if (taxMoney <= 0) {
            tax = 0;
        } else if (taxMoney <= 1500) {
            tax = taxMoney * 0.03 - 0;
        }  else if (taxMoney > 1500 && taxMoney <= 4500) {
            tax = taxMoney * 0.1 - 105;
        } else if (taxMoney > 4500 && taxMoney <= 9000) {
            tax = taxMoney * 0.2 - 555;
        } else if (taxMoney > 9000 && taxMoney <= 35000) {
            tax = taxMoney * 0.25 - 1005;
        } else if (taxMoney > 35000 && taxMoney <= 55000) {
            tax = taxMoney * 0.30 - 2755;
        } else if (taxMoney > 55000 && taxMoney <= 80000) {
            tax = taxMoney * 0.35 - 5505;
        } else if (taxMoney > 80000) {
            tax = taxMoney * 0.45 - 13505;
        }
        return tax;
    }
}
class Employee{
    private String name;
    private double salary;
    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }
    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }
}