Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Who cares about Domain Rules?

, 20 Nov 2006 MIT
With this article, I am evolving a domain problem towards the best possible solution.
domainrules_src.zip
BusinessRules
0 - Procedural_Way
0 - Procedural_Way.smp
ProceduralWay.cd
Tests
1 - Evolution One
1 - Evolution One.smp
EvolutionOne.cd
Tests
2 - Evolution Two
2 - Evolution Two.smp
EvolutionTwo.cd
Tests
3 - Evolution Three
3 - Evolution Three.smp
EvolutionThree.cd
IncomeTaxEngine.cd
Tests
4 - Evolution Four
4 - Evolution Four.smp
EvolutionFour.cd
Tests
5 - Evolution Five
5 - Evolution Five.smp
EvolutionFive.cd
TaxBandGenerator.cd
Tests
bin
Debug
BusinessRules.dll
BusinessRules.dll.lastcodeanalysissucceeded
NMock2.dll
BusinessRules.csproj.user
obj
Debug
BusinessRules.dll
TempPE
Properties
Tools
NMock2.dll
nunit.framework.dll
using System;

namespace BusinessRules.Evolution_Five
{
    /// <summary>
    /// The Tax Band domain object symbolizes the bands found in countries tax regimes. 
    /// </summary>
    /// <remarks>
    /// <para>The tax band domain object is an immutable object and contains three properties:</para>
    /// <list type="number">
    /// <item><description><c>StartAmount</c> is the lower limit in the band.</description></item>
    /// <item><description><c>EndAmount</c> is the upper limit in the band.</description></item>
    /// <item><description><c>TaxRate</c> is the rate applied to the band.</description></item>
    /// </list>
    /// </remarks>
    public class TaxBand
    {
        #region Contants

        private const double ZeroValue = 0.0;

        #endregion

        #region Fields

        private double lowerLimitAmount;
        private double upperLimitAmount;
        private double taxRate;

        #endregion

        #region C'tors

        /// <summary>
        /// Initializes a new instance of the <see cref="TaxBand"/> class.
        /// </summary>
        /// <param name="lowerLimitAmount">The start amount.</param>
        /// <param name="upperLimitAmount">The end amount.</param>
        /// <param name="taxRate">The tax rate.</param>
        public TaxBand(double lowerLimitAmount, double upperLimitAmount, double taxRate)
        {
            this.lowerLimitAmount = lowerLimitAmount;
            this.upperLimitAmount = upperLimitAmount;
            this.taxRate = taxRate;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Calculates the band tax portion.
        /// </summary>
        /// <param name="income">The investors income.</param>
        /// <returns>
        /// Returns a double representing the portion of the tax liability of this tax band.
        /// </returns>
        public double CalculateTaxPortion(double income)
        {
            if (EqualToOrLessThan(income, LowerLimitAmount))
            {
                return ZeroValue;
            }
            else if (FallWithinTaxBand(income))
            {
                return (income - lowerLimitAmount) * taxRate;
            }
            else if (EqualToOrGreaterThan(income, UpperLimitAmount))
            {
                return (upperLimitAmount - lowerLimitAmount) * taxRate;
            }
            else
            {
                return ZeroValue;
            }
        }


        /// <summary>
        /// Determines whether the income fall within the tax band.
        /// </summary>
        /// <param name="income">The income.</param>
        /// <returns>
        /// 	<c>true</c> if the income fall within the tax band; otherwise, <c>false</c>.
        /// </returns>
        private bool FallWithinTaxBand(double income)
        {
            return EqualToOrGreaterThan(income, LowerLimitAmount) && EqualToOrLessThan(income, UpperLimitAmount);
        }

        /// <summary>
        /// Income is equal to or greater than the band value.
        /// </summary>
        /// <param name="income">The income.</param>
        /// <param name="bandValue">The band value.</param>
        /// <returns>
        /// 	<c>true</c> if the income is equal to or greater than the band value; otherwise, <c>false</c>.
        /// </returns>
        private bool EqualToOrGreaterThan(double income, double bandValue)
        {
            return income >= bandValue;
        }

        /// <summary>
        /// Income is equal to or less than the band value.
        /// </summary>
        /// <param name="income">The income.</param>
        /// <param name="bandValue">The band value.</param>
        /// <returns>
        /// 	<c>true</c> if the income is equal to or less than the band value; otherwise, <c>false</c>.
        /// </returns>
        private bool EqualToOrLessThan(double income, double bandValue)
        {
            return income <= bandValue;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the tax rate.
        /// </summary>
        /// <value>The tax rate.</value>
        public double TaxRate
        {
            get { return taxRate; }
        }

        /// <summary>
        /// Gets the lower limit amount.
        /// </summary>
        /// <value>The lower limit amount.</value>
        public double LowerLimitAmount
        {
            get { return lowerLimitAmount; }
        }

        /// <summary>
        /// Gets the upper limit amount.
        /// </summary>
        /// <value>The upper limit amount.</value>
        public double UpperLimitAmount
        {
            get { return upperLimitAmount; }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Maruis Marais
Web Developer
New Zealand New Zealand
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150129.1 | Last Updated 21 Nov 2006
Article Copyright 2006 by Maruis Marais
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid