-
-
Save anonymous/9cbe9ab68f75f52eebfb to your computer and use it in GitHub Desktop.
Tax calculator
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 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