Skip to content

Instantly share code, notes, and snippets.

@hazzik hazzik/TaxCalculator.cs
Last active Sep 27, 2015

Embed
What would you like to do?
public class Tests
{
[Fact]
public void Tax10()
{
var calc = new TaxCalculator();
decimal tax = calc.Tax(5000);
Assert.Equal(500, tax);
}
[Fact]
public void Tax14()
{
var calc = new TaxCalculator();
decimal tax = calc.Tax(5800);
Assert.Equal(609.2m, tax);
}
[Fact]
public void Tax23()
{
var calc = new TaxCalculator();
decimal tax = calc.Tax(9000);
Assert.Equal(1087.8m, tax);
}
[Fact]
public void Tax30()
{
var calc = new TaxCalculator();
decimal tax = calc.Tax(15000);
Assert.Equal(2532.9m, tax);
}
[Fact]
public void Tax45()
{
var calc = new TaxCalculator();
decimal tax = calc.Tax(50000);
Assert.Equal(15068.1m, tax);
}
}
public class TaxCalculator
{
private readonly IEnumerable<Tuple<decimal, decimal>> taxRates = new[]
{
Tuple.Create(40230m, .45m),
Tuple.Create(21240m, .33m),
Tuple.Create(14070m, .30m),
Tuple.Create(8660m, .23m),
Tuple.Create(5070m, .14m),
Tuple.Create(0m, .1m),
};
public decimal Tax(decimal sum)
{
decimal result = 0m;
decimal x = sum;
foreach (var taxRate in taxRates)
{
if (x > taxRate.Item1)
{
result += (x - taxRate.Item1)*taxRate.Item2;
x = taxRate.Item1;
}
}
return result;
}
}
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.