Skip to content

Instantly share code, notes, and snippets.

/gist:9cbe9ab68f75f52eebfb Secret
Created Sep 24, 2011

Embed
What would you like to do?
Tax calculator
public class TaxRate
{
public TaxRate(decimal vat, decimal limit)
{
Vat = vat;
Limit = limit;
}
public decimal Vat { get; private set; }
public decimal Limit { get; private set; }
}
public class TaxCalculator
{
private static IList<TaxRate> taxRates;
static TaxCalculator()
{
taxRates = new List<TaxRate>()
{
new TaxRate(10,0),
new TaxRate(14,5070),
new TaxRate(23,8660),
new TaxRate(30,14070),
new TaxRate(33,21240),
new TaxRate(45,40230)
};
}
public Decimal GetTaxAmount(decimal income)
{
decimal taxToPay = 0;
decimal upperLimit;
TaxRate currentRate;
if (income<=0)
return taxToPay;
upperLimit = income;
for (int i=(taxRates.Count-1); i>-1; i--)
{
currentRate = taxRates[i];
if (income < currentRate.Limit)
continue;
taxToPay += CalculatePercentage(upperLimit - currentRate.Limit, currentRate.Vat);
upperLimit = currentRate.Limit;
}
return taxToPay;
}
private decimal CalculatePercentage(decimal number, decimal percentage)
{
return (number*percentage)/100;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.