Created
September 20, 2015 20:22
-
-
Save tarunbod/a450992fa65b2f1e90a1 to your computer and use it in GitHub Desktop.
Tax Calculator for single filers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class TaxCalculator { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
//System.out.print("Enter your gross income: "); | |
//double income = scanner.nextInt(); | |
double taxed = TaxSection.getAmountTaxed(65535); | |
System.out.println("You must pay " + taxed + " amount in taxes."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum TaxSection { | |
/*TEN(0.10, 0, 9225), | |
FIFTEEN(0.15, 9225, 37450), | |
TWENTY_FIVE(0.25, 37450, 90750), | |
TWENTY_EIGHT(0.28, 90750, 189300), | |
THIRTY_THREE(0.33, 189300, 411500), | |
THIRTY_FIVE(0.35, 411500, 413200), | |
THIRTY_NINE(0.396, 413200, Integer.MAX_VALUE);*/ | |
TEN(0.10, 1, 9075), | |
FIFTEEN(0.15, 9076, 36900), | |
TWENTY_FIVE(0.25, 36901, 89350), | |
TWENTY_EIGHT(0.28, 89351, 186350), | |
THIRTY_THREE(0.33, 183651, 405100), | |
THIRTY_FIVE(0.35, 405101, 406750), | |
THIRTY_NINE(0.396, 406751, Integer.MAX_VALUE); | |
private final int lowerBound; | |
private final int upperBound; | |
private final double rate; | |
private TaxSection(double rate, int lowerBound, int upperBound) { | |
this.rate = rate; | |
this.upperBound = upperBound; | |
this.lowerBound = lowerBound; | |
} | |
public int getLowerBound() { | |
return lowerBound; | |
} | |
public int getUpperBound() { | |
return upperBound; | |
} | |
public double getRate() { | |
return rate; | |
} | |
@Override | |
public String toString() { | |
return name() + "[lowerBound=" + lowerBound + ", upperBound=" + upperBound + ", rate=" + rate + "]"; | |
} | |
public static TaxSection getSectionForIncome(double income) { | |
for (TaxSection section : TaxSection.values()) { | |
if (income >= section.getLowerBound() && income <= section.getUpperBound()) { | |
return section; | |
} | |
} | |
return null; | |
} | |
public static double getAmountTaxed(double income) { | |
TaxSection lastSection = getSectionForIncome(income); | |
double taxed = 0; | |
while (lastSection != null) { | |
taxed += (income -= lastSection.getLowerBound()) * lastSection.getRate(); | |
if (lastSection == TEN) { | |
lastSection = null; | |
} else { | |
lastSection = getSectionForIncome(income); | |
} | |
} | |
return taxed; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment