Skip to content

Instantly share code, notes, and snippets.

@StevePy
Created September 23, 2011 13:17
Show Gist options
  • Save StevePy/1237303 to your computer and use it in GitHub Desktop.
Save StevePy/1237303 to your computer and use it in GitHub Desktop.
Ayende's Tax Thing
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;
}
}
}
@StevePy
Copy link
Author

StevePy commented Sep 23, 2011

Minor formatting change for readability in both VS & Gist

@StevePy
Copy link
Author

StevePy commented Sep 23, 2011

Ooh, syntax highlighting based on file extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment