1

Class DIAGRAM: enter image description here

#DEPARTMENT CLASS:

public class Department
{
    public int deptNumber;
    public  bool isProducing;
    public  float produce;
    public float GetIncentive(float multiplyFactor)
    {
        float incentive = 0;
        if (isProducing)
        {
            incentive = produce * multiplyFactor;
        }
        return incentive;
    }       
}

#EMPLOYEE CLASS:


public class Employee
{
    public int fixedSalary = 50000;
    public DateTime workingFrom = new DateTime(2012, 02, 27);
    public Department dept { get; set; }


    public double GetAllowance()
    {
        return GetAllowance(new DateTime(2014, 03, 31));
    }

    public double GetAllowance(DateTime cutOffDate)
    {
        double workExperience = ((cutOffDate - workingFrom).Days)/365;
        double allowance = 0;
        if (workExperience < 5)
        {
            allowance = 0.05 * fixedSalary;
        }
        else if(workExperience >= 5 && workExperience < 10)
        {
            allowance = 0.1 * fixedSalary;
        }
        else if(workExperience >= 10 && workExperience < 15)
        {
            allowance = 0.15 * fixedSalary;
        }
        else if (workExperience >= 15)
        {
            allowance = 0.2 * fixedSalary;
        }
        Console.WriteLine("work experience:{0}",workExperience);
        Console.WriteLine("allowance:{0}", allowance);
        return allowance;
    }


    public double GetTotalSalary(DateTime cutOffDate, float multiplyFactor)
    {            
        return fixedSalary + GetAllowance() + ***dept.GetIncentive(multiplyFactor);***
    }


    public double GetTotalSalary(float multiplyFactor)
    {
        return fixedSalary + GetAllowance() + dept.GetIncentive(multiplyFactor);
    }
}

I am trying to implemet the concept of Aggregation between Department and Employee class, an employee can work under only one department.

While I am calling the GetIncentive method present in Department class using the property object dept that I have created in Employee class, it gives me error - Object reference not set to an instance of an object. Line is highlighted for a quick look.

How to go about implementing the "Aggregation" relationship in between these two classes and correct the error? Help is much appreciated! Thanks in advance.

0 Answers0