Created
September 23, 2011 15:45
-
-
Save anonymous/1237707 to your computer and use it in GitHub Desktop.
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 decimal MinSalaryAmount { get; set; } | |
public decimal MaxSalaryAmount { get; set; } | |
public decimal Rate { get; set; } | |
} |
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 TaxRateCalculation | |
{ | |
private readonly IList<TaxRate> _taxRateList; | |
public TaxRateCalculation() | |
{ | |
_taxRateList = new List<TaxRate>(); | |
_taxRateList.Add(new TaxRate { MinSalaryAmount = 0, MaxSalaryAmount = 5070, Rate = 0.1m }); | |
_taxRateList.Add(new TaxRate { MinSalaryAmount = 5071, MaxSalaryAmount = 8660, Rate = 0.14m }); | |
_taxRateList.Add(new TaxRate { MinSalaryAmount = 8661, MaxSalaryAmount = 14070, Rate = 0.23m }); | |
_taxRateList.Add(new TaxRate { MinSalaryAmount = 14071, MaxSalaryAmount = 21240, Rate = 0.3m }); | |
_taxRateList.Add(new TaxRate { MinSalaryAmount = 21241, MaxSalaryAmount = 40230, Rate = 0.33m }); | |
_taxRateList.Add(new TaxRate { MinSalaryAmount = 40231, MaxSalaryAmount = decimal.MaxValue, Rate = 0.45m }); | |
} | |
public decimal CalculateTaxFromSalary(decimal salary) | |
{ | |
decimal tax = 0; | |
decimal originalSalary = Math.Round(salary, MidpointRounding.ToEven); | |
var orderedListTaxRate = from trl in _taxRateList | |
orderby trl.MinSalaryAmount ascending | |
select trl; | |
foreach (var taxRate in orderedListTaxRate) | |
{ | |
if (salary <= 0) | |
{ | |
break; | |
} | |
var slice = originalSalary <= taxRate.MaxSalaryAmount && originalSalary >= taxRate.MinSalaryAmount | |
? salary | |
: taxRate.MaxSalaryAmount - taxRate.MinSalaryAmount; | |
tax += slice*taxRate.Rate; | |
salary -= slice; | |
} | |
return tax; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment