Creating the Classes Receipt, MileageAllowance and PerDiemAllowance

All three subclasses of Expense follow the same design pattern. They are derived from Expense and implement the abstract properties. Let’s begin with the class Receipt:

using System;

using NDO;

namespace BusinessClasses

{

    [NDOPersistent]

    public class Receipt : Expense

    {

        string text;

        public string ReceiptText

        {

            get { return text; }

            set { text = value; }

        }

        decimal sum;

        public decimal Sum

        {

            get { return sum; }

            set { sum = value; }

        }

        public Receipt()

        {

        }

        public override decimal Amount

        {

            get { return sum; }

        }

        public override string Text

        {

            get { return text; }

        }

    }

}

The class needs the two properties ReceiptText and Sum for the manipulation of your data – for example in a user interface. They encapsulate the two persistent fields. Furthermore the class inherits the persistent field date. The two properties Amount and Item are needed for the polymorphic specification of costs with items. It seems as if the logic is implemented twice here. But the polymorphic façade of the class consists of read-only-properties, while the specific properties ReceiptText and Sum are also writable.

This is the source of the class MileageAllowance. 40 cents per mile is reimbursed in this simplified business example:

using System;

using NDO;

namespace BusinessClasses

{

    [NDOPersistent]

    public class MileageAllowance : Expense

    {

        int milesDriven;

        public int MilesDriven

        {

            get { return milesDriven; }

            set { milesDriven = value; }

        }

        public MileageAllowance()

        {

        }

        public override decimal Amount

        {

                //TODO: call accountant and get correct values ;-)

            get { return (decimal) milesDriven * (decimal) 0.4; }

        }

        public override string Text

        {

            get { return "Mileage Allowance"; }

        }

    }

}

Note that you could easily implement the class with the reimbursement rate read from the database or a configuration file. The structure and logic of the MileageAllowance class is totally different from the structure and logic of the Receipt class. The class only needs the mileage driven as an integer value.

The class PerDiemAllowance is similar to the MileageAllowance; not only in its structure, but in the simplification of the logic too:

using System;

using NDO;

namespace BusinessClasses

{

    [NDOPersistent]

    public class PerDiemAllowance : Expense

    {

        decimal hours;

        public decimal Hours

        {

            get { return hours; }

            set { hours = value; }

        }

        public PerDiemAllowance()

        {

        }

        public override decimal Amount

        {

            get

            {

                //TODO: call accountant once more  ;-)

                if (hours <= 8)

                    return 0;

                if (hours <= 10)

                    return 5;

                if (hours <= 12)

                    return 10;

                return 20;

            }

        }

        public override string Text

        {

            get { return "Per Diem Allowance"; }

        }

    }

}