SHARE
TWEET

Ayende's Tax Problem

a guest Sep 23rd, 2011 747 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using NUnit.Framework;
  4.  
  5. namespace TaxRate.Tests.Unit
  6. {
  7.     [TestFixture]
  8.     public class TaxCalculatorTests
  9.     {
  10.         [Test]
  11.         [TestCase(5000, 500)]
  12.         [TestCase(5800, 609.2)]
  13.         [TestCase(9000, 1087.8)]
  14.         [TestCase(15000, 2532.9)]
  15.         [TestCase(50000, 15068.1)]
  16.         public void Calculate_Given15k_ShouldReturnCorrectResult(decimal grossSalary, decimal expectedTax)
  17.         {
  18.             // Arrange
  19.             IList<Rate> rates = new List<Rate>();
  20.             rates.Add(new Rate(5070 - 0, (decimal)0.10));
  21.             rates.Add(new Rate(8660 - 5070, (decimal)0.14));
  22.             rates.Add(new Rate(14070 - 8660, (decimal)0.23));
  23.             rates.Add(new Rate(21240 - 14070, (decimal)0.30));
  24.             rates.Add(new Rate(40230 - 21240, (decimal)0.33));
  25.             rates.Add(new Rate(int.MaxValue - 40230, (decimal)0.45));
  26.             var calculator = new TaxCalculator(rates);
  27.            
  28.             // Act
  29.             decimal tax = calculator.CalculateTax(grossSalary);
  30.  
  31.             // Assert
  32.             Assert.AreEqual(expectedTax, tax, "Tax is not correct");
  33.         }
  34.     }
  35.  
  36.     public interface ITaxCalculator
  37.     {
  38.         decimal CalculateTax(decimal grossSalary);
  39.     }
  40.  
  41.     public class Rate
  42.     {
  43.         public decimal Amount { get; private set; }
  44.         public decimal Percent { get; private set; }
  45.  
  46.         public Rate(decimal amount, decimal percent)
  47.         {
  48.             Amount = amount;
  49.             Percent = percent;
  50.         }
  51.     }
  52.  
  53.     public class TaxCalculator : ITaxCalculator
  54.     {
  55.         private readonly IList<Rate> _rates;
  56.  
  57.         public TaxCalculator(IList<Rate> rates)
  58.         {
  59.             _rates = rates;
  60.         }
  61.  
  62.         public decimal CalculateTax(decimal grossSalary)
  63.         {
  64.             decimal totalTax = 0;
  65.             decimal remaining = grossSalary;
  66.             int currentRateId = 0;
  67.  
  68.             while (remaining > 0)
  69.             {
  70.                 var rate = _rates[currentRateId];
  71.  
  72.                 decimal taxable = Math.Min(rate.Amount, remaining);
  73.  
  74.                 decimal taxForThisRate = taxable * rate.Percent;
  75.  
  76.                 totalTax += taxForThisRate;
  77.  
  78.                 remaining -= rate.Amount;
  79.                 currentRateId++;
  80.             }
  81.  
  82.             return totalTax;
  83.         }
  84.     }
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top