Skip to content

Instantly share code, notes, and snippets.

Created September 23, 2011 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1237707 to your computer and use it in GitHub Desktop.
Save anonymous/1237707 to your computer and use it in GitHub Desktop.
public class TaxRate
{
public decimal MinSalaryAmount { get; set; }
public decimal MaxSalaryAmount { get; set; }
public decimal Rate { get; set; }
}
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