Created
September 23, 2011 13:17
-
-
Save StevePy/1237303 to your computer and use it in GitHub Desktop.
Ayende's Tax Thing
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 class TaxCalculator | |
{ | |
private static readonly IList<TaxBracket> TaxBrackets; | |
static TaxCalculator() | |
{ | |
TaxBrackets = setupTaxBrackets(); | |
} | |
public decimal CalculateTax( decimal grossIncome ) | |
{ | |
var taxBracket = TaxBrackets.First( tax => tax.UpperBoundary >= grossIncome ); | |
var taxBracketIndex = TaxBrackets.IndexOf( taxBracket ); | |
var totalTax = (taxBracketIndex == 0) | |
? grossIncome * taxBracket.Rate | |
: (grossIncome - TaxBrackets[taxBracketIndex - 1].UpperBoundary) * taxBracket.Rate | |
+ TaxBrackets[taxBracketIndex - 1].TaxOnUpperBoundary; | |
return totalTax; | |
} | |
private static IList<TaxBracket> setupTaxBrackets() | |
{ | |
var taxBrackets = new List<TaxBracket> { | |
new TaxBracket( 0.1m, 5070m ) | |
, new TaxBracket( 0.14m, 8660m ) | |
, new TaxBracket( 0.23m, 14070m ) | |
, new TaxBracket( 0.3m, 21240m ) | |
, new TaxBracket( 0.33m, 40230m ) | |
, new TaxBracket( 0.45m, decimal.MaxValue ) | |
}; | |
for (int counter = 1; counter < taxBrackets.Count; counter++) | |
{ // Update boundary tax amounts to reflect previous upper boundary tax amounts. | |
taxBrackets[counter].TaxOnUpperBoundary = taxBrackets[counter - 1].TaxOnUpperBoundary | |
+ (taxBrackets[counter].UpperBoundary - taxBrackets[counter - 1].UpperBoundary) * taxBrackets[counter].Rate; | |
} | |
return taxBrackets; | |
} | |
private class TaxBracket | |
{ | |
public decimal UpperBoundary | |
{ | |
get; | |
private set; | |
} | |
public decimal Rate | |
{ | |
get; | |
private set; | |
} | |
public decimal TaxOnUpperBoundary | |
{ | |
get; | |
set; | |
} | |
public TaxBracket( decimal rate, decimal upperBounds ) | |
{ | |
Rate = rate; | |
UpperBoundary = upperBounds; | |
TaxOnUpperBoundary = rate * upperBounds; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ooh, syntax highlighting based on file extension.