Feel the charm of Java -- a calculation method of after tax salary based on Java two-dimensional array and if statement

1. Thanks

This is my first CSDN blog. As a Java Xiaobai, I want to thank Dane first Fuyuan bubble The teacher's leadership: humorous, funny and easy to understand. Being able to speak such a very rational course as Java so vividly and admirably makes me a good start in java learning.

2. Content and significance of sharing

In this Java practice, with the help of the salary calculation case in the new personal income tax policy, understand how to use two-dimensional array, If statement and for loop to solve the problem of cumulative data calculation. This time, we share the guidance of our predecessors and the common progress of our partners.

3. Problems to be solved in this programming practice

Question Name: after tax salary

2019 Since January 1, the state has introduced a new individual income tax policy, with a value of 5000 yuan on the threshold. In other words, the pre tax salary is deducted from the three insurances and one fund (the amount of the three insurances and one fund is assumed to be 10% of the pre tax salary)%)If it is less than 5000 yuan, no tax will be paid. If it is more than 5000 yuan, the tax will be paid according to the gradient. The specific gradient proportion is as follows:
 1st gear: 0 ~ 3000 3 yuan tax%
 2nd gear: 3000 ~ 12000 For the part of 10 yuan, pay tax%
 3rd gear: 12000 ~ 25000 Tax 20%
 4th gear: 25000 ~ 35000 Tax 25%
 5th gear: 35000 ~ 55000 Tax 30%
 6th gear: 55000 ~ 80000 Tax 35%
 Grade 7: Tax 45 for the part exceeding 80000%
For example, if Xiao Lan's pre tax salary is 18000 after joining an enterprise, his monthly income tax is 18000-1800-5000=11200 The amount of individual income tax is 3000 yuan×3%+8200×10%=910 The after tax salary is 15290 yuan.
Please complete a personal income tax calculation program. After the user enters the pre tax salary, calculate the corresponding tax amount and the post tax salary?

4. Challenges and Countermeasures in the process of realization

The first method is usually used to solve this problem, such as taxCal1(), which is implemented through if statements and simple numerical operation. It is simple and relatively easy to implement. However, it has the disadvantage of weak scalability. If you increase the salary file, you need to add lengthy code. In addition, the internal implementation depends on manual calculation, and the code readability is weak.

What are the opportunities to further optimize the program to simplify our work and reduce the dependence on manual calculation? The problems to be solved show several characteristics:

  • The wages of the six gears have common attributes, such as minimum wage, maximum wage and tax rate, so the for cycle will have opportunities
  • The calculations of the six gears are accumulated on the basis of the above, and are still solved through the for loop and If judgment statement
  • The wage gap of the six gears is different, so it cannot be a simple cycle, and an array may be used.

The above features lead to a computer meeting to build taxCal2(). See the implementation process below for details.

5 implementation process

The following code provides two calculation methods. The first is the common calculation method taxCal1(), and the second is taxCal2(), which is built based on the problem characteristics

package ZVC.Varaible;

import java.util.Scanner;
public class TestTax {
    public static void main(String[] args) {
        System.out.println("Please enter your pre tax salary (yuan):");
        double s = new Scanner(System.in).nextDouble();
        System.out.println("According to the first calculation method:");
        taxCal1(s);
        System.out.println("According to the second calculation method:");
        taxCal2(s);
    }
    public static void taxCal1(double s){
        if((s*0.9-5000) < 0){
            System.out.println("Your after tax salary is:"+(s*0.9));
        }else if((s*0.9-5000) <= 3000){
            System.out.println("Your after tax salary is:"+(s*0.9-(s*0.9-5000)*0.03));
        }else if((s*0.9-5000) <= 12000){
            System.out.println("Your after tax salary is:"+(s*0.9-(s*0.9-8000)*0.1-3000*0.03));
        }else if((s*0.9-5000) <= 25000){
            System.out.println("Your after tax salary is:"+(s*0.9-(s*0.9-17000)*0.2-9000*0.1-3000*0.03));
        }else if((s*0.9-5000) <= 35000){
            System.out.println("Your after tax salary is:"+(s*0.9-(s*0.9-30000)*0.25-13000*0.2-9000*0.1-3000*0.03));
        }else if((s*0.9-5000) <= 55000){
            System.out.println("Your after tax salary is:"+(s*0.9-(s*0.9-40000)*0.3-10000*0.25-13000*0.2-9000*0.1-3000*0.03));
        }else if((s*0.9-5000) <= 80000){
            System.out.println("Your after tax salary is:"+(s*0.9-(s*0.9-60000)*0.35-20000*0.3-10000*0.25-13000*0.2-9000*0.1-3000*0.03));
        }else {
            System.out.println("Your after tax salary is:"+(s*0.9-(s*0.9-85000)*0.45-25000*0.35-20000*0.3-10000*0.25-13000*0.2-9000*0.1-3000*0.03));
        }
    }
    public static void taxCal2(double sBt){
        double[][] sc = {{0,3000,12000,25000,35000,55000,80000},{3000,9000,13000,10000,20000,25000,80000},{0.03,0.10,0.20,0.25,0.30,0.35,0.45}};
        int n = 0;//Initialize gear
        while((n<sc[0].length) && ((sBt*0.9-5000) > (sc[0][n]))){
            n=n + 1;
        };
        double sp = 0;//Middle gear for tax payment (excluding the last gear for tax payment)
        double sAt = 0;//After tax salary
        if (n==0 || ((sBt*0.9-5000)<=0))
        {
            sAt = sBt * 0.9; //Calculate the after tax salary for gear 0
        }else if((sBt*0.9-5000)>0) //Calculate the after tax salary after gear 1
            {
                for (int i = 0; i < n-1; i++)
                {
                    sp = sp + sc[1][i] * sc[2][i];
                }
                sAt = sBt * 0.9 - sp - (sBt * 0.9 - 5000 - sc[0][n-1]) * sc[2][n-1];
        }
        System.out.println("The gear is the second gear" + n + "File.");
        System.out.println("Your after tax salary is"+sAt+"element");
    }
}

6. Interact together

The above code is my first attempt to use a binary array. Please criticize and correct any mistakes. What do you think about the implementation of this process and the use of methods?

7. About myself

My name is haizhiguizi. I'm not a returnee, so I hope I can return to the breadth and inclusiveness of the sea. I'm an enterprise management consultant, engaged in excellent operation management consulting, aiming to support organizations and people to break through system constraints and bloom life. I love programming. It allows me to modularize my management experience and give me the opportunity to better simplify people's life Work to help people better create internal and external value.
Create the future together: Kevin yuan@zenithvue.com

Tags: Java Back-end

Posted on Fri, 12 Nov 2021 23:19:15 -0500 by djpeterlewis