HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Federal and state income tax calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
federaltaxincomestateandcalculator

Problem

I'm trying to optimize this code by:

  • condensing repetitive statements



  • Improving the code/make it less confusing



  • Branch out to include other filing status such as "married" "joint" filing status



  • branch out to all 50 states



Any suggestions how to do any of these things would be awesome.

```
/** This program calculates Single-filing 2015 Federal Tax Schedule and Georgia State income Tax For GA residents making >= $7,000 */

import java.util.Scanner;

public class fedIncomeTax
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter income over $7,000: "); // this is because there are different tax rates for GA residents who earn below that mark

if (in.hasNextDouble()) // Checks whether the next input is a real number && if false prints error message
{

double begIncome = in.nextDouble();

// Georgia State tax for incomes >= $7,000
final double GA_TAX_1 = 0.06;
final double GA_TAX_2 = 230;

// fed tax rates
final double FED_TAX_10 = 0.10; // $1 - 9075
final double FED_TAX_15 = 0.15; // $9076 - 36900
final double FED_TAX_25 = 0.25; // $36901 - 89350
final double FED_TAX_28 = 0.28; // $89351 - 186350
final double FED_TAX_33 = 0.33; // $186,351 - 405,100
final double FED_TAX_35 = 0.35; // $405,101 - $406,750
final double FED_TAX_39 = 0.396; // > $406,750

// income brackets
final double BRACKET_10 = 9075;
final double BRACKET_15 = 36900;
final double BRACKET_25 = 89350;
final double BRACKET_28 = 186350;
final double BRACKET_33 = 405100;
final double BRACKET_35 = 406750;
final double BRACKET_39 = 406751;

// max amount per bracket
final double MAX_TAX_15 = 907.50;
final double MAX_TAX_25 = 5081.25;
final double MAX_TAX_28 = 18193.75;
final double MAX_TAX_33 = 45353.75;
final double MAX_TAX_35 = 117541.25;
final double MAX_TAX_39 = 118118.75;

//double begIncome = in.nextDouble();
if (begIncome BRACKET_10 && begIncome BRACKET_15 && begIncome BRACKET_25 && begIncome BRACKET_28 &&

Solution

That huge if/else statement you have is terrible.

First, you print out the values using the same print statement at the end of each if, just move that to the end:

else if (begIncome > BRACKET_35)                                         // > $406,750
{
   double govTax = (begIncome - BRACKET_35) * FED_TAX_39 + MAX_TAX_39;
   double localTax = (begIncome * GA_TAX_1) + GA_TAX_2;
   double totalTax = govTax + localTax;
   double endIncome = begIncome - totalTax;
   System.out.printf("Your tax rate is:       $ %8.2f\n", totalTax);
   System.out.printf ("Your ending income is:  $ %8.2f", endIncome);
}


Becomes:

else if (begIncome > BRACKET_35)                                         // > $406,750
{
   double govTax = (begIncome - BRACKET_35) * FED_TAX_39 + MAX_TAX_39;
   double localTax = (begIncome * GA_TAX_1) + GA_TAX_2;
   double totalTax = govTax + localTax;
   double endIncome = begIncome - totalTax;
}  

System.out.printf("Your tax rate is:       $ %8.2f\n", totalTax);
System.out.printf ("Your ending income is:  $ %8.2f", endIncome);


Then, you have the same code run with different values in each of the if statements. Why not separate that into a method, and pass the values needed to calculate the required values? If you do this, you would be able to change your if into something like this:

else if (begIncome > BRACKET_33 && begIncome  BRACKET_35)                                         // > $406,750
{
   CalcTax(begIncome, BRACKET_35);
}


If you do this, you may want to make your final static variables global so you don't have to pass them to the method - both methods will just be able to access them. However, as a general rule, try to keep the scope of variables as tight as possible.

Also, instead of having different variables for the different tax values, you could create an array of tax brackets with all the values:

final static int[] TaxBrackets = { 9075, 36900, /* other values... */ };

Code Snippets

else if (begIncome > BRACKET_35)                                         // > $406,750
{
   double govTax = (begIncome - BRACKET_35) * FED_TAX_39 + MAX_TAX_39;
   double localTax = (begIncome * GA_TAX_1) + GA_TAX_2;
   double totalTax = govTax + localTax;
   double endIncome = begIncome - totalTax;
   System.out.printf("Your tax rate is:       $ %8.2f\n", totalTax);
   System.out.printf ("Your ending income is:  $ %8.2f", endIncome);
}
else if (begIncome > BRACKET_35)                                         // > $406,750
{
   double govTax = (begIncome - BRACKET_35) * FED_TAX_39 + MAX_TAX_39;
   double localTax = (begIncome * GA_TAX_1) + GA_TAX_2;
   double totalTax = govTax + localTax;
   double endIncome = begIncome - totalTax;
}  

System.out.printf("Your tax rate is:       $ %8.2f\n", totalTax);
System.out.printf ("Your ending income is:  $ %8.2f", endIncome);
else if (begIncome > BRACKET_33 && begIncome <= BRACKET_35)            // $405,101 - $406,750
{
   CalcTax(begIncome, BRACKET_33);
}
else if (begIncome > BRACKET_35)                                         // > $406,750
{
   CalcTax(begIncome, BRACKET_35);
}
final static int[] TaxBrackets = { 9075, 36900, /* other values... */ };

Context

StackExchange Code Review Q#79684, answer score: 6

Revisions (0)

No revisions yet.