We would like to calculate the taxes someone owes (using a VERY simplified and version of the US tax system). In this system, the following chart shows how much tax someone owes based on their income and filing status (single or married):
We are going to focus on writing a function that does this task. We can write tests for that function that verify all the different categories of filer (status/income) work as expected. Once that function is working, we could easily place it into a program that asked the user for their income and status.
Subsection7.12.2Understanding the Problem And Tests
Before we start writing code, we need to understand the problem. A good way to make sure we do is to solve it by hand. In this case, there is not one solution - each combination of status and income will have a different solution. We can start by solving a few of the cases by hand:
If we are single and make $5,000, we should pay 10%. That is \(0.10 \cdot 5000 = 500\)
Anytime that we have a complex problem, It is a good idea to solve it in steps. For math problems this means doing individual parts of the calculation so we can check those results before combining the results. For a problem involving complex selection it means only worrying about one test at a time. Here we need to decide whether to worry about the status or income first. The status seems simpler (there are only two categories) and more determinative (single and married filers use completely different tables), so we will start there.
We wonβt worry about the function returning the correct value yet, we will just print out if each test case represents a filing that is single or married. Try running this version. The output is a little hard to read, but you should be able to find ***SINGLE*** in two spots and then one ***MARRIED***.
Once part B is finished, take a moment to look at the structure of the conditions for the income. Note that:
Because else if only runs if the previous tests in the chain have failed, we do not need to state something like income > 30000 && income <= 70000. The only way we reach the 70000 test is if the income is not <= 30000.
Complete the Married section. Paste your Single code from above into this problem, then fix the two ??? areas of the program. Here is a link if you need to look back at the Married Tax Table.
Of course, there are other ways we could write the code. But they would almost all involve more repeating ourselves. One such alternate would be to replace the nested ifs with complex conditions:
...
if (isMarried && income <= 20000) {...
else if (isMarried && income <= 50000) {...
else if (isMarried && income <= 100000) {...
else if (isMarried) {...
else if (!isMarried income <= 10000) {...
...
Although that structure could produce the right values, the repetition is unnecessary and just serves to make the code harder to read or update.
It is also worth mentioning that we have a lot of βmagic numbersβ like 50000 in this program that probably should be constants. In a real tax program, those special values would likely be stored in a configuration file and read into variables so the tax brackets could be changed without rebuilding the entire program.