Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using NUnit.Framework;
- namespace TaxRate.Tests.Unit
- {
- [TestFixture]
- public class TaxCalculatorTests
- {
- [Test]
- [TestCase(5000, 500)]
- [TestCase(5800, 609.2)]
- [TestCase(9000, 1087.8)]
- [TestCase(15000, 2532.9)]
- [TestCase(50000, 15068.1)]
- public void Calculate_Given15k_ShouldReturnCorrectResult(decimal grossSalary, decimal expectedTax)
- {
- // Arrange
- IList<Rate> rates = new List<Rate>();
- rates.Add(new Rate(5070 - 0, (decimal)0.10));
- rates.Add(new Rate(8660 - 5070, (decimal)0.14));
- rates.Add(new Rate(14070 - 8660, (decimal)0.23));
- rates.Add(new Rate(21240 - 14070, (decimal)0.30));
- rates.Add(new Rate(40230 - 21240, (decimal)0.33));
- rates.Add(new Rate(int.MaxValue - 40230, (decimal)0.45));
- var calculator = new TaxCalculator(rates);
- // Act
- decimal tax = calculator.CalculateTax(grossSalary);
- // Assert
- Assert.AreEqual(expectedTax, tax, "Tax is not correct");
- }
- }
- public interface ITaxCalculator
- {
- decimal CalculateTax(decimal grossSalary);
- }
- public class Rate
- {
- public decimal Amount { get; private set; }
- public decimal Percent { get; private set; }
- public Rate(decimal amount, decimal percent)
- {
- Amount = amount;
- Percent = percent;
- }
- }
- public class TaxCalculator : ITaxCalculator
- {
- private readonly IList<Rate> _rates;
- public TaxCalculator(IList<Rate> rates)
- {
- _rates = rates;
- }
- public decimal CalculateTax(decimal grossSalary)
- {
- decimal totalTax = 0;
- decimal remaining = grossSalary;
- int currentRateId = 0;
- while (remaining > 0)
- {
- var rate = _rates[currentRateId];
- decimal taxable = Math.Min(rate.Amount, remaining);
- decimal taxForThisRate = taxable * rate.Percent;
- totalTax += taxForThisRate;
- remaining -= rate.Amount;
- currentRateId++;
- }
- return totalTax;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement