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 < 7000)
{
System.out.println("Error: Please enter an income figure of $7000 or more.");
}
// Now we know that the user entered a real number
else
{
// Compute taxes by income range
if (begIncome <= BRACKET_10) // $1 - 9075
{
double govTax = begIncome * (FED_TAX_10);
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_10 && begIncome <= BRACKET_15) // $9076 - 36900
{
double govTax = (begIncome - BRACKET_10) * FED_TAX_15 + MAX_TAX_15;
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_15 && begIncome <= BRACKET_25) // $36901 - 89350
{
double govTax = (begIncome - BRACKET_15) * FED_TAX_25 + MAX_TAX_25;
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_25 && begIncome <= BRACKET_28) // $89351 - 186350
{
double govTax = (begIncome - BRACKET_25) * FED_TAX_28 + MAX_TAX_28;
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_28 && begIncome <= BRACKET_33) // $186,351 - 405,100
{
double govTax = (begIncome - BRACKET_28) * FED_TAX_33 + MAX_TAX_33;
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
{
double govTax = (begIncome - BRACKET_33) * FED_TAX_35 + MAX_TAX_35;
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
{
System.out.println("Error: Please input a numerical value greater than 0.");
}
}
}
float
ordouble
to store values. Rounding errors can become an issue. Fixed-precision or something likeBigDecimal
is a better way to do it. \$\endgroup\$